Search in sources :

Example 71 with ApplicationConfig

use of com.alipay.sofa.rpc.config.ApplicationConfig in project sofa-rpc by sofastack.

the class MeshRegistryTest method testOnlyPublish.

@Test
public void testOnlyPublish() throws InterruptedException {
    Field registedAppField = null;
    try {
        registedAppField = MeshRegistry.class.getDeclaredField("registedApp");
        registedAppField.setAccessible(true);
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    }
    Boolean registedAppValue = null;
    // in case of effected by other case.
    try {
        registedAppValue = (Boolean) registedAppField.get(registry);
        registedAppField.set(registry, false);
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    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);
    Thread.sleep(3000);
    try {
        registedAppValue = (Boolean) registedAppField.get(registry);
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    LOGGER.info("final registedAppValue is " + registedAppValue);
    Assert.assertTrue(registedAppValue);
}
Also used : Field(java.lang.reflect.Field) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) Test(org.junit.Test)

Example 72 with ApplicationConfig

use of com.alipay.sofa.rpc.config.ApplicationConfig in project sofa-rpc by sofastack.

the class NacosRegistryHelperTest method convertProviderToInstances.

@Test
public void convertProviderToInstances() {
    ServerConfig serverConfig = new ServerConfig().setProtocol("bolt").setHost("0.0.0.0").setPort(12200);
    ProviderConfig<?> provider = new ProviderConfig();
    provider.setInterfaceId("com.alipay.xxx.TestService").setApplication(new ApplicationConfig().setAppName("test-server")).setUniqueId("nacos-test").setProxy("javassist").setRegister(true).setSerialization("hessian2").setServer(serverConfig).setWeight(222).setTimeout(3000);
    List<Instance> instances = NacosRegistryHelper.convertProviderToInstances(provider);
    assertNotNull(instances);
    assertEquals(1, instances.size());
    Instance instance = instances.get(0);
    assertNotNull(instance);
    assertEquals(NacosRegistryHelper.DEFAULT_CLUSTER, instance.getClusterName());
    assertEquals(serverConfig.getPort(), instance.getPort());
    assertEquals(serverConfig.getProtocol(), instance.getMetadata().get(RpcConstants.CONFIG_KEY_PROTOCOL));
    assertEquals(provider.getSerialization(), instance.getMetadata().get(RpcConstants.CONFIG_KEY_SERIALIZATION));
    assertEquals(provider.getUniqueId(), instance.getMetadata().get(RpcConstants.CONFIG_KEY_UNIQUEID));
    assertEquals(provider.getWeight(), Integer.parseInt(instance.getMetadata().get(RpcConstants.CONFIG_KEY_WEIGHT)));
    assertEquals(provider.getTimeout(), Integer.parseInt(instance.getMetadata().get(RpcConstants.CONFIG_KEY_TIMEOUT)));
    assertEquals(provider.getSerialization(), instance.getMetadata().get(RpcConstants.CONFIG_KEY_SERIALIZATION));
    assertEquals(provider.getAppName(), instance.getMetadata().get(RpcConstants.CONFIG_KEY_APP_NAME));
    assertEquals("com.alipay.xxx.TestService:nacos-test:DEFAULT", instance.getServiceName());
}
Also used : ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) Instance(com.alibaba.nacos.api.naming.pojo.Instance) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) Test(org.junit.Test)

Example 73 with ApplicationConfig

use of com.alipay.sofa.rpc.config.ApplicationConfig 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 74 with ApplicationConfig

use of com.alipay.sofa.rpc.config.ApplicationConfig in project sofa-rpc by sofastack.

the class BoltExceptionTest method testAll.

@Test
public void testAll() {
    final String directUrl = "bolt://127.0.0.1:12300";
    final ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setDirectUrl(directUrl).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setBootstrap("bolt").setApplication(new ApplicationConfig().setAppName("clientApp")).setReconnectPeriod(1000);
    HelloService helloService = consumerConfig.refer();
    // 关闭后再调用一个抛异常
    try {
        helloService.sayHello("xx", 22);
    } catch (Exception e) {
        // 应该抛出异常
        Assert.assertTrue(e instanceof SofaRouteException);
        Assert.assertTrue(e.getMessage().contains(directUrl));
    }
}
Also used : SofaRouteException(com.alipay.sofa.rpc.core.exception.SofaRouteException) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) HelloService(com.alipay.sofa.rpc.test.HelloService) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) SofaRouteException(com.alipay.sofa.rpc.core.exception.SofaRouteException) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest) Test(org.junit.Test)

Example 75 with ApplicationConfig

use of com.alipay.sofa.rpc.config.ApplicationConfig 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

ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)113 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)77 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)64 Test (org.junit.Test)53 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)52 HelloService (com.alipay.sofa.rpc.test.HelloService)30 ActivelyDestroyTest (com.alipay.sofa.rpc.test.ActivelyDestroyTest)21 CountDownLatch (java.util.concurrent.CountDownLatch)20 HelloServiceImpl (com.alipay.sofa.rpc.test.HelloServiceImpl)15 MethodConfig (com.alipay.sofa.rpc.config.MethodConfig)14 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)14 SofaResponseCallback (com.alipay.sofa.rpc.core.invoke.SofaResponseCallback)13 ArrayList (java.util.ArrayList)12 RegistryConfig (com.alipay.sofa.rpc.config.RegistryConfig)11 RequestBase (com.alipay.sofa.rpc.core.request.RequestBase)11 ProviderGroup (com.alipay.sofa.rpc.client.ProviderGroup)10 EchoService (com.alipay.sofa.rpc.test.EchoService)10 EchoRequest (com.alipay.sofa.rpc.server.bolt.pb.EchoRequest)8 DemoServiceImpl (com.alipay.sofa.rpc.bootstrap.dubbo.demo.DemoServiceImpl)7 Filter (com.alipay.sofa.rpc.filter.Filter)7