use of com.alipay.sofa.rpc.config.ProviderConfig in project sofa-rpc by sofastack.
the class ConsulRegistryAclTest 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 DiscardTimeoutTest method testAll.
@Test
public void testAll() {
ServerConfig serverConfig = new ServerConfig().setStopTimeout(0).setPort(22222).setQueues(5).setCoreThreads(1).setMaxThreads(1);
// 发布一个服务,每个请求要执行2秒
ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl(2000)).setServer(serverConfig).setRegister(false);
providerConfig.export();
ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setTimeout(5000).setDirectUrl("bolt://127.0.0.1:22222").setRegister(false);
final HelloService helloService = consumerConfig.refer();
final AtomicInteger success = new AtomicInteger();
final AtomicInteger failure = new AtomicInteger();
int times = 4;
final CountDownLatch latch = new CountDownLatch(times);
for (int i = 0; i < times; i++) {
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
try {
helloService.sayHello("xxx", 22);
success.incrementAndGet();
} catch (Exception e) {
failure.incrementAndGet();
Assert.assertTrue(e instanceof SofaRpcException);
Assert.assertTrue(((SofaRpcException) e).getErrorType() == RpcErrorType.CLIENT_TIMEOUT);
} finally {
latch.countDown();
}
}
}, "T1");
thread1.start();
}
try {
// 此时客户端提前抛出超时异常
latch.await(10000, TimeUnit.MILLISECONDS);
} catch (InterruptedException ignore) {
}
// 2秒1个 超时3秒 超时3个
// 第一个请求正常
// 第二个请求返回时超时
// 第三个请求返回时超时
// 第四个请求已超时
Assert.assertEquals(success.get(), 2);
Assert.assertEquals(failure.get(), 2);
}
use of com.alipay.sofa.rpc.config.ProviderConfig in project sofa-rpc by sofastack.
the class ZookeeperOverrideObserverTest method testAll.
@Test
public void testAll() throws Exception {
try {
RegistryConfig registryConfig = new RegistryConfig().setProtocol(RpcConstants.REGISTRY_PROTOCOL_ZK).setAddress("127.0.0.1:2181");
ZookeeperRegistry registry = (ZookeeperRegistry) RegistryFactory.getRegistry(registryConfig);
registry.start();
ServerConfig serverConfig = new ServerConfig().setPort(22222).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT);
ProviderConfig<OverrideService> providerConfig = new ProviderConfig<OverrideService>().setInterfaceId(OverrideService.class.getName()).setRef(new OverrideServiceImpl(22222)).setServer(serverConfig).setRegistry(registryConfig).setParameter(ProviderInfoAttrs.ATTR_WARMUP_TIME, "2000").setParameter(ProviderInfoAttrs.ATTR_WARMUP_WEIGHT, "100").setWeight(0);
ServerConfig serverConfig2 = new ServerConfig().setPort(22111).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT);
ProviderConfig<OverrideService> providerConfig2 = new ProviderConfig<OverrideService>().setInterfaceId(OverrideService.class.getName()).setRef(new OverrideServiceImpl(22111)).setServer(serverConfig2).setRegistry(registryConfig).setRepeatedExportLimit(-1).setWeight(0);
providerConfig.export();
providerConfig2.export();
ConsumerConfig<OverrideService> consumerConfig = new ConsumerConfig<OverrideService>().setInterfaceId(OverrideService.class.getName()).setRegistry(registryConfig).setTimeout(3333).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT);
OverrideService overrideService = consumerConfig.refer();
AddressHolder addressHolder = consumerConfig.getConsumerBootstrap().getCluster().getAddressHolder();
Assert.assertTrue(addressHolder.getAllProviderSize() == 2);
providerConfig2.unExport();
Assert.assertTrue(delayGetSize(addressHolder, 1, 100) == 1);
List<String> path = registry.getZkClient().getChildren().forPath("/sofa-rpc/" + OverrideService.class.getCanonicalName() + "/providers");
String url = URLDecoder.decode(path.get(0), "UTF-8");
ProviderInfo providerInfo = ProviderHelper.toProviderInfo(url);
// 模拟下发一个override
String override1 = providerInfo.getProtocolType() + "://" + providerInfo.getHost() + ":" + providerInfo.getPort() + "?timeout=2345";
String overridePath1 = "/sofa-rpc/" + OverrideService.class.getCanonicalName() + "/overrides/" + URLEncoder.encode(override1, "UTF-8");
registry.getZkClient().create().creatingParentContainersIfNeeded().withMode(CreateMode.PERSISTENT).forPath(overridePath1);
Assert.assertTrue(delayGetTimeout(consumerConfig, 2345, 100) == 2345);
// 删除目前没有影响
registry.getZkClient().delete().forPath(overridePath1);
Thread.sleep(500);
Assert.assertTrue(delayGetTimeout(consumerConfig, 2345, 100) == 2345);
// 恢复到3333
String override2 = providerInfo.getProtocolType() + "://" + providerInfo.getHost() + ":" + providerInfo.getPort() + "?timeout=3333";
String overridePath2 = "/sofa-rpc/" + OverrideService.class.getCanonicalName() + "/overrides/" + URLEncoder.encode(override2, "UTF-8");
registry.getZkClient().create().creatingParentContainersIfNeeded().withMode(CreateMode.PERSISTENT).forPath(overridePath2);
Assert.assertTrue(delayGetTimeout(consumerConfig, 3333, 100) == 3333);
// 清除持久化的 path
registry.getZkClient().delete().forPath(overridePath2);
} catch (Throwable e) {
LOGGER.error("ZookeeperOverrideObserver test case failed", e);
Assert.assertTrue(e.getMessage(), false);
}
}
use of com.alipay.sofa.rpc.config.ProviderConfig in project sofa-rpc by sofastack.
the class ZookeeperSubscribeObserverTest method testAll.
@Test
public void testAll() throws Exception {
RegistryConfig registryConfig = new RegistryConfig().setProtocol(RpcConstants.REGISTRY_PROTOCOL_ZK).setAddress("127.0.0.1:2181");
ZookeeperRegistry registry = (ZookeeperRegistry) RegistryFactory.getRegistry(registryConfig);
registry.start();
ServerConfig serverConfig = new ServerConfig().setPort(22222).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT);
ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl(22222)).setServer(serverConfig).setRegistry(registryConfig).setParameter(ProviderInfoAttrs.ATTR_WARMUP_TIME, "2000").setParameter(ProviderInfoAttrs.ATTR_WARMUP_WEIGHT, "100").setUniqueId("uniqueIdA").setRepeatedExportLimit(-1).setWeight(0);
ServerConfig serverConfig2 = new ServerConfig().setPort(22111).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT);
ProviderConfig<HelloService> providerConfig2 = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl(22111)).setServer(serverConfig2).setRegistry(registryConfig).setUniqueId("uniqueIdB").setRepeatedExportLimit(-1).setWeight(0);
providerConfig.export();
providerConfig2.export();
ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRegistry(registryConfig).setTimeout(3333).setUniqueId("uniqueIdA").setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT);
HelloService helloService = consumerConfig.refer();
AddressHolder addressHolder = consumerConfig.getConsumerBootstrap().getCluster().getAddressHolder();
Assert.assertTrue(addressHolder.getAllProviderSize() == 1);
ServerConfig serverConfig3 = new ServerConfig().setPort(22133).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT);
ProviderConfig<HelloService> providerConfig3 = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl(22133)).setServer(serverConfig3).setRegistry(registryConfig).setRepeatedExportLimit(-1).setUniqueId("uniqueIdB").setRepeatedExportLimit(-1).setWeight(0);
providerConfig3.export();
Assert.assertTrue(delayGetSize(addressHolder, 1, 100) == 1);
ServerConfig serverConfig4 = new ServerConfig().setPort(22244).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT);
ProviderConfig<HelloService> providerConfig4 = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl(22244)).setServer(serverConfig4).setRegistry(registryConfig).setUniqueId("uniqueIdA").setRepeatedExportLimit(-1).setWeight(0);
providerConfig4.export();
Assert.assertTrue(delayGetSize(addressHolder, 1, 100) == 2);
}
use of com.alipay.sofa.rpc.config.ProviderConfig in project sofa-rpc by sofastack.
the class Http1ServerTest method testHttp1General.
@Test
public void testHttp1General() throws Exception {
// 只有1个线程 执行
ServerConfig serverConfig = new ServerConfig().setStopTimeout(60000).setPort(12300).setProtocol(RpcConstants.PROTOCOL_TYPE_HTTP).setDaemon(true);
// 发布一个服务,每个请求要执行1秒
ProviderConfig<HttpService> providerConfig = new ProviderConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setRef(new HttpServiceImpl()).setApplication(new ApplicationConfig().setAppName("serverApp")).setServer(serverConfig).setUniqueId("uuu").setRegister(false);
providerConfig.export();
HttpClient httpclient = HttpClientBuilder.create().build();
{
// GET 图标
String url = "http://127.0.0.1:12300/favicon.ico";
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpclient.execute(httpGet);
Assert.assertEquals(200, httpResponse.getStatusLine().getStatusCode());
Assert.assertTrue(StringUtils.isEmpty(getStringContent(httpResponse)));
}
{
// 其它未知命令
String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService/add";
HttpOptions httpGet = new HttpOptions(url);
HttpResponse httpResponse = httpclient.execute(httpGet);
Assert.assertEquals(400, httpResponse.getStatusLine().getStatusCode());
Assert.assertTrue(StringUtils.isNotEmpty(getStringContent(httpResponse)));
}
{
// HEAD 不存在的服务
String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService/add";
HttpHead httpHead = new HttpHead(url);
HttpResponse httpResponse = httpclient.execute(httpHead);
Assert.assertEquals(404, httpResponse.getStatusLine().getStatusCode());
}
{
// HEAD 不存在的方法
String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService:uuu/xasdasdasd";
HttpHead httpHead = new HttpHead(url);
HttpResponse httpResponse = httpclient.execute(httpHead);
Assert.assertEquals(404, httpResponse.getStatusLine().getStatusCode());
}
{
// HEAD 存在的方法
String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService:uuu/add";
HttpHead httpHead = new HttpHead(url);
HttpResponse httpResponse = httpclient.execute(httpHead);
Assert.assertEquals(200, httpResponse.getStatusLine().getStatusCode());
}
{
// GET 异常:地址不对
String url = "http://127.0.0.1:12300/com.alipay.sofa";
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpclient.execute(httpGet);
Assert.assertEquals(400, httpResponse.getStatusLine().getStatusCode());
Assert.assertNotNull(getStringContent(httpResponse));
}
{
// GET 不存在的接口
String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService/asdasdas?code=xxx";
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpclient.execute(httpGet);
Assert.assertEquals(404, httpResponse.getStatusLine().getStatusCode());
Assert.assertTrue(getStringContent(httpResponse).contains("asdasdas"));
}
{
// GET 不存在的方法
String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService:uuu/asdasdas?code=xxx";
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpclient.execute(httpGet);
Assert.assertEquals(404, httpResponse.getStatusLine().getStatusCode());
Assert.assertTrue(getStringContent(httpResponse).contains("asdasdas"));
}
{
// GET 异常:参数数量不对
String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService:uuu/query";
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpclient.execute(httpGet);
Assert.assertEquals(400, httpResponse.getStatusLine().getStatusCode());
Assert.assertNotNull(getStringContent(httpResponse));
}
{
// GET 异常:参数数量不对
String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService:uuu/add?code=1";
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpclient.execute(httpGet);
Assert.assertEquals(400, httpResponse.getStatusLine().getStatusCode());
Assert.assertNotNull(getStringContent(httpResponse));
}
{
// GET 异常:参数类型不对
String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService:uuu/query?code=xxx";
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpclient.execute(httpGet);
Assert.assertEquals(400, httpResponse.getStatusLine().getStatusCode());
Assert.assertNotNull(getStringContent(httpResponse));
}
{
// GET 正确
String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService:uuu/add?code=1&name=22";
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpclient.execute(httpGet);
Assert.assertEquals(200, httpResponse.getStatusLine().getStatusCode());
Assert.assertEquals("221", getStringContent(httpResponse));
}
{
// POST 未知序列化
String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService/adasdad";
HttpPost httpPost = new HttpPost(url);
EchoRequest request = EchoRequest.newBuilder().setGroup(Group.A).setName("xxx").build();
ByteArrayEntity entity = new ByteArrayEntity(request.toByteArray(), null);
httpPost.setEntity(entity);
HttpResponse httpResponse = httpclient.execute(httpPost);
Assert.assertEquals(400, httpResponse.getStatusLine().getStatusCode());
Assert.assertNotNull(getStringContent(httpResponse));
}
}
Aggregations