Search in sources :

Example 26 with ConsumerConfig

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

the class Http2ClearTextPriorKnowledgeTest method testProtobuf.

@Test
public void testProtobuf() {
    // 只有1个线程 执行
    ServerConfig serverConfig = new ServerConfig().setStopTimeout(60000).setPort(12300).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setDaemon(true);
    // 发布一个服务,每个请求要执行1秒
    ProviderConfig<HttpService> providerConfig = new ProviderConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setRef(new HttpServiceImpl()).setApplication(new ApplicationConfig().setAppName("serverApp")).setServer(serverConfig).setRegister(false);
    providerConfig.export();
    {
        ConsumerConfig<HttpService> consumerConfig = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).setDirectUrl("h2c://127.0.0.1:12300").setApplication(new ApplicationConfig().setAppName("clientApp")).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C);
        HttpService httpService = consumerConfig.refer();
        EchoRequest request = EchoRequest.newBuilder().setGroup(Group.A).setName("xxx").build();
        EchoResponse response = httpService.echoPb(request);
        Assert.assertEquals("helloxxx", response.getMessage());
        Assert.assertEquals(200, response.getCode());
    }
    {
        ConsumerConfig<HttpService> consumerConfig2 = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).setDirectUrl("h2c://127.0.0.1:12300").setApplication(new ApplicationConfig().setAppName("clientApp")).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setInvokeType(RpcConstants.INVOKER_TYPE_ONEWAY).setRepeatedReferLimit(-1);
        HttpService httpService2 = consumerConfig2.refer();
        EchoRequest request = EchoRequest.newBuilder().setGroup(Group.A).setName("xxx").build();
        try {
            httpService2.echoPb(request);
            // NOT SUPPORTED NOW, If want support this, need add key to head.
            Assert.fail();
        } catch (Exception e) {
        }
    }
    {
        ConsumerConfig<HttpService> consumerConfig3 = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).setDirectUrl("h2c://127.0.0.1:12300").setApplication(new ApplicationConfig().setAppName("clientApp")).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setInvokeType(RpcConstants.INVOKER_TYPE_FUTURE).setRepeatedReferLimit(-1);
        HttpService httpService3 = consumerConfig3.refer();
        EchoRequest request = EchoRequest.newBuilder().setGroup(Group.A).setName("xxx").build();
        EchoResponse response = httpService3.echoPb(request);
        Assert.assertNull(response);
        ResponseFuture future = RpcInvokeContext.getContext().getFuture();
        try {
            response = (EchoResponse) future.get();
            Assert.assertEquals("helloxxx", response.getMessage());
            Assert.assertEquals(200, response.getCode());
        } catch (Exception e) {
            e.printStackTrace();
            Assert.fail();
        }
    }
    {
        final EchoResponse[] result = new EchoResponse[1];
        final CountDownLatch latch = new CountDownLatch(1);
        ConsumerConfig<HttpService> consumerConfig4 = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).setDirectUrl("h2c://127.0.0.1:12300").setApplication(new ApplicationConfig().setAppName("clientApp")).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setInvokeType(RpcConstants.INVOKER_TYPE_CALLBACK).setOnReturn(new SofaResponseCallback() {

            @Override
            public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
                result[0] = (EchoResponse) appResponse;
                latch.countDown();
            }

            @Override
            public void onAppException(Throwable throwable, String methodName, RequestBase request) {
                latch.countDown();
            }

            @Override
            public void onSofaException(SofaRpcException sofaException, String methodName, RequestBase request) {
                latch.countDown();
            }
        }).setRepeatedReferLimit(-1);
        HttpService httpService4 = consumerConfig4.refer();
        EchoRequest request = EchoRequest.newBuilder().setGroup(Group.A).setName("xxx").build();
        EchoResponse response = httpService4.echoPb(request);
        Assert.assertNull(response);
        try {
            latch.await(2000, TimeUnit.MILLISECONDS);
            response = result[0];
            Assert.assertNotNull(response);
            Assert.assertEquals("helloxxx", response.getMessage());
            Assert.assertEquals(200, response.getCode());
        } catch (Exception e) {
            e.printStackTrace();
            Assert.fail();
        }
    }
}
Also used : EchoResponse(com.alipay.sofa.rpc.server.bolt.pb.EchoResponse) EchoRequest(com.alipay.sofa.rpc.server.bolt.pb.EchoRequest) SofaResponseCallback(com.alipay.sofa.rpc.core.invoke.SofaResponseCallback) ResponseFuture(com.alipay.sofa.rpc.message.ResponseFuture) CountDownLatch(java.util.concurrent.CountDownLatch) HttpServiceImpl(com.alipay.sofa.rpc.server.http.HttpServiceImpl) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) RequestBase(com.alipay.sofa.rpc.core.request.RequestBase) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) HttpService(com.alipay.sofa.rpc.server.http.HttpService) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 27 with ConsumerConfig

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

the class TripleHessianInvokeTest method testInvoke.

@Test
public void testInvoke() throws InterruptedException {
    RpcRunningState.setDebugMode(true);
    ApplicationConfig clientApp = new ApplicationConfig().setAppName("triple-client");
    ApplicationConfig serverApp = new ApplicationConfig().setAppName("triple-server");
    int port = getPort();
    ServerConfig serverConfig = new ServerConfig().setProtocol(RpcConstants.PROTOCOL_TYPE_TRIPLE).setPort(port);
    TripleHessianInterfaceImpl ref = new TripleHessianInterfaceImpl();
    ProviderConfig<TripleHessianInterface> providerConfig = getProviderConfig().setApplication(serverApp).setBootstrap(RpcConstants.PROTOCOL_TYPE_TRIPLE).setInterfaceId(TripleHessianInterface.class.getName()).setRef(ref).setServer(serverConfig).setRegister(false);
    providerConfig.export();
    ConsumerConfig<TripleHessianInterface> consumerConfig = new ConsumerConfig<TripleHessianInterface>();
    consumerConfig.setInterfaceId(TripleHessianInterface.class.getName()).setProtocol(RpcConstants.PROTOCOL_TYPE_TRIPLE).setDirectUrl("localhost:" + port).setRegister(false).setApplication(clientApp);
    TripleHessianInterface helloService = consumerConfig.refer();
    LOGGER.info("Grpc stub bean successful: {}", helloService.getClass().getName());
    helloService.call();
    Assert.assertEquals("call", ref.getFlag());
    // test Pressure Mark
    boolean isLoadTest = helloService.testPressureMark("name");
    Assert.assertFalse(isLoadTest);
    SofaTracerSpan currentSpan = SofaTraceContextHolder.getSofaTraceContext().getCurrentSpan();
    Map<String, String> bizBaggage = currentSpan.getSofaTracerSpanContext().getBizBaggage();
    bizBaggage.put("mark", "T");
    Assert.assertTrue(TracerUtils.isLoadTest(currentSpan));
    isLoadTest = helloService.testPressureMark("name");
    Assert.assertTrue(isLoadTest);
    String s = helloService.call1();
    Assert.assertEquals("call1", ref.getFlag());
    Assert.assertEquals("call1", s);
    Request request = new Request();
    int age = RandomUtils.nextInt();
    request.setAge(age);
    String call2 = "call2";
    request.setFlag(call2);
    Response response = helloService.call2(request);
    Assert.assertEquals(age, response.getAge());
    Assert.assertEquals(call2, response.getFlag());
    Response response1 = helloService.call2(null);
    Assert.assertNull(response1);
    providerConfig.unExport();
    serverConfig.destroy();
}
Also used : ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) SofaTracerSpan(com.alipay.common.tracer.core.span.SofaTracerSpan) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) Test(org.junit.Test)

Example 28 with ConsumerConfig

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

the class TripleHessianInvokeTest method testInvokeWithUniqueId.

@Test
public void testInvokeWithUniqueId() throws InterruptedException {
    String uniqueId = "uniqueId1";
    RpcRunningState.setDebugMode(true);
    ApplicationConfig clientApp = new ApplicationConfig().setAppName("triple-client");
    ApplicationConfig serverApp = new ApplicationConfig().setAppName("triple-server");
    int port = getPort();
    ServerConfig serverConfig = new ServerConfig().setProtocol(RpcConstants.PROTOCOL_TYPE_TRIPLE).setPort(port);
    TripleHessianInterfaceImpl ref = new TripleHessianInterfaceImpl();
    ProviderConfig<TripleHessianInterface> providerConfig = getProviderConfig().setApplication(serverApp).setUniqueId(uniqueId).setBootstrap(RpcConstants.PROTOCOL_TYPE_TRIPLE).setInterfaceId(TripleHessianInterface.class.getName()).setRef(ref).setServer(serverConfig).setRegister(false);
    providerConfig.export();
    ConsumerConfig<TripleHessianInterface> consumerConfig = new ConsumerConfig<TripleHessianInterface>();
    consumerConfig.setInterfaceId(TripleHessianInterface.class.getName()).setProtocol(RpcConstants.PROTOCOL_TYPE_TRIPLE).setDirectUrl("localhost:" + port).setUniqueId(uniqueId).setRegister(false).setApplication(clientApp);
    TripleHessianInterface helloService = consumerConfig.refer();
    LOGGER.info("Grpc stub bean successful: {}", helloService.getClass().getName());
    helloService.call();
    Assert.assertEquals("call", ref.getFlag());
    String s = helloService.call1();
    Assert.assertEquals("call1", ref.getFlag());
    Assert.assertEquals("call1", s);
    Request request = new Request();
    int age = RandomUtils.nextInt();
    request.setAge(age);
    String call2 = "call2";
    request.setFlag(call2);
    Response response = helloService.call2(request);
    Assert.assertEquals(age, response.getAge());
    Assert.assertEquals(call2, response.getFlag());
    Response response1 = helloService.call2(null);
    Assert.assertNull(response1);
    // 测试没有设置 uniqueId 的情况,也能访问
    consumerConfig = new ConsumerConfig<TripleHessianInterface>();
    consumerConfig.setInterfaceId(TripleHessianInterface.class.getName()).setProtocol(RpcConstants.PROTOCOL_TYPE_TRIPLE).setDirectUrl("localhost:" + port).setRegister(false).setApplication(clientApp);
    helloService = consumerConfig.refer();
    LOGGER.info("Grpc stub bean successful: {}", helloService.getClass().getName());
    helloService.call();
    Assert.assertEquals("call", ref.getFlag());
    s = helloService.call1();
    Assert.assertEquals("call1", ref.getFlag());
    Assert.assertEquals("call1", s);
    request = new Request();
    age = RandomUtils.nextInt();
    request.setAge(age);
    call2 = "call2";
    request.setFlag(call2);
    response = helloService.call2(request);
    Assert.assertEquals(age, response.getAge());
    Assert.assertEquals(call2, response.getFlag());
    response1 = helloService.call2(null);
    Assert.assertNull(response1);
    providerConfig.unExport();
    serverConfig.destroy();
}
Also used : ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) Test(org.junit.Test)

Example 29 with ConsumerConfig

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

the class RpcReferenceContextFilter method invoke.

@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
    ConsumerConfig config = (ConsumerConfig) invoker.getConfig();
    RpcReferenceContext referenceCtx = new RpcReferenceContext();
    referenceCtx.setGeneric(config.isGeneric());
    referenceCtx.setInterfaceName(config.getInterfaceId());
    referenceCtx.setUniqueId(config.getUniqueId());
    referenceCtx.setServiceName(request.getTargetServiceUniqueName());
    referenceCtx.setMethodName(request.getMethodName());
    RpcInternalContext context = RpcInternalContext.getContext();
    ProviderInfo providerInfo = context.getProviderInfo();
    if (providerInfo != null) {
        referenceCtx.setTargetAppName(providerInfo.getStaticAttr(ProviderInfoAttrs.ATTR_APP_NAME));
        referenceCtx.setTargetUrl(providerInfo.getHost() + ":" + providerInfo.getPort());
    }
    referenceCtx.setProtocol(config.getProtocol());
    referenceCtx.setInvokeType(request.getInvokeType());
    referenceCtx.setRouteRecord((String) context.getAttachment(RpcConstants.INTERNAL_KEY_ROUTER_RECORD));
    RpcInvokeContext.getContext().put(RemotingConstants.INVOKE_CTX_RPC_REF_CTX, referenceCtx);
    SofaResponse response = invoker.invoke(request);
    // 调用后
    InetSocketAddress local = context.getLocalAddress();
    if (local != null) {
        referenceCtx.setClientIP(NetUtils.toIpString(local));
        referenceCtx.setClientPort(local.getPort());
    }
    Long ct = (Long) context.getAttachment(RpcConstants.INTERNAL_KEY_CONN_CREATE_TIME);
    if (ct != null) {
        referenceCtx.setConnEstablishedSpan(ct);
    }
    Integer qs = (Integer) context.getAttachment(RpcConstants.INTERNAL_KEY_REQ_SIZE);
    if (qs != null) {
        referenceCtx.setRequestSize(qs.longValue());
    }
    Integer ps = (Integer) context.getAttachment(RpcConstants.INTERNAL_KEY_RESP_SIZE);
    if (ps != null) {
        referenceCtx.setResponseSize(ps.longValue());
    }
    referenceCtx.setTraceId((String) context.getAttachment(RpcConstants.INTERNAL_KEY_TRACE_ID));
    referenceCtx.setRpcId((String) context.getAttachment(RpcConstants.INTERNAL_KEY_SPAN_ID));
    Long ce = (Long) context.getAttachment(RpcConstants.INTERNAL_KEY_CLIENT_ELAPSE);
    if (ce != null) {
        referenceCtx.setCostTime(ce);
    }
    return response;
}
Also used : ProviderInfo(com.alipay.sofa.rpc.client.ProviderInfo) InetSocketAddress(java.net.InetSocketAddress) RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) RpcReferenceContext(com.alipay.sofa.rpc.api.context.RpcReferenceContext) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse)

Example 30 with ConsumerConfig

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

the class Http2ClientMultipleMain method main.

public static void main(String[] args) {
    ApplicationConfig application = new ApplicationConfig().setAppName("test-client");
    ConsumerConfig<ProtoService> consumerConfig = new ConsumerConfig<ProtoService>().setApplication(application).setInterfaceId(ProtoService.class.getName()).setProtocol("h2c").setDirectUrl("h2c://127.0.0.1:12300").setSerialization("protobuf").setTimeout(3000);
    final ProtoService protoService = consumerConfig.refer();
    LOGGER.warn("started at pid {}", RpcRuntimeContext.PID);
    final int threads = 50;
    final AtomicLong cnt = new AtomicLong(0);
    final ThreadPoolExecutor service1 = new ThreadPoolExecutor(threads, threads, 0L, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>(), // 无队列
    new NamedThreadFactory("client-"));
    for (int i = 0; i < threads; i++) {
        service1.execute(new Runnable() {

            @Override
            public void run() {
                while (true) {
                    try {
                        EchoRequest request = EchoRequest.newBuilder().setGroup(Group.A).setName("12345678").build();
                        EchoResponse s = protoService.echoObj(request);
                        cnt.incrementAndGet();
                    } catch (Throwable e) {
                        LOGGER.error("", e);
                    }
                }
            }
        });
    }
    Thread thread = new Thread(new Runnable() {

        private long last = 0;

        @Override
        public void run() {
            while (true) {
                long count = cnt.get();
                long tps = count - last;
                LOGGER.error("last 1s invoke: {}, queue: {}", tps, service1.getQueue().size());
                last = count;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                }
            }
        }
    }, "Print-tps-THREAD");
    thread.start();
}
Also used : EchoResponse(com.alipay.sofa.rpc.protobuf.EchoResponse) NamedThreadFactory(com.alipay.sofa.rpc.common.struct.NamedThreadFactory) EchoRequest(com.alipay.sofa.rpc.protobuf.EchoRequest) AtomicLong(java.util.concurrent.atomic.AtomicLong) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ProtoService(com.alipay.sofa.rpc.protobuf.ProtoService) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Aggregations

ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)139 Test (org.junit.Test)86 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)68 ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)61 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)44 ActivelyDestroyTest (com.alipay.sofa.rpc.test.ActivelyDestroyTest)40 HelloService (com.alipay.sofa.rpc.test.HelloService)38 CountDownLatch (java.util.concurrent.CountDownLatch)27 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)21 HelloServiceImpl (com.alipay.sofa.rpc.test.HelloServiceImpl)21 ArrayList (java.util.ArrayList)19 ProviderGroup (com.alipay.sofa.rpc.client.ProviderGroup)18 SofaResponseCallback (com.alipay.sofa.rpc.core.invoke.SofaResponseCallback)14 RegistryConfig (com.alipay.sofa.rpc.config.RegistryConfig)12 RequestBase (com.alipay.sofa.rpc.core.request.RequestBase)12 MethodConfig (com.alipay.sofa.rpc.config.MethodConfig)10 ProviderInfo (com.alipay.sofa.rpc.client.ProviderInfo)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 ConsumerBootstrap (com.alipay.sofa.rpc.bootstrap.ConsumerBootstrap)8 SofaTimeOutException (com.alipay.sofa.rpc.core.exception.SofaTimeOutException)8