Search in sources :

Example 6 with ConsumerConfig

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

the class RpcLookout method collectConsumerSubInfo.

/**
 * Collect the RPC client information.
 *
 * @param consumerConfig client information model
 */
public void collectConsumerSubInfo(final ConsumerConfig consumerConfig) {
    try {
        Id consumerConfigId = rpcLookoutId.fetchConsumerSubId();
        Lookout.registry().info(consumerConfigId, new Info<ConsumerConfig>() {

            @Override
            public ConsumerConfig value() {
                return consumerConfig;
            }
        });
    } catch (Throwable t) {
        LOGGER.error(LogCodes.getLog(LogCodes.ERROR_METRIC_REPORT_ERROR), t);
    }
}
Also used : ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) Id(com.alipay.lookout.api.Id)

Example 7 with ConsumerConfig

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

the class TripleServerTest method testSync.

@Test
public // 同步调用,直连
void testSync() {
    ApplicationConfig applicationConfig = new ApplicationConfig().setAppName("triple-server");
    int port = 50052;
    ServerConfig serverConfig = new ServerConfig().setProtocol(RpcConstants.PROTOCOL_TYPE_TRIPLE).setPort(port);
    ProviderConfig<SofaGreeterTriple.IGreeter> providerConfig = new ProviderConfig<SofaGreeterTriple.IGreeter>().setApplication(applicationConfig).setBootstrap(RpcConstants.PROTOCOL_TYPE_TRIPLE).setInterfaceId(SofaGreeterTriple.IGreeter.class.getName()).setRef(new GreeterImpl()).setServer(serverConfig);
    providerConfig.export();
    ConsumerConfig<SofaGreeterTriple.IGreeter> consumerConfig = new ConsumerConfig<SofaGreeterTriple.IGreeter>();
    consumerConfig.setInterfaceId(SofaGreeterTriple.IGreeter.class.getName()).setProtocol(RpcConstants.PROTOCOL_TYPE_TRIPLE).setDirectUrl("tri://127.0.0.1:" + port);
    SofaGreeterTriple.IGreeter greeterBlockingStub = consumerConfig.refer();
    HelloRequest.DateTime dateTime = HelloRequest.DateTime.newBuilder().setDate("2018-12-28").setTime("11:13:00").build();
    HelloReply reply = null;
    HelloRequest request = HelloRequest.newBuilder().setName("world").setDateTime(dateTime).build();
    reply = greeterBlockingStub.sayHello(request);
    Assert.assertNotNull(reply);
}
Also used : SofaGreeterTriple(io.grpc.examples.helloworld.SofaGreeterTriple) HelloRequest(io.grpc.examples.helloworld.HelloRequest) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) HelloReply(io.grpc.examples.helloworld.HelloReply) Test(org.junit.Test)

Example 8 with ConsumerConfig

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

the class TripleServerTest method testSyncTimeout.

@Test
public // 同步调用,直连
void testSyncTimeout() {
    ApplicationConfig applicationConfig = new ApplicationConfig().setAppName("triple-server");
    int port = 50052;
    ServerConfig serverConfig = new ServerConfig().setProtocol(RpcConstants.PROTOCOL_TYPE_TRIPLE).setPort(port);
    ProviderConfig<SofaGreeterTriple.IGreeter> providerConfig = new ProviderConfig<SofaGreeterTriple.IGreeter>().setApplication(applicationConfig).setBootstrap(RpcConstants.PROTOCOL_TYPE_TRIPLE).setInterfaceId(SofaGreeterTriple.IGreeter.class.getName()).setRef(new GreeterImpl()).setServer(serverConfig);
    providerConfig.export();
    ConsumerConfig<SofaGreeterTriple.IGreeter> consumerConfig = new ConsumerConfig<SofaGreeterTriple.IGreeter>();
    consumerConfig.setInterfaceId(SofaGreeterTriple.IGreeter.class.getName()).setProtocol(RpcConstants.PROTOCOL_TYPE_TRIPLE).setDirectUrl("tri://127.0.0.1:" + port).setTimeout(1);
    SofaGreeterTriple.IGreeter greeterBlockingStub = consumerConfig.refer();
    HelloRequest.DateTime dateTime = HelloRequest.DateTime.newBuilder().setDate("2018-12-28").setTime("11:13:00").build();
    HelloReply reply = null;
    HelloRequest request = HelloRequest.newBuilder().setName("world").setDateTime(dateTime).build();
    boolean exp = false;
    try {
        reply = greeterBlockingStub.sayHello(request);
    } catch (SofaTimeOutException e) {
        exp = true;
    }
    Assert.assertTrue(exp);
}
Also used : SofaGreeterTriple(io.grpc.examples.helloworld.SofaGreeterTriple) HelloRequest(io.grpc.examples.helloworld.HelloRequest) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) SofaTimeOutException(com.alipay.sofa.rpc.core.exception.SofaTimeOutException) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) HelloReply(io.grpc.examples.helloworld.HelloReply) Test(org.junit.Test)

Example 9 with ConsumerConfig

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

the class TripleServerTest method testSyncWithUniqueId.

@Test
public // 同步调用,直连 有uniqueId
void testSyncWithUniqueId() {
    ApplicationConfig applicationConfig = new ApplicationConfig().setAppName("triple-server");
    int port = 50052;
    ServerConfig serverConfig = new ServerConfig().setProtocol(RpcConstants.PROTOCOL_TYPE_TRIPLE).setPort(port);
    ProviderConfig<SofaGreeterTriple.IGreeter> providerConfig = new ProviderConfig<SofaGreeterTriple.IGreeter>().setApplication(applicationConfig).setUniqueId("abc").setBootstrap(RpcConstants.PROTOCOL_TYPE_TRIPLE).setInterfaceId(SofaGreeterTriple.IGreeter.class.getName()).setRef(new GreeterImpl()).setServer(serverConfig);
    providerConfig.export();
    ConsumerConfig<SofaGreeterTriple.IGreeter> consumerConfig = new ConsumerConfig<SofaGreeterTriple.IGreeter>();
    consumerConfig.setInterfaceId(SofaGreeterTriple.IGreeter.class.getName()).setProtocol(RpcConstants.PROTOCOL_TYPE_TRIPLE).setUniqueId("abc").setDirectUrl("tri://127.0.0.1:" + port);
    SofaGreeterTriple.IGreeter greeterBlockingStub = consumerConfig.refer();
    HelloRequest.DateTime dateTime = HelloRequest.DateTime.newBuilder().setDate("2018-12-28").setTime("11:13:00").build();
    HelloReply reply = null;
    HelloRequest request = HelloRequest.newBuilder().setName("world").setDateTime(dateTime).build();
    reply = greeterBlockingStub.sayHello(request);
    Assert.assertNotNull(reply);
    consumerConfig = new ConsumerConfig<SofaGreeterTriple.IGreeter>();
    consumerConfig.setInterfaceId(SofaGreeterTriple.IGreeter.class.getName()).setProtocol(RpcConstants.PROTOCOL_TYPE_TRIPLE).setDirectUrl("tri://127.0.0.1:" + port);
    greeterBlockingStub = consumerConfig.refer();
    dateTime = HelloRequest.DateTime.newBuilder().setDate("2018-12-28").setTime("11:13:00").build();
    request = HelloRequest.newBuilder().setName("world").setDateTime(dateTime).build();
    reply = greeterBlockingStub.sayHello(request);
    Assert.assertNotNull(reply);
}
Also used : SofaGreeterTriple(io.grpc.examples.helloworld.SofaGreeterTriple) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) HelloRequest(io.grpc.examples.helloworld.HelloRequest) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) HelloReply(io.grpc.examples.helloworld.HelloReply) Test(org.junit.Test)

Example 10 with ConsumerConfig

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

the class Http2ClearTextExceptionTest method testProtobuf.

@Test
public void testProtobuf() throws InterruptedException {
    // 只有1个线程 执行
    ServerConfig serverConfig = new ServerConfig().setStopTimeout(60000).setPort(12333).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setDaemon(true);
    // 发布一个服务,每个请求要执行1秒
    ProviderConfig<HttpService> providerConfig = new ProviderConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setRef(new HttpServiceImpl()).setServer(serverConfig).setApplication(new ApplicationConfig().setAppName("serverApp")).setRegister(false);
    providerConfig.export();
    {
        ConsumerConfig<HttpService> consumerConfig = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).setDirectUrl("h2c://127.0.0.1:12333").setApplication(new ApplicationConfig().setAppName("clientApp")).setTimeout(500).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C);
        HttpService httpService = consumerConfig.refer();
        EchoRequest request = EchoRequest.newBuilder().setGroup(Group.B).setName("xxx").build();
        try {
            EchoResponse response = httpService.echoPb(request);
            Assert.fail();
        } catch (SofaRpcException e) {
            Assert.assertTrue(e.getErrorType() == RpcErrorType.SERVER_BIZ);
        }
    }
    {
        ConsumerConfig<HttpService> consumerConfig2 = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).setDirectUrl("h2c://127.0.0.1:12333").setApplication(new ApplicationConfig().setAppName("clientApp")).setTimeout(500).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setInvokeType(RpcConstants.INVOKER_TYPE_ONEWAY).setRepeatedReferLimit(-1);
        HttpService httpService2 = consumerConfig2.refer();
        EchoRequest request = EchoRequest.newBuilder().setGroup(Group.B).setName("xxx").build();
        try {
            httpService2.echoPb(request);
            // NOT SUPPORTED NOW, If want support this, need add key to head.
            Assert.fail();
        } catch (Exception e) {
            Assert.assertTrue(e instanceof SofaRpcException);
        }
    }
    {
        ConsumerConfig<HttpService> consumerConfig3 = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).setDirectUrl("h2c://127.0.0.1:12333").setApplication(new ApplicationConfig().setAppName("clientApp")).setTimeout(500).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setInvokeType(RpcConstants.INVOKER_TYPE_FUTURE).setRepeatedReferLimit(-1);
        HttpService httpService3 = consumerConfig3.refer();
        EchoRequest request = EchoRequest.newBuilder().setGroup(Group.B).setName("xxx").build();
        EchoResponse response = httpService3.echoPb(request);
        Assert.assertNull(response);
        ResponseFuture future = RpcInvokeContext.getContext().getFuture();
        try {
            future.get();
            Assert.fail();
        } catch (ExecutionException e) {
            SofaRpcException re = (SofaRpcException) e.getCause();
            Assert.assertTrue(re.getErrorType() == RpcErrorType.SERVER_BIZ);
        }
    }
    {
        final Object[] result = new Object[1];
        final CountDownLatch latch = new CountDownLatch(1);
        ConsumerConfig<HttpService> consumerConfig4 = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).setTimeout(500).setDirectUrl("h2c://127.0.0.1:12333").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] = appResponse;
                latch.countDown();
            }

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

            @Override
            public void onSofaException(SofaRpcException exception, String methodName, RequestBase request) {
                result[0] = exception;
                latch.countDown();
            }
        }).setRepeatedReferLimit(-1);
        HttpService httpService4 = consumerConfig4.refer();
        EchoRequest request = EchoRequest.newBuilder().setGroup(Group.B).setName("xxx").build();
        EchoResponse response = httpService4.echoPb(request);
        Assert.assertNull(response);
        latch.await(2000, TimeUnit.MILLISECONDS);
        Throwable e = (Throwable) result[0];
        Assert.assertTrue(e instanceof SofaRpcException);
        Assert.assertTrue(((SofaRpcException) e).getErrorType() == RpcErrorType.SERVER_BIZ);
    }
}
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) ExecutionException(java.util.concurrent.ExecutionException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) RequestBase(com.alipay.sofa.rpc.core.request.RequestBase) 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) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

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