Search in sources :

Example 71 with ProviderConfig

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;
}
Also used : IntStream(java.util.stream.IntStream) ConsulStarterBuilder(com.pszymczyk.consul.ConsulStarterBuilder) HealthService(com.ecwid.consul.v1.health.model.HealthService) RegistryFactory(com.alipay.sofa.rpc.registry.RegistryFactory) ProviderGroup(com.alipay.sofa.rpc.client.ProviderGroup) After(org.junit.After) Map(java.util.Map) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) Before(org.junit.Before) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) ConsulClient(com.ecwid.consul.v1.ConsulClient) ProviderInfo(com.alipay.sofa.rpc.client.ProviderInfo) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.Test) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) ConsulProcess(com.pszymczyk.consul.ConsulProcess) RegistryConfig(com.alipay.sofa.rpc.config.RegistryConfig) Response(com.ecwid.consul.v1.Response) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ProviderInfoListener(com.alipay.sofa.rpc.listener.ProviderInfoListener) HealthServicesRequest(com.ecwid.consul.v1.health.HealthServicesRequest) Assert(org.junit.Assert) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig)

Example 72 with ProviderConfig

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);
}
Also used : ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) HelloService(com.alipay.sofa.rpc.test.HelloService) HelloServiceImpl(com.alipay.sofa.rpc.test.HelloServiceImpl) CountDownLatch(java.util.concurrent.CountDownLatch) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 73 with ProviderConfig

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);
    }
}
Also used : RegistryConfig(com.alipay.sofa.rpc.config.RegistryConfig) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) AddressHolder(com.alipay.sofa.rpc.client.AddressHolder) ProviderInfo(com.alipay.sofa.rpc.client.ProviderInfo) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) Test(org.junit.Test) BaseZkTest(com.alipay.sofa.rpc.registry.base.BaseZkTest)

Example 74 with ProviderConfig

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);
}
Also used : RegistryConfig(com.alipay.sofa.rpc.config.RegistryConfig) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) AddressHolder(com.alipay.sofa.rpc.client.AddressHolder) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) HelloService(com.alipay.sofa.rpc.test.HelloService) HelloServiceImpl(com.alipay.sofa.rpc.test.HelloServiceImpl) Test(org.junit.Test) BaseZkTest(com.alipay.sofa.rpc.registry.base.BaseZkTest)

Example 75 with ProviderConfig

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));
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) HttpGet(org.apache.http.client.methods.HttpGet) HttpOptions(org.apache.http.client.methods.HttpOptions) EchoRequest(com.alipay.sofa.rpc.server.bolt.pb.EchoRequest) HttpResponse(org.apache.http.HttpResponse) HttpHead(org.apache.http.client.methods.HttpHead) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) HttpClient(org.apache.http.client.HttpClient) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Aggregations

ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)121 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)88 Test (org.junit.Test)73 ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)52 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)47 HelloService (com.alipay.sofa.rpc.test.HelloService)33 HelloServiceImpl (com.alipay.sofa.rpc.test.HelloServiceImpl)33 ActivelyDestroyTest (com.alipay.sofa.rpc.test.ActivelyDestroyTest)28 RegistryConfig (com.alipay.sofa.rpc.config.RegistryConfig)21 CountDownLatch (java.util.concurrent.CountDownLatch)19 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)17 ProviderGroup (com.alipay.sofa.rpc.client.ProviderGroup)10 Filter (com.alipay.sofa.rpc.filter.Filter)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 EchoService (com.alipay.sofa.rpc.test.EchoService)9 ArrayList (java.util.ArrayList)9 ProviderInfo (com.alipay.sofa.rpc.client.ProviderInfo)8 EchoServiceImpl (com.alipay.sofa.rpc.test.EchoServiceImpl)8 MethodConfig (com.alipay.sofa.rpc.config.MethodConfig)7 RpcInvokeContext (com.alipay.sofa.rpc.context.RpcInvokeContext)7