use of com.alipay.sofa.rpc.config.ProviderConfig in project sofa-rpc by sofastack.
the class NacosRegistryTest method testVirtualHostAndVirtualPort.
/**
* 测试NacosRegistry 对 serverconfig中 VirtualHost 和 VirtualPort 参数的支持情况
*
* @throws Exception the exception
*/
@Test
public void testVirtualHostAndVirtualPort() throws Exception {
// wait nacos startup ok
TimeUnit.SECONDS.sleep(10);
// 模拟的场景 client -> proxy:127.7.7.7:8888 -> netty:0.0.0.0:12200
String virtualHost = "127.7.7.7";
int virtualPort = 8888;
serverConfig = new ServerConfig().setProtocol("bolt").setHost("0.0.0.0").setPort(12200).setAdaptivePort(// Turn off adaptive port
false).setVirtualHost(virtualHost).setVirtualPort(virtualPort);
// Verify the influence of virtualHost and virtualPort on the parameters when creating rpcserver
Method m_resolveServerConfig = ServerFactory.class.getDeclaredMethod("resolveServerConfig", ServerConfig.class);
m_resolveServerConfig.setAccessible(true);
m_resolveServerConfig.invoke(new ServerFactory(), serverConfig);
Assert.assertNotEquals("boundhost should not be equal to virtualHost", serverConfig.getBoundHost(), virtualHost);
Assert.assertEquals("boundPort should be oriPort", serverConfig.getPort(), 12200);
ProviderConfig<?> provider = new ProviderConfig();
provider.setInterfaceId("com.alipay.xxx.NacosTestService2").setApplication(new ApplicationConfig().setAppName("test-server2")).setUniqueId("nacos-test2").setProxy("javassist").setRegister(true).setRegistry(registryConfig).setServer(serverConfig);
// 注册
registry.register(provider);
Thread.sleep(1000);
ConsumerConfig<?> consumer = new ConsumerConfig();
consumer.setInterfaceId("com.alipay.xxx.NacosTestService2").setApplication(new ApplicationConfig().setAppName("test-consumer2")).setUniqueId("nacos-test2").setProxy("javassist").setSubscribe(true).setSerialization("java").setInvokeType("sync").setTimeout(4444);
// 订阅
CountDownLatch latch = new CountDownLatch(1);
MockProviderInfoListener providerInfoListener = new MockProviderInfoListener();
providerInfoListener.setCountDownLatch(latch);
consumer.setProviderInfoListener(providerInfoListener);
List<ProviderGroup> all = registry.subscribe(consumer);
// providerInfoListener.updateAllProviders(all);
latch.await(2000, TimeUnit.MILLISECONDS);
Map<String, ProviderInfo> ps = providerInfoListener.getData();
Assert.assertEquals("after register: 1", 1, ps.size());
Map.Entry<String, ProviderInfo> psEntry = (Map.Entry) ps.entrySet().toArray()[0];
ProviderInfo pri = psEntry.getValue();
Assert.assertEquals("The provider's key should consist of virtualHost and virtualPort", psEntry.getKey(), virtualHost + ":" + virtualPort);
Assert.assertEquals("The provider's host should be virtualHost", virtualHost, pri.getHost());
Assert.assertEquals("The provider's port should be virtualPort", virtualPort, pri.getPort());
}
use of com.alipay.sofa.rpc.config.ProviderConfig in project sofa-rpc by sofastack.
the class ConsulRegistryTest method providerConfig.
private ProviderConfig<?> providerConfig(String uniqueId, int... ports) {
ProviderConfig<?> provider = new ProviderConfig();
provider.setInterfaceId(INTERFACE_ID).setUniqueId(uniqueId).setApplication(new ApplicationConfig().setAppName("consul-registry-test")).setProxy("javassist").setRegister(true).setRegistry(registryConfig).setSerialization("hessian2").setWeight(222).setTimeout(3000);
IntStream.of(ports).mapToObj(port -> new ServerConfig().setProtocol("bolt").setHost("localhost").setPort(port)).forEach(provider::setServer);
return provider;
}
use of com.alipay.sofa.rpc.config.ProviderConfig in project sofa-rpc by sofastack.
the class MeshRegistryTest method testAll.
@Test
public void testAll() throws Exception {
int timeoutPerSub = 1000;
ServerConfig serverConfig = new ServerConfig().setProtocol("bolt").setHost("0.0.0.0").setPort(12200);
ProviderConfig<?> provider = new ProviderConfig();
provider.setInterfaceId("com.alipay.xxx.TestService").setUniqueId("unique123Id").setApplication(new ApplicationConfig().setAppName("test-server")).setProxy("javassist").setRegister(true).setRegistry(registryConfig).setSerialization("hessian2").setServer(serverConfig).setWeight(222).setTimeout(3000);
// 注册
registry.register(provider);
ConsumerConfig<?> consumer = new ConsumerConfig();
consumer.setInterfaceId("com.alipay.xxx.TestService").setUniqueId("unique123Id").setApplication(new ApplicationConfig().setAppName("test-server")).setProxy("javassist").setSubscribe(true).setSerialization("java").setInvokeType("sync").setTimeout(4444);
String tag0 = MeshRegistryHelper.buildMeshKey(provider, serverConfig.getProtocol());
String tag1 = MeshRegistryHelper.buildMeshKey(consumer, consumer.getProtocol());
Assert.assertEquals(tag1, tag0);
// 订阅
MeshRegistryTest.MockProviderInfoListener providerInfoListener = new MeshRegistryTest.MockProviderInfoListener();
consumer.setProviderInfoListener(providerInfoListener);
List<ProviderGroup> groups = registry.subscribe(consumer);
Assert.assertNull(groups);
Thread.sleep(3000);
Map<String, ProviderGroup> ps = providerInfoListener.getData();
Assert.assertTrue(ps.toString(), ps.size() == 1);
// 反注册
CountDownLatch latch = new CountDownLatch(1);
providerInfoListener.setCountDownLatch(latch);
registry.unRegister(provider);
latch.await(timeoutPerSub, TimeUnit.MILLISECONDS);
// mesh 并不直接感知.
Assert.assertTrue(ps.size() == 1);
// 一次发2个端口的再次注册
latch = new CountDownLatch(1);
providerInfoListener.setCountDownLatch(latch);
provider.getServer().add(new ServerConfig().setProtocol("bolt").setHost("0.0.0.0").setPort(12201));
registry.register(provider);
latch.await(timeoutPerSub * 2, TimeUnit.MILLISECONDS);
// 重复订阅
ConsumerConfig<?> consumer2 = new ConsumerConfig();
consumer2.setInterfaceId("com.alipay.xxx.TestService").setUniqueId("unique123Id").setApplication(new ApplicationConfig().setAppName("test-server")).setProxy("javassist").setSubscribe(true).setSerialization("java").setInvokeType("sync").setTimeout(4444);
CountDownLatch latch2 = new CountDownLatch(1);
MeshRegistryTest.MockProviderInfoListener providerInfoListener2 = new MeshRegistryTest.MockProviderInfoListener();
providerInfoListener2.setCountDownLatch(latch2);
consumer2.setProviderInfoListener(providerInfoListener2);
List<ProviderGroup> groups2 = registry.subscribe(consumer2);
Assert.assertNull(groups);
Thread.sleep(3000);
Map<String, ProviderGroup> ps2 = providerInfoListener2.getData();
Assert.assertTrue(ps2.size() == 1);
// 取消订阅者1
registry.unSubscribe(consumer);
// 批量反注册,判断订阅者2的数据
latch = new CountDownLatch(1);
providerInfoListener2.setCountDownLatch(latch);
List<ProviderConfig> providerConfigList = new ArrayList<ProviderConfig>();
providerConfigList.add(provider);
registry.batchUnRegister(providerConfigList);
latch.await(timeoutPerSub, TimeUnit.MILLISECONDS);
Assert.assertTrue(ps2.size() == 1);
// 批量取消订阅
List<ConsumerConfig> consumerConfigList = new ArrayList<ConsumerConfig>();
consumerConfigList.add(consumer2);
registry.batchUnSubscribe(consumerConfigList);
}
use of com.alipay.sofa.rpc.config.ProviderConfig in project sofa-rpc by sofastack.
the class ProviderTracerFilter method invoke.
@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
SofaTracerSpan serverSpan = null;
try {
SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
serverSpan = sofaTraceContext.getCurrentSpan();
if (serverSpan != null) {
RpcInternalContext context = RpcInternalContext.getContext();
serverSpan.setTag(RpcSpanTags.SERVICE, request.getTargetServiceUniqueName());
serverSpan.setTag(RpcSpanTags.METHOD, request.getMethodName());
// 客户端地址
serverSpan.setTag(RpcSpanTags.REMOTE_IP, context.getRemoteHostName());
// 从请求里获取ConsumerTracerFilter额外传递的信息
serverSpan.setTag(RpcSpanTags.REMOTE_APP, (String) request.getRequestProp(HEAD_APP_NAME));
serverSpan.setTag(RpcSpanTags.PROTOCOL, (String) request.getRequestProp(HEAD_PROTOCOL));
serverSpan.setTag(RpcSpanTags.INVOKE_TYPE, (String) request.getRequestProp(HEAD_INVOKE_TYPE));
ProviderConfig providerConfig = (ProviderConfig) invoker.getConfig();
serverSpan.setTag(RpcSpanTags.LOCAL_APP, providerConfig.getAppName());
serverSpan.setTag(RpcSpanTags.SERVER_THREAD_POOL_WAIT_TIME, (Number) context.getAttachment(RpcConstants.INTERNAL_KEY_PROCESS_WAIT_TIME));
}
return invoker.invoke(request);
} finally {
if (serverSpan != null) {
serverSpan.setTag(RpcSpanTags.SERVER_BIZ_TIME, (Number) RpcInternalContext.getContext().getAttachment(RpcConstants.INTERNAL_KEY_IMPL_ELAPSE));
}
}
}
use of com.alipay.sofa.rpc.config.ProviderConfig in project sofa-boot by sofastack.
the class ProviderConfigContainer method exportAllDubboProvideConfig.
/**
* export所有 Dubbo 类型的 ProviderConfig
*/
public void exportAllDubboProvideConfig() {
for (ProviderConfig providerConfig : getAllProviderConfig()) {
ServerConfig serverConfig = (ServerConfig) providerConfig.getServer().get(0);
if (serverConfig.getProtocol().equalsIgnoreCase(SofaBootRpcConfigConstants.RPC_PROTOCOL_DUBBO)) {
providerConfig.setRegister(true);
providerConfig.export();
if (LOGGER.isInfoEnabled()) {
LOGGER.info("service published. interfaceId[" + providerConfig.getInterfaceId() + "]; protocol[" + serverConfig.getProtocol() + "]");
}
}
}
}
Aggregations