Search in sources :

Example 41 with ApplicationConfig

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

the class Http1ServerTest method testHttp1Json.

@Test
public void testHttp1Json() 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();
    {
        // POST jackson不存在的接口
        String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService/adasdad";
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader(RemotingConstants.HEAD_SERIALIZE_TYPE, "json");
        ExampleObj obj = new ExampleObj();
        obj.setId(1);
        obj.setName("xxx");
        byte[] bytes = mapper.writeValueAsBytes(obj);
        ByteArrayEntity entity = new ByteArrayEntity(bytes, ContentType.create("application/json"));
        httpPost.setEntity(entity);
        HttpResponse httpResponse = httpclient.execute(httpPost);
        Assert.assertEquals(404, httpResponse.getStatusLine().getStatusCode());
        Assert.assertNotNull(getStringContent(httpResponse));
    }
    {
        // POST 不存在的方法
        String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService:uuu/adasdad";
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader(RemotingConstants.HEAD_SERIALIZE_TYPE, "json");
        ExampleObj obj = new ExampleObj();
        obj.setId(1);
        obj.setName("xxx");
        byte[] bytes = mapper.writeValueAsBytes(obj);
        ByteArrayEntity entity = new ByteArrayEntity(bytes, ContentType.create("application/json"));
        httpPost.setEntity(entity);
        HttpResponse httpResponse = httpclient.execute(httpPost);
        Assert.assertEquals(404, httpResponse.getStatusLine().getStatusCode());
        Assert.assertNotNull(getStringContent(httpResponse));
    }
    {
        // POST 不传 HEAD_SERIALIZE_TYPE
        String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService:uuu/object";
        HttpPost httpPost = new HttpPost(url);
        ExampleObj obj = new ExampleObj();
        obj.setId(1);
        obj.setName("xxx");
        byte[] bytes = mapper.writeValueAsBytes(obj);
        ByteArrayEntity entity = new ByteArrayEntity(bytes, ContentType.create("application/json"));
        httpPost.setEntity(entity);
        HttpResponse httpResponse = httpclient.execute(httpPost);
        Assert.assertEquals(200, httpResponse.getStatusLine().getStatusCode());
        byte[] data = EntityUtils.toByteArray(httpResponse.getEntity());
        ExampleObj result = mapper.readValue(data, ExampleObj.class);
        Assert.assertEquals("xxxxx", result.getName());
    }
    {
        // POST 正常请求
        String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService:uuu/object";
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader(RemotingConstants.HEAD_SERIALIZE_TYPE, "json");
        ExampleObj obj = new ExampleObj();
        obj.setId(1);
        obj.setName("xxx");
        byte[] bytes = mapper.writeValueAsBytes(obj);
        ByteArrayEntity entity = new ByteArrayEntity(bytes, ContentType.create("application/json"));
        httpPost.setEntity(entity);
        HttpResponse httpResponse = httpclient.execute(httpPost);
        Assert.assertEquals(200, httpResponse.getStatusLine().getStatusCode());
        byte[] data = EntityUtils.toByteArray(httpResponse.getEntity());
        ExampleObj result = mapper.readValue(data, ExampleObj.class);
        Assert.assertEquals("xxxxx", result.getName());
    }
}
Also used : ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) HttpPost(org.apache.http.client.methods.HttpPost) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) HttpClient(org.apache.http.client.HttpClient) HttpResponse(org.apache.http.HttpResponse) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 42 with ApplicationConfig

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

the class ServerB method main.

public static void main(String[] args) {
    // B服务里的C服务客户端
    ConsumerConfig<ServiceC> consumerConfig = new ConsumerConfig<ServiceC>().setApplication(new ApplicationConfig().setAppName("BBB")).setInterfaceId(ServiceC.class.getName()).setDirectUrl("bolt://127.0.0.1:12299?appName=CCC").setRegister(false).setInvokeType(// 不设置,调用级别可设置
    "callback").setTimeout(2000);
    ServiceC serviceC = consumerConfig.refer();
    ServerConfig serverConfig = new ServerConfig().setPort(12298).setDaemon(false);
    ProviderConfig<ServiceB> providerConfig = new ProviderConfig<ServiceB>().setInterfaceId(ServiceB.class.getName()).setApplication(new ApplicationConfig().setAppName("BBB")).setRef(new ServiceBImpl(serviceC)).setServer(serverConfig).setRegister(false);
    providerConfig.export();
}
Also used : ServiceC(com.alipay.sofa.rpc.asynchain.ServiceC) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ServiceB(com.alipay.sofa.rpc.asynchain.ServiceB) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ServiceBImpl(com.alipay.sofa.rpc.asynchain.ServiceBImpl) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig)

Example 43 with ApplicationConfig

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

the class ServerC method main.

public static void main(String[] args) {
    ServerConfig serverConfig = new ServerConfig().setPort(12299).setDaemon(false);
    ProviderConfig<ServiceC> providerConfig = new ProviderConfig<ServiceC>().setInterfaceId(ServiceC.class.getName()).setApplication(new ApplicationConfig().setAppName("CCC")).setRef(new ServiceCImpl(1000)).setServer(serverConfig).setRegister(false);
    providerConfig.export();
}
Also used : ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ServiceC(com.alipay.sofa.rpc.asynchain.ServiceC) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ServiceCImpl(com.alipay.sofa.rpc.asynchain.ServiceCImpl)

Example 44 with ApplicationConfig

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

the class DubboServerTest method testRegistrySync.

@Test
public // 同步调用,走服务注册中心
void testRegistrySync() {
    // 只有1个线程 执行
    ServerConfig serverConfig = new ServerConfig().setStopTimeout(10).setPort(20880).setProtocol("dubbo").setQueues(100).setCoreThreads(1).setMaxThreads(2).setHost(SystemInfo.getLocalHost());
    // 发布一个服务,每个请求要执行1秒
    ApplicationConfig serverApplacation = new ApplicationConfig();
    serverApplacation.setAppName("server");
    List<RegistryConfig> registryConfigs = new ArrayList<RegistryConfig>();
    RegistryConfig registryConfig;
    registryConfig = new RegistryConfig().setProtocol(RpcConstants.REGISTRY_PROTOCOL_ZK).setAddress("127.0.0.1:2181").setSubscribe(true).setRegister(true);
    List<MethodConfig> methodConfigs = new ArrayList<MethodConfig>();
    MethodConfig methodConfig = new MethodConfig();
    methodConfig.setTimeout(3000);
    methodConfig.setName("sayHello");
    methodConfigs.add(methodConfig);
    registryConfigs.add(registryConfig);
    ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl()).setServer(serverConfig).setRegister(true).setBootstrap("dubbo").setRegistry(registryConfigs).setApplication(serverApplacation);
    providerConfig.export();
    ApplicationConfig clientApplication = new ApplicationConfig();
    clientApplication.setAppName("client");
    ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setTimeout(30000).setRegister(true).setProtocol("dubbo").setApplication(clientApplication).setRegistry(registryConfigs).setMethods(methodConfigs).setInJVM(false);
    final HelloService demoService = consumerConfig.refer();
    String result = demoService.sayHello("xxx", 22);
    Assert.assertNotNull(result);
    ConsumerBootstrap bootstrap = consumerConfig.getConsumerBootstrap();
    Assert.assertTrue(bootstrap instanceof DubboConsumerBootstrap);
    Assert.assertTrue(bootstrap.isSubscribed());
    Assert.assertNotNull(bootstrap.getProxyIns());
    bootstrap.unRefer();
    try {
        bootstrap.getCluster();
        Assert.fail();
    } catch (Exception e) {
        Assert.assertTrue(e instanceof UnsupportedOperationException);
    }
    try {
        bootstrap.subscribe();
        Assert.fail();
    } catch (Exception e) {
        Assert.assertTrue(e instanceof UnsupportedOperationException);
    }
}
Also used : RegistryConfig(com.alipay.sofa.rpc.config.RegistryConfig) ArrayList(java.util.ArrayList) HelloService(com.alipay.sofa.rpc.test.HelloService) HelloServiceImpl(com.alipay.sofa.rpc.test.HelloServiceImpl) MethodConfig(com.alipay.sofa.rpc.config.MethodConfig) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ConsumerBootstrap(com.alipay.sofa.rpc.bootstrap.ConsumerBootstrap) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) Test(org.junit.Test) BaseZkTest(com.alipay.sofa.rpc.registry.base.BaseZkTest)

Example 45 with ApplicationConfig

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

the class DubboConsumerBootstrap method copyApplication.

private void copyApplication(ConsumerConfig<T> consumerConfig, ReferenceConfig<T> referenceConfig) {
    ApplicationConfig applicationConfig = consumerConfig.getApplication();
    com.alibaba.dubbo.config.ApplicationConfig dubboConfig = new com.alibaba.dubbo.config.ApplicationConfig();
    dubboConfig.setName(applicationConfig.getAppName());
    referenceConfig.setApplication(dubboConfig);
}
Also used : ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig)

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