use of com.alipay.sofa.rpc.config.MethodConfig in project sofa-rpc by sofastack.
the class DubooServerTest method testFuture.
@Test
public // future调用,从future中取值.
void testFuture() {
// 只有1个线程 执行
ServerConfig serverConfig = new ServerConfig().setStopTimeout(60000).setPort(20880).setProtocol("dubbo").setQueues(100).setCoreThreads(1).setMaxThreads(2);
// 发布一个服务,每个请求要执行1秒
ApplicationConfig serverApplacation = new ApplicationConfig();
serverApplacation.setAppName("server");
providerConfig = new ProviderConfig<DemoService>().setInterfaceId(DemoService.class.getName()).setRef(new DemoServiceImpl()).setServer(serverConfig).setBootstrap("dubbo").setRegister(false).setApplication(serverApplacation);
providerConfig.export();
ApplicationConfig clientApplication = new ApplicationConfig();
clientApplication.setAppName("client");
List<MethodConfig> methodConfigs = new ArrayList<MethodConfig>();
MethodConfig methodConfig = new MethodConfig();
methodConfig.setInvokeType(RpcConstants.INVOKER_TYPE_FUTURE);
methodConfig.setName("sayHello");
consumerConfig = new ConsumerConfig<DemoService>().setInterfaceId(DemoService.class.getName()).setDirectUrl("dubbo://127.0.0.1:20880").setTimeout(30000).setRegister(false).setProtocol("dubbo").setBootstrap("dubbo").setApplication(clientApplication).setInvokeType(RpcConstants.INVOKER_TYPE_FUTURE).setMethods(methodConfigs);
final DemoService demoService = consumerConfig.refer();
String result = demoService.sayHello("xxx");
Assert.assertEquals(null, result);
Future<Object> future = RpcContext.getContext().getFuture();
String futureResult = null;
try {
futureResult = (String) future.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
Assert.assertEquals("Hello xxx", futureResult);
}
use of com.alipay.sofa.rpc.config.MethodConfig in project sofa-rpc by sofastack.
the class MockTestRegistry method convertProviderToUrls.
/**
* Convert provider to url.
*
* @param providerConfig the ProviderConfig
* @return the url list
*/
public static String convertProviderToUrls(ProviderConfig providerConfig, ServerConfig server) {
StringBuilder sb = new StringBuilder(200);
String appName = providerConfig.getAppName();
// 虚拟ip
String host = server.getVirtualHost();
if (host == null) {
host = server.getHost();
if (NetUtils.isLocalHost(host) || NetUtils.isAnyHost(host)) {
host = SystemInfo.getLocalHost();
}
} else {
if (LOGGER.isWarnEnabled(appName)) {
LOGGER.warnWithApp(appName, "Virtual host is specified, host will be change from {} to {} when register", server.getHost(), host);
}
}
// 虚拟port
Integer port = server.getVirtualPort();
if (port == null) {
port = server.getPort();
} else {
if (LOGGER.isWarnEnabled(appName)) {
LOGGER.warnWithApp(appName, "Virtual port is specified, host will be change from {} to {} when register", server.getPort(), port);
}
}
String protocol = server.getProtocol();
sb.append(host).append(":").append(port).append(server.getContextPath());
sb.append("?").append(ATTR_RPC_VERSION).append("=").append(Version.RPC_VERSION);
sb.append(getKeyPairs(ATTR_SERIALIZATION, providerConfig.getSerialization()));
sb.append(getKeyPairs(ATTR_WEIGHT, providerConfig.getWeight()));
if (providerConfig.getTimeout() > 0) {
sb.append(getKeyPairs(ATTR_TIMEOUT, providerConfig.getTimeout()));
}
sb.append(getKeyPairs(ATTR_APP_NAME, appName));
sb.append(getKeyPairs(ATTR_WARMUP_TIME, providerConfig.getParameter(ATTR_WARMUP_TIME.toString())));
sb.append(getKeyPairs(ATTR_WARMUP_WEIGHT, providerConfig.getParameter(ATTR_WARMUP_WEIGHT.toString())));
Map<String, MethodConfig> methodConfigs = providerConfig.getMethods();
if (CommonUtils.isNotEmpty(methodConfigs)) {
for (Map.Entry<String, MethodConfig> entry : methodConfigs.entrySet()) {
String methodName = entry.getKey();
MethodConfig methodConfig = entry.getValue();
sb.append(getKeyPairs("." + methodName + "." + ATTR_TIMEOUT, methodConfig.getTimeout()));
// 方法级配置,只能放timeout
String key = "[" + methodName + "]";
String value = "[clientTimeout" + "#" + methodConfig.getTimeout() + "]";
sb.append(getKeyPairs(key, value));
}
}
sb.append(getKeyPairs(ATTR_START_TIME, RpcRuntimeContext.now()));
return sb.toString();
}
use of com.alipay.sofa.rpc.config.MethodConfig in project sofa-rpc by sofastack.
the class BaggageFutureTest method doTest.
@Override
public void doTest() throws Exception {
ServerConfig serverConfig = new ServerConfig().setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setPort(12299);
// C服务的服务端
CSampleServiceImpl refC = new CSampleServiceImpl();
ProviderConfig<SampleService> serviceBeanC = new ProviderConfig<SampleService>();
serviceBeanC.setInterfaceId(SampleService.class.getName());
serviceBeanC.setApplication(new ApplicationConfig().setAppName("CCC"));
serviceBeanC.setUniqueId("C2");
serviceBeanC.setRef(refC);
serviceBeanC.setServer(serverConfig);
serviceBeanC.setRegister(false);
serviceBeanC.export();
// D服务的服务端
DSampleServiceImpl refD = new DSampleServiceImpl();
ProviderConfig<SampleService> serviceBeanD = new ProviderConfig<SampleService>();
serviceBeanD.setInterfaceId(SampleService.class.getName());
serviceBeanD.setApplication(new ApplicationConfig().setAppName("DDD"));
serviceBeanD.setUniqueId("D2");
serviceBeanD.setRef(refD);
serviceBeanD.setServer(serverConfig);
serviceBeanD.setRegister(false);
serviceBeanD.export();
// B服务里的C服务客户端
ConsumerConfig referenceBeanC = new ConsumerConfig();
referenceBeanC.setApplication(new ApplicationConfig().setAppName("BBB"));
referenceBeanC.setInterfaceId(SampleService.class.getName());
referenceBeanC.setUniqueId("C2");
referenceBeanC.setDirectUrl("localhost:12299");
referenceBeanC.setTimeout(1000);
MethodConfig methodConfigC = new MethodConfig().setName("hello").setInvokeType(RpcConstants.INVOKER_TYPE_FUTURE);
referenceBeanC.setMethods(Collections.singletonList(methodConfigC));
SampleService sampleServiceC = (SampleService) referenceBeanC.refer();
// B服务里的D服务客户端
ConsumerConfig referenceBeanD = new ConsumerConfig();
referenceBeanD.setApplication(new ApplicationConfig().setAppName("BBB"));
referenceBeanD.setInterfaceId(SampleService.class.getName());
referenceBeanD.setUniqueId("D2");
referenceBeanD.setDirectUrl("localhost:12299?p=1&v=4.0");
referenceBeanD.setTimeout(1000);
MethodConfig methodConfigD = new MethodConfig().setName("hello").setInvokeType(RpcConstants.INVOKER_TYPE_FUTURE);
referenceBeanD.setMethods(Collections.singletonList(methodConfigD));
SampleService sampleServiceD = (SampleService) referenceBeanD.refer();
// B服务的服务端
BFutureSampleServiceImpl refB = new BFutureSampleServiceImpl(sampleServiceC, sampleServiceD);
ProviderConfig<SampleService> ServiceBeanB = new ProviderConfig<SampleService>();
ServiceBeanB.setInterfaceId(SampleService.class.getName());
ServiceBeanB.setApplication(new ApplicationConfig().setAppName("BBB"));
ServiceBeanB.setUniqueId("B2");
ServiceBeanB.setRef(refB);
ServiceBeanB.setServer(serverConfig);
ServiceBeanB.setRegister(false);
ServiceBeanB.export();
// A 服务
ConsumerConfig referenceBeanA = new ConsumerConfig();
referenceBeanA.setApplication(new ApplicationConfig().setAppName("AAA"));
referenceBeanA.setUniqueId("B2");
referenceBeanA.setInterfaceId(SampleService.class.getName());
referenceBeanA.setDirectUrl("localhost:12299");
referenceBeanA.setTimeout(3000);
MethodConfig methodConfigA = new MethodConfig().setName("hello").setInvokeType(RpcConstants.INVOKER_TYPE_FUTURE);
referenceBeanA.setMethods(Collections.singletonList(methodConfigA));
SampleService service = (SampleService) referenceBeanA.refer();
// 开始测试
RpcInvokeContext context = RpcInvokeContext.getContext();
context.putRequestBaggage("reqBaggageB", "a2bbb");
context.putRequestBaggage("reqBaggageC", "a2ccc");
context.putRequestBaggage("reqBaggageD", "a2ddd");
String ret = service.hello();
Assert.assertEquals(ret, null);
ret = (String) SofaResponseFuture.getResponse(5000, false);
Assert.assertEquals(ret, "hello world chello world d");
Assert.assertEquals(refB.getReqBaggage(), "a2bbb");
Assert.assertEquals(refC.getReqBaggage(), "a2ccc");
Assert.assertEquals(refD.getReqBaggage(), "a2ddd");
Assert.assertEquals(context.getResponseBaggage("respBaggageB"), "b2aaa");
Assert.assertEquals(context.getResponseBaggage("respBaggageC"), "c2aaa");
Assert.assertEquals(context.getResponseBaggage("respBaggageD"), "d2aaa");
Assert.assertNull(context.getResponseBaggage("respBaggageB_force"));
Assert.assertNull(context.getResponseBaggage("respBaggageC_force"));
Assert.assertNull(context.getResponseBaggage("respBaggageD_force"));
RpcInvokeContext.removeContext();
context = RpcInvokeContext.getContext();
ret = null;
ret = service.hello();
Assert.assertEquals(ret, null);
ret = (String) SofaResponseFuture.getResponse(5000, false);
Assert.assertEquals(ret, "hello world chello world d");
Assert.assertNull(refB.getReqBaggage());
Assert.assertNull(refC.getReqBaggage());
Assert.assertNull(refD.getReqBaggage());
Assert.assertNull(context.getResponseBaggage("respBaggageB"));
Assert.assertNull(context.getResponseBaggage("respBaggageC"));
Assert.assertNull(context.getResponseBaggage("respBaggageD"));
Assert.assertEquals(context.getResponseBaggage("respBaggageB_force"), "b2aaaff");
Assert.assertEquals(context.getResponseBaggage("respBaggageC_force"), "c2aaaff");
Assert.assertEquals(context.getResponseBaggage("respBaggageD_force"), "d2aaaff");
}
use of com.alipay.sofa.rpc.config.MethodConfig in project sofa-rpc by sofastack.
the class BaggageCallbackTest method doTest.
@Override
public void doTest() throws Exception {
ServerConfig serverConfig = new ServerConfig().setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setPort(12299);
// C服务的服务端
CSampleServiceImpl refC = new CSampleServiceImpl();
ProviderConfig<SampleService> serviceBeanC = new ProviderConfig<SampleService>();
serviceBeanC.setInterfaceId(SampleService.class.getName());
serviceBeanC.setApplication(new ApplicationConfig().setAppName("CCC"));
serviceBeanC.setUniqueId("C3");
serviceBeanC.setRef(refC);
serviceBeanC.setServer(serverConfig);
serviceBeanC.setRegister(false);
serviceBeanC.export();
// D服务的服务端
DSampleServiceImpl refD = new DSampleServiceImpl();
ProviderConfig<SampleService> serviceBeanD = new ProviderConfig<SampleService>();
serviceBeanD.setInterfaceId(SampleService.class.getName());
serviceBeanD.setApplication(new ApplicationConfig().setAppName("DDD"));
serviceBeanD.setUniqueId("D3");
serviceBeanD.setRef(refD);
serviceBeanD.setServer(serverConfig);
serviceBeanD.setRegister(false);
serviceBeanD.export();
// B服务里的C服务客户端
ConsumerConfig referenceBeanC = new ConsumerConfig();
referenceBeanC.setApplication(new ApplicationConfig().setAppName("BBB"));
referenceBeanC.setInterfaceId(SampleService.class.getName());
referenceBeanC.setUniqueId("C3");
referenceBeanC.setDirectUrl("localhost:12299");
referenceBeanC.setTimeout(1000);
MethodConfig methodConfigC = new MethodConfig().setName("hello").setInvokeType(RpcConstants.INVOKER_TYPE_CALLBACK);
referenceBeanC.setMethods(Collections.singletonList(methodConfigC));
SampleService sampleServiceC = (SampleService) referenceBeanC.refer();
// B服务里的D服务客户端
ConsumerConfig referenceBeanD = new ConsumerConfig();
referenceBeanD.setApplication(new ApplicationConfig().setAppName("BBB"));
referenceBeanD.setInterfaceId(SampleService.class.getName());
referenceBeanD.setUniqueId("D3");
referenceBeanD.setDirectUrl("localhost:12299?p=1&v=4.0");
referenceBeanD.setTimeout(1000);
MethodConfig methodConfigD = new MethodConfig().setName("hello").setInvokeType(RpcConstants.INVOKER_TYPE_CALLBACK);
referenceBeanD.setMethods(Collections.singletonList(methodConfigD));
SampleService sampleServiceD = (SampleService) referenceBeanD.refer();
// B服务的服务端
BCallbackSampleServiceImpl refB = new BCallbackSampleServiceImpl(sampleServiceC, sampleServiceD);
ProviderConfig<SampleService> ServiceBeanB = new ProviderConfig<SampleService>();
ServiceBeanB.setInterfaceId(SampleService.class.getName());
ServiceBeanB.setApplication(new ApplicationConfig().setAppName("BBB"));
ServiceBeanB.setUniqueId("B3");
ServiceBeanB.setRef(refB);
ServiceBeanB.setServer(serverConfig);
ServiceBeanB.setRegister(false);
ServiceBeanB.export();
// A 服务
final String[] str = new String[1];
final CountDownLatch[] latch = new CountDownLatch[] { new CountDownLatch(1) };
final RpcInvokeContext[] contexts = new RpcInvokeContext[1];
ConsumerConfig referenceBeanA = new ConsumerConfig();
referenceBeanA.setApplication(new ApplicationConfig().setAppName("AAA"));
referenceBeanA.setUniqueId("B3");
referenceBeanA.setInterfaceId(SampleService.class.getName());
referenceBeanA.setDirectUrl("localhost:12299");
referenceBeanA.setTimeout(3000);
MethodConfig methodConfigA = new MethodConfig().setName("hello").setInvokeType(RpcConstants.INVOKER_TYPE_CALLBACK);
methodConfigA.setOnReturn(new SofaResponseCallback() {
@Override
public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
Assert.assertNotSame(RpcInvokeContext.getContext(), contexts[0]);
str[0] = (String) appResponse;
latch[0].countDown();
}
@Override
public void onAppException(Throwable t, String methodName, RequestBase request) {
assertEquals("sampleService", t.getMessage());
assertEquals("sayException", methodName);
}
@Override
public void onSofaException(SofaRpcException sofaException, String methodName, RequestBase request) {
// never go to this
assertEquals("sampleService", sofaException.getMessage());
assertEquals("sayException", methodName);
}
});
referenceBeanA.setMethods(Collections.singletonList(methodConfigA));
SampleService service = (SampleService) referenceBeanA.refer();
// 开始测试
RpcInvokeContext context = RpcInvokeContext.getContext();
contexts[0] = context;
context.putRequestBaggage("reqBaggageB", "a2bbb");
context.putRequestBaggage("reqBaggageC", "a2ccc");
context.putRequestBaggage("reqBaggageD", "a2ddd");
String ret = service.hello();
Assert.assertEquals(ret, null);
latch[0].await(5000, TimeUnit.MILLISECONDS);
ret = str[0];
Assert.assertEquals(ret, "hello world chello world d");
Assert.assertEquals(refB.getReqBaggage(), "a2bbb");
Assert.assertEquals(refC.getReqBaggage(), "a2ccc");
Assert.assertEquals(refD.getReqBaggage(), "a2ddd");
Assert.assertEquals(context.getResponseBaggage("respBaggageB"), "b2aaa");
Assert.assertEquals(context.getResponseBaggage("respBaggageC"), "c2aaa");
Assert.assertEquals(context.getResponseBaggage("respBaggageD"), "d2aaa");
Assert.assertNull(context.getResponseBaggage("respBaggageB_force"));
Assert.assertNull(context.getResponseBaggage("respBaggageC_force"));
Assert.assertNull(context.getResponseBaggage("respBaggageD_force"));
RpcInvokeContext.removeContext();
context = RpcInvokeContext.getContext();
contexts[0] = context;
latch[0] = new CountDownLatch(1);
str[0] = null;
ret = null;
ret = service.hello();
Assert.assertEquals(ret, null);
latch[0].await(5000, TimeUnit.MILLISECONDS);
ret = str[0];
Assert.assertEquals(ret, "hello world chello world d");
Assert.assertNull(refB.getReqBaggage());
Assert.assertNull(refC.getReqBaggage());
Assert.assertNull(refD.getReqBaggage());
Assert.assertNull(context.getResponseBaggage("respBaggageB"));
Assert.assertNull(context.getResponseBaggage("respBaggageC"));
Assert.assertNull(context.getResponseBaggage("respBaggageD"));
Assert.assertEquals(context.getResponseBaggage("respBaggageB_force"), "b2aaaff");
Assert.assertEquals(context.getResponseBaggage("respBaggageC_force"), "c2aaaff");
Assert.assertEquals(context.getResponseBaggage("respBaggageD_force"), "d2aaaff");
}
use of com.alipay.sofa.rpc.config.MethodConfig in project sofa-rpc by sofastack.
the class SofaRegistryHelper method convertProviderToUrls.
/**
* Convert provider to url.
*
* @param providerConfig the ProviderConfig
* @return the url list
*/
public static String convertProviderToUrls(ProviderConfig providerConfig, ServerConfig server) {
StringBuilder sb = new StringBuilder(200);
String appName = providerConfig.getAppName();
// 虚拟ip
String host = server.getVirtualHost();
if (host == null) {
host = server.getHost();
if (NetUtils.isLocalHost(host) || NetUtils.isAnyHost(host)) {
host = SystemInfo.getLocalHost();
}
} else {
if (LOGGER.isWarnEnabled(appName)) {
LOGGER.warnWithApp(appName, "Virtual host is specified, host will be change from {} to {} when register", server.getHost(), host);
}
}
// 虚拟port
Integer port = server.getVirtualPort();
if (port == null) {
port = server.getPort();
} else {
if (LOGGER.isWarnEnabled(appName)) {
LOGGER.warnWithApp(appName, "Virtual port is specified, host will be change from {} to {} when register", server.getPort(), port);
}
}
String protocol = server.getProtocol();
sb.append(host).append(":").append(port).append(server.getContextPath());
// .append(providerConfig.getInterfaceId())
sb.append("?").append(ATTR_RPC_VERSION).append("=").append(Version.RPC_VERSION);
sb.append(getKeyPairs(ATTR_SERIALIZATION, providerConfig.getSerialization()));
sb.append(getKeyPairs(ATTR_WEIGHT, providerConfig.getWeight()));
if (providerConfig.getTimeout() > 0) {
sb.append(getKeyPairs(ATTR_TIMEOUT, providerConfig.getTimeout()));
}
sb.append(getKeyPairs(ATTR_APP_NAME, appName));
// 兼容老系统,代码是否剥离?
if (PROTOCOL_TYPE_BOLT.equals(protocol)) {
// p=1
sb.append(getKeyPairs(RPC_REMOTING_PROTOCOL, RemotingConstants.PROTOCOL_BOLT));
} else if (PROTOCOL_TYPE_TR.equals(protocol)) {
// p=13
sb.append(getKeyPairs(RPC_REMOTING_PROTOCOL, RemotingConstants.PROTOCOL_TR));
}
// v=4.0
sb.append(getKeyPairs(RPC_SERVICE_VERSION, SOFA4_RPC_SERVICE_VERSION));
// _SERIALIZETYPE=xx
sb.append(getKeyPairs(SERIALIZE_TYPE_KEY, providerConfig.getSerialization()));
// _WEIGHT=100
sb.append(getKeyPairs(WEIGHT_KEY, providerConfig.getWeight()));
if (providerConfig.getTimeout() > 0) {
// _TIMEOUT=3000
sb.append(getKeyPairs(TIMEOUT, providerConfig.getTimeout()));
}
sb.append(getKeyPairs(APP_NAME, appName));
if (StringUtils.isNotBlank(SystemInfo.getHostMachine())) {
sb.append(getKeyPairs(HOST_MACHINE_KEY, SystemInfo.getHostMachine()));
}
Map<String, MethodConfig> methodConfigs = providerConfig.getMethods();
if (CommonUtils.isNotEmpty(methodConfigs)) {
for (Map.Entry<String, MethodConfig> entry : methodConfigs.entrySet()) {
String methodName = entry.getKey();
MethodConfig methodConfig = entry.getValue();
sb.append(getKeyPairs("." + methodName + "." + ATTR_TIMEOUT, methodConfig.getTimeout()));
// 方法级配置,只能放timeout
String key = "[" + methodName + "]";
String value = "[" + KEY_TIMEOUT + "#" + methodConfig.getTimeout() + "]";
sb.append(getKeyPairs(key, value));
}
}
sb.append(convertMap2Pair(providerConfig.getParameters()));
addCommonAttrs(sb);
return sb.toString();
}
Aggregations