Search in sources :

Example 1 with ApplicationConfig

use of com.alipay.sofa.rpc.config.ApplicationConfig in project Sentinel by alibaba.

the class DemoConsumer method main.

public static void main(String[] args) throws Exception {
    ApplicationConfig application = new ApplicationConfig().setAppName("DemoConsumer");
    ConsumerConfig<DemoService> consumerConfig = new ConsumerConfig<DemoService>().setApplication(application).setInterfaceId(DemoService.class.getName()).setProtocol("bolt").setDirectUrl("bolt://127.0.0.1:12001").setInvokeType(RpcConstants.INVOKER_TYPE_SYNC);
    // 设置是否启用Sentinel,默认启用
    // 也可在rpc-config.json全局设置
    // consumerConfig.setParameter("sofa.rpc.sentinel.enabled", "false");
    DemoService helloService = consumerConfig.refer();
    System.out.println("DemoConsumer started!");
    long sleepMs = 5;
    int total = 5000;
    int index = 0;
    System.out.println("Total call " + total + " times and sleep " + sleepMs + "ms after each call.");
    while (true) {
        try {
            index++;
            String result = helloService.sayHello(index, "SOFARPC", 2020);
            System.out.println("[" + index + "][Consumer]receive response: " + result);
        } catch (Exception e) {
            System.out.println("[" + index + "][Consumer]receive exception: " + e.getMessage());
        }
        TimeUnit.MILLISECONDS.sleep(sleepMs);
        if (index == total) {
            break;
        }
    }
    System.out.println("DemoConsumer exit!");
    System.exit(0);
}
Also used : ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) DemoService(com.alibaba.csp.sentinel.demo.sofa.rpc.service.DemoService)

Example 2 with ApplicationConfig

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

the class FaultBaseTest method init.

@Before
public void init() {
    // 只有1个线程 执行
    serverConfig = new ServerConfig().setStopTimeout(60000).setPort(12299).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setQueues(100).setCoreThreads(10).setMaxThreads(20);
    ApplicationConfig providerAconfig = new ApplicationConfig();
    providerAconfig.setAppName("testApp");
    // 发布一个服务,每个请求要执行1秒
    providerConfig = new ProviderConfig<FaultHelloService>().setInterfaceId(FaultHelloService.class.getName()).setRef(new HelloServiceTimeOutImpl()).setServer(serverConfig).setRegister(false).setApplication(providerAconfig);
    // just for test
    consumerConfigNotUse = new ConsumerConfig<FaultHelloService>().setInterfaceId(FaultHelloService.class.getName()).setTimeout(500).setDirectUrl("127.0.0.1:12299").setRegister(false).setUniqueId("xxx").setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT);
    ApplicationConfig applicationConfig = new ApplicationConfig();
    applicationConfig.setAppName(APP_NAME1);
    consumerConfig = new ConsumerConfig<FaultHelloService>().setInterfaceId(FaultHelloService.class.getName()).setTimeout(500).setDirectUrl("127.0.0.1:12299").setRegister(false).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setApplication(applicationConfig);
    consumerConfig2 = new ConsumerConfig<FaultHelloService2>().setInterfaceId(FaultHelloService2.class.getName()).setTimeout(500).setDirectUrl("127.0.0.1:12299").setRegister(false).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setApplication(applicationConfig);
    consumerConfigAnotherApp = new ConsumerConfig<FaultHelloService>().setInterfaceId(FaultHelloService.class.getName()).setDirectUrl("127.0.0.1:12299").setTimeout(500).setRegister(true).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setApplication(new ApplicationConfig().setAppName(APP_NAME2));
    FaultToleranceModule module = (FaultToleranceModule) ExtensionLoaderFactory.getExtensionLoader(Module.class).getExtension("fault-tolerance");
    module.getRegulator().init();
}
Also used : ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) FaultHelloService2(com.alipay.sofa.rpc.client.aft.bean.FaultHelloService2) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) FaultToleranceModule(com.alipay.sofa.rpc.module.FaultToleranceModule) FaultHelloService(com.alipay.sofa.rpc.client.aft.bean.FaultHelloService) HelloServiceTimeOutImpl(com.alipay.sofa.rpc.client.aft.bean.HelloServiceTimeOutImpl) Before(org.junit.Before)

Example 3 with ApplicationConfig

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

the class TripleServerTest method testExposeTwice.

@Test
public void testExposeTwice() {
    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();
    applicationConfig = new ApplicationConfig().setAppName("triple-server");
    serverConfig = new ServerConfig().setProtocol(RpcConstants.PROTOCOL_TYPE_TRIPLE).setPort(port);
    providerConfig = new ProviderConfig<SofaGreeterTriple.IGreeter>().setApplication(applicationConfig).setUniqueId("abcd").setBootstrap(RpcConstants.PROTOCOL_TYPE_TRIPLE).setInterfaceId(SofaGreeterTriple.IGreeter.class.getName()).setRef(new GreeterImpl()).setServer(serverConfig);
    providerConfig.export();
}
Also used : ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) SofaGreeterTriple(io.grpc.examples.helloworld.SofaGreeterTriple) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) Test(org.junit.Test)

Example 4 with ApplicationConfig

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

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

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