本文整理汇总了Java中com.alibaba.dubbo.remoting.exchange.ExchangeClient类的典型用法代码示例。如果您正苦于以下问题:Java ExchangeClient类的具体用法?Java ExchangeClient怎么用?Java ExchangeClient使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ExchangeClient类属于com.alibaba.dubbo.remoting.exchange包,在下文中一共展示了ExchangeClient类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testClient
import com.alibaba.dubbo.remoting.exchange.ExchangeClient; //导入依赖的package包/类
@Test
public void testClient() throws Throwable {
// 读取参数
if (PerformanceUtils.getProperty("server", null) == null) {
logger.warn("Please set -Dserver=127.0.0.1:9911");
return;
}
final String server = System.getProperty("server", "127.0.0.1:9911");
final String transporter = PerformanceUtils.getProperty(Constants.TRANSPORTER_KEY, Constants.DEFAULT_TRANSPORTER);
final String serialization = PerformanceUtils.getProperty(Constants.SERIALIZATION_KEY, Constants.DEFAULT_REMOTING_SERIALIZATION);
final int timeout = PerformanceUtils.getIntProperty(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT);
int sleep = PerformanceUtils.getIntProperty("sleep", 60*1000*60);
final String url = "exchange://" + server + "?transporter=" + transporter + "&serialization=" + serialization + "&timeout=" + timeout;
ExchangeClient exchangeClient =initClient(url);
Thread.sleep(sleep);
closeClient(exchangeClient);
}
示例2: getClients
import com.alibaba.dubbo.remoting.exchange.ExchangeClient; //导入依赖的package包/类
private ExchangeClient[] getClients(URL url){
//是否共享连接
boolean service_share_connect = false;
int connections = url.getParameter(Constants.CONNECTIONS_KEY, 0);
//如果connections不配置,则共享连接,否则每服务每连接
if (connections == 0){
service_share_connect = true;
connections = 1;
}
ExchangeClient[] clients = new ExchangeClient[connections];
for (int i = 0; i < clients.length; i++) {
if (service_share_connect){
clients[i] = getSharedClient(url);
} else {
clients[i] = initClient(url);
}
}
return clients;
}
示例3: getSharedClient
import com.alibaba.dubbo.remoting.exchange.ExchangeClient; //导入依赖的package包/类
/**
*获取共享连接
*/
private ExchangeClient getSharedClient(URL url){
String key = url.getAddress();
ReferenceCountExchangeClient client = referenceClientMap.get(key);
if ( client != null ){
if ( !client.isClosed()){
client.incrementAndGetCount();
return client;
} else {
// logger.warn(new IllegalStateException("client is closed,but stay in clientmap .client :"+ client));
referenceClientMap.remove(key);
}
}
ExchangeClient exchagneclient = initClient(url);
client = new ReferenceCountExchangeClient(exchagneclient, ghostClientMap);
referenceClientMap.put(key, client);
ghostClientMap.remove(key);
return client;
}
示例4: getClient
import com.alibaba.dubbo.remoting.exchange.ExchangeClient; //导入依赖的package包/类
private ExchangeClient getClient(Invoker<?> invoker){
if (invoker.getUrl().getParameter(Constants.CONNECTIONS_KEY, 1) == 1){
return getInvokerClient(invoker);
} else {
ReferenceCountExchangeClient client = getReferenceClient(invoker);
try {
Field clientField = ReferenceCountExchangeClient.class.getDeclaredField("client");
clientField.setAccessible(true);
return (ExchangeClient) clientField.get(client);
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.getMessage());
throw new RuntimeException(e);
}
}
}
示例5: isAvailable
import com.alibaba.dubbo.remoting.exchange.ExchangeClient; //导入依赖的package包/类
@Override
public boolean isAvailable() {
if (!super.isAvailable()) {
return false;
}
for (ExchangeClient client : clients){
if (client.isConnected()
&& !client.hasAttribute(Constants.CHANNEL_ATTRIBUTE_READONLY_KEY)){
//cannot write == not Available ?
return true ;
}
}
return false;
}
示例6: testExport
import com.alibaba.dubbo.remoting.exchange.ExchangeClient; //导入依赖的package包/类
@Test
public void testExport() {
RegistryProtocol registryProtocol = new RegistryProtocol();
registryProtocol.setCluster(new FailfastCluster());
registryProtocol.setRegistryFactory(ExtensionLoader.getExtensionLoader(RegistryFactory.class).getAdaptiveExtension());
Protocol dubboProtocol = DubboProtocol.getDubboProtocol();
registryProtocol.setProtocol(dubboProtocol);
URL newRegistryUrl = registryUrl.addParameter(Constants.EXPORT_KEY, serviceUrl);
DubboInvoker<DemoService> invoker = new DubboInvoker<DemoService>(DemoService.class,
newRegistryUrl, new ExchangeClient[] { new MockedClient("10.20.20.20", 2222, true) });
Exporter<DemoService> exporter = registryProtocol.export(invoker);
Exporter<DemoService> exporter2 = registryProtocol.export(invoker);
//同一个invoker,多次export的exporter不同
Assert.assertNotSame(exporter, exporter2);
exporter.unexport();
exporter2.unexport();
}
示例7: testExport
import com.alibaba.dubbo.remoting.exchange.ExchangeClient; //导入依赖的package包/类
@Test
public void testExport() {
RegistryProtocol registryProtocol = new RegistryProtocol();
registryProtocol.setCluster(new FailfastCluster());
registryProtocol.setRegistryFactory(ExtensionLoader.getExtensionLoader(RegistryFactory.class).getAdaptiveExtension());
Protocol dubboProtocol = DubboProtocol.getDubboProtocol();
registryProtocol.setProtocol(dubboProtocol);
URL newRegistryUrl = registryUrl.addParameter(Constants.EXPORT_KEY, serviceUrl);
DubboInvoker<DemoService> invoker = new DubboInvoker<DemoService>(DemoService.class,
newRegistryUrl, new ExchangeClient[]{new MockedClient("10.20.20.20", 2222, true)});
Exporter<DemoService> exporter = registryProtocol.export(invoker);
Exporter<DemoService> exporter2 = registryProtocol.export(invoker);
//同一个invoker,多次export的exporter不同
Assert.assertNotSame(exporter, exporter2);
exporter.unexport();
exporter2.unexport();
}
示例8: getExchangeChannel
import com.alibaba.dubbo.remoting.exchange.ExchangeClient; //导入依赖的package包/类
@Override
public ExchangeChannel getExchangeChannel(InetSocketAddress remoteAddress) {
String host = remoteAddress.getAddress() != null ? remoteAddress.getAddress().getHostAddress() : remoteAddress.getHostName();
int port = remoteAddress.getPort();
ExchangeChannel channel = super.getExchangeChannel(remoteAddress);
if (channel == null) {
for (Map.Entry<URL, ExchangeClient> entry : clients.entrySet()) {
URL url = entry.getKey();
if (url.getIp().equals(host) && url.getPort() == port) {
return entry.getValue();
}
}
}
return channel;
}
示例9: connect
import com.alibaba.dubbo.remoting.exchange.ExchangeClient; //导入依赖的package包/类
protected Client connect(URL url) throws RemotingException {
if (servers.containsKey(url)) {
return null;
}
ExchangeClient client = clients.get(url);
if (client == null) { // TODO 有并发间隙
client = Exchangers.connect(url, dispatcher);
clients.put(url, client);
}
return client;
}
示例10: initClient
import com.alibaba.dubbo.remoting.exchange.ExchangeClient; //导入依赖的package包/类
public static ExchangeClient initClient(String url){
// 创建客户端
ExchangeClient exchangeClient = null;
PeformanceTestHandler handler = new PeformanceTestHandler(url);
boolean run = true;
while(run){
try{
exchangeClient= Exchangers.connect(url,handler);
} catch (Throwable t){
if(t!=null && t.getCause()!=null && t.getCause().getClass()!=null && (t.getCause().getClass()==java.net.ConnectException.class
|| t.getCause().getClass()== java.net.ConnectException.class)){
}else {
t.printStackTrace();
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (exchangeClient != null) {
run = false;
}
}
return exchangeClient;
}
示例11: ReferenceCountExchangeClient
import com.alibaba.dubbo.remoting.exchange.ExchangeClient; //导入依赖的package包/类
public ReferenceCountExchangeClient(ExchangeClient client, ConcurrentMap<String, LazyConnectExchangeClient> ghostClientMap) {
this.client = client;
refenceCount.incrementAndGet();
this.url = client.getUrl();
if (ghostClientMap == null){
throw new IllegalStateException("ghostClientMap can not be null, url: " + url);
}
this.ghostClientMap = ghostClientMap;
}
示例12: initClient
import com.alibaba.dubbo.remoting.exchange.ExchangeClient; //导入依赖的package包/类
/**
* 创建新连接.
*/
private ExchangeClient initClient(URL url) {
// client type setting.
String str = url.getParameter(Constants.CLIENT_KEY, url.getParameter(Constants.SERVER_KEY, Constants.DEFAULT_REMOTING_CLIENT));
String version = url.getParameter(Constants.DUBBO_VERSION_KEY);
boolean compatible = (version != null && version.startsWith("1.0."));
url = url.addParameter(Constants.CODEC_KEY, Version.isCompatibleVersion() && compatible ? COMPATIBLE_CODEC_NAME : DubboCodec.NAME);
//默认开启heartbeat
url = url.addParameterIfAbsent(Constants.HEARTBEAT_KEY, String.valueOf(Constants.DEFAULT_HEARTBEAT));
// BIO存在严重性能问题,暂时不允许使用
if (str != null && str.length() > 0 && ! ExtensionLoader.getExtensionLoader(Transporter.class).hasExtension(str)) {
throw new RpcException("Unsupported client type: " + str + "," +
" supported client type is " + StringUtils.join(ExtensionLoader.getExtensionLoader(Transporter.class).getSupportedExtensions(), " "));
}
ExchangeClient client ;
try {
//设置连接应该是lazy的
if (url.getParameter(Constants.LAZY_CONNECT_KEY, false)){
client = new LazyConnectExchangeClient(url ,requestHandler);
} else {
client = Exchangers.connect(url ,requestHandler);
}
} catch (RemotingException e) {
throw new RpcException("Fail to create remoting client for service(" + url
+ "): " + e.getMessage(), e);
}
return client;
}
示例13: isAvailable
import com.alibaba.dubbo.remoting.exchange.ExchangeClient; //导入依赖的package包/类
@Override
public boolean isAvailable() {
if (!super.isAvailable())
return false;
for (ExchangeClient client : clients){
if (client.isConnected() && !client.hasAttribute(Constants.CHANNEL_ATTRIBUTE_READONLY_KEY)){
//cannot write == not Available ?
return true ;
}
}
return false;
}
示例14: destroy
import com.alibaba.dubbo.remoting.exchange.ExchangeClient; //导入依赖的package包/类
public void destroy() {
//防止client被关闭多次.在connect per jvm的情况下,client.close方法会调用计数器-1,当计数器小于等于0的情况下,才真正关闭
if (super.isDestroyed()){
return ;
} else {
//dubbo check ,避免多次关闭
destroyLock.lock();
try{
if (super.isDestroyed()){
return ;
}
super.destroy();
if (invokers != null){
invokers.remove(this);
}
for (ExchangeClient client : clients) {
try {
client.close();
} catch (Throwable t) {
logger.warn(t.getMessage(), t);
}
}
}finally {
destroyLock.unlock();
}
}
}
示例15: getClients
import com.alibaba.dubbo.remoting.exchange.ExchangeClient; //导入依赖的package包/类
private ExchangeClient[] getClients(URL url){
int connections = url.getParameter(Constants.CONNECTIONS_KEY, 1);
ExchangeClient[] clients = new ExchangeClient[connections];
for (int i = 0; i < clients.length; i++) {
clients[i] = initClient(url);
}
return clients;
}