com.alipay.sofa.rpc.config.ApplicationConfig Java Examples
The following examples show how to use
com.alipay.sofa.rpc.config.ApplicationConfig.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BoltServerMain.java From sofa-rpc with Apache License 2.0 | 6 votes |
public static void main(String[] args) { ApplicationConfig application = new ApplicationConfig().setAppName("test-server"); ServerConfig serverConfig = new ServerConfig() .setPort(22000) .setDaemon(false); ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>() .setInterfaceId(HelloService.class.getName()) .setApplication(application) .setRef(new HelloServiceImpl()) .setServer(serverConfig) .setRegister(false); ProviderConfig<EchoService> providerConfig2 = new ProviderConfig<EchoService>() .setInterfaceId(EchoService.class.getName()) .setApplication(application) .setRef(new EchoServiceImpl()) .setServer(serverConfig) .setRegister(false); providerConfig.export(); providerConfig2.export(); LOGGER.warn("started at pid {}", RpcRuntimeContext.PID); }
Example #2
Source File: BoltMockTest.java From sofa-rpc with Apache License 2.0 | 6 votes |
@Test public void testRemote() { HttpMockServer.initSever(1235); HttpMockServer.addMockPath("/", JSON.toJSONString("mockJson")); HttpMockServer.start(); final ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>() .setInterfaceId(HelloService.class.getName()) .setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT) .setBootstrap("bolt") .setApplication(new ApplicationConfig().setAppName("clientApp")) .setReconnectPeriod(1000) .setMockMode(MockMode.REMOTE) .setParameter("mockUrl", "http://127.0.0.1:1235/"); HelloService helloService = consumerConfig.refer(); Assert.assertEquals("mockJson", helloService.sayHello("xx", 22)); HttpMockServer.stop(); }
Example #3
Source File: BoltMockTest.java From sofa-rpc with Apache License 2.0 | 6 votes |
@Test public void testLocal() { final ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>() .setInterfaceId(HelloService.class.getName()) .setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT) .setBootstrap("bolt") .setApplication(new ApplicationConfig().setAppName("clientApp")) .setReconnectPeriod(1000) .setMockMode("local") .setMockRef(new HelloService() { @Override public String sayHello(String name, int age) { return "mock"; } }); HelloService helloService = consumerConfig.refer(); Assert.assertEquals("mock", helloService.sayHello("xx", 22)); }
Example #4
Source File: BoltExceptionTest.java From sofa-rpc with Apache License 2.0 | 6 votes |
@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)); } }
Example #5
Source File: FutureServerMain.java From sofa-rpc with Apache License 2.0 | 6 votes |
public static void main(String[] args) { ApplicationConfig applicationConfig = new ApplicationConfig().setAppName("future-server"); ServerConfig serverConfig2 = new ServerConfig() .setPort(22222) .setDaemon(false); ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>() .setApplication(applicationConfig) .setInterfaceId(HelloService.class.getName()) .setRef(new HelloServiceImpl(1000)) .setServer(serverConfig2); providerConfig.export(); ProviderConfig<EchoService> providerConfig2 = new ProviderConfig<EchoService>() .setApplication(applicationConfig) .setInterfaceId(EchoService.class.getName()) .setRef(new EchoServiceImpl()) .setServer(serverConfig2); providerConfig2.export(); }
Example #6
Source File: ServerB.java From sofa-rpc with Apache License 2.0 | 6 votes |
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(); }
Example #7
Source File: OnewayServerMain.java From sofa-rpc with Apache License 2.0 | 6 votes |
public static void main(String[] args) { ApplicationConfig applicationConfig = new ApplicationConfig().setAppName("oneway-server"); ServerConfig serverConfig2 = new ServerConfig() .setPort(22222) .setDaemon(false); ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>() .setApplication(applicationConfig) .setInterfaceId(HelloService.class.getName()) .setRef(new HelloServiceImpl(1000)) .setServer(serverConfig2); providerConfig.export(); ProviderConfig<EchoService> providerConfig2 = new ProviderConfig<EchoService>() .setApplication(applicationConfig) .setInterfaceId(EchoService.class.getName()) .setRef(new EchoServiceImpl()) .setServer(serverConfig2); providerConfig2.export(); }
Example #8
Source File: ConsulRegistryTest.java From sofa-rpc with Apache License 2.0 | 6 votes |
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; }
Example #9
Source File: CallbackServerMain.java From sofa-rpc with Apache License 2.0 | 6 votes |
public static void main(String[] args) { ApplicationConfig applicationConfig = new ApplicationConfig().setAppName("future-server"); ServerConfig serverConfig2 = new ServerConfig() .setPort(22222) .setDaemon(false); // C服务的服务端 ProviderConfig<HelloService> CProvider = new ProviderConfig<HelloService>() .setApplication(applicationConfig) .setInterfaceId(HelloService.class.getName()) .setRef(new HelloServiceImpl(1000)) .setServer(serverConfig2); CProvider.export(); }
Example #10
Source File: CustomFilterServerMain.java From sofa-rpc with Apache License 2.0 | 6 votes |
public static void main(String[] args) { Filter customEchoFilter2 = new CustomEchoFilter2(); ApplicationConfig application = new ApplicationConfig().setAppName("test-server"); ServerConfig serverConfig = new ServerConfig() .setPort(22000) .setDaemon(false); ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>() .setInterfaceId(HelloService.class.getName()) .setApplication(application) .setRef(new HelloServiceImpl()) .setServer(serverConfig) .setFilter(Arrays.asList("customEcho")) .setFilterRef(Arrays.asList(customEchoFilter2)) .setRegister(false); providerConfig.export(); LOGGER.error("started at pid {}", RpcRuntimeContext.PID); }
Example #11
Source File: TripleServerTest.java From sofa-rpc with Apache License 2.0 | 5 votes |
@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); }
Example #12
Source File: ConsulRegistryTest.java From sofa-rpc with Apache License 2.0 | 5 votes |
private ConsumerConfig<?> consumerConfig(String uniqueId) { ConsumerConfig<?> consumer = new ConsumerConfig(); consumer.setInterfaceId(INTERFACE_ID) .setUniqueId(uniqueId) .setApplication(new ApplicationConfig().setAppName("consul-registry-test")) .setProxy("javassist") .setSubscribe(true) .setSerialization("java") .setInvokeType("sync") .setTimeout(4444); return consumer; }
Example #13
Source File: NacosRegistryHelperTest.java From sofa-rpc with Apache License 2.0 | 5 votes |
@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()); }
Example #14
Source File: NacosRegistryHelperTest.java From sofa-rpc with Apache License 2.0 | 5 votes |
@Test public void buildServiceName() { 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); String serviceName = NacosRegistryHelper.buildServiceName(provider, RpcConstants.PROTOCOL_TYPE_BOLT); assertEquals(serviceName, "com.alipay.xxx.TestService:nacos-test:DEFAULT"); serviceName = NacosRegistryHelper.buildServiceName(provider, RpcConstants.PROTOCOL_TYPE_TR); assertEquals(serviceName, "com.alipay.xxx.TestService:nacos-test:DEFAULT"); serviceName = NacosRegistryHelper.buildServiceName(provider, RpcConstants.PROTOCOL_TYPE_TRIPLE); assertEquals(serviceName, "com.alipay.xxx.TestService:nacos-test:" + RpcConstants.PROTOCOL_TYPE_TRIPLE); serviceName = NacosRegistryHelper.buildServiceName(provider, RpcConstants.PROTOCOL_TYPE_REST); assertEquals(serviceName, "com.alipay.xxx.TestService:nacos-test:" + RpcConstants.PROTOCOL_TYPE_REST); }
Example #15
Source File: DubooServerTest.java From sofa-rpc with Apache License 2.0 | 5 votes |
@Test //同步泛化调用,直连 public void testGenericSync() { // 只有1个线程 执行 ServerConfig serverConfig = new ServerConfig() .setStopTimeout(60000) .setPort(20880) .setProtocol("dubbo") .setQueues(100).setCoreThreads(1).setMaxThreads(2); // 发布一个服务,每个请求要执行1秒 ApplicationConfig serverApplacation = new ApplicationConfig(); serverApplacation.setAppName("server"); providerConfig = new ProviderConfig<DemoService>() .setInterfaceId(DemoService.class.getName()) .setRef(new DemoServiceImpl()) .setBootstrap("dubbo") .setServer(serverConfig) // .setParameter(RpcConstants.CONFIG_HIDDEN_KEY_WARNING, "false") .setRegister(false).setApplication(serverApplacation); providerConfig.export(); ApplicationConfig clientApplication = new ApplicationConfig(); clientApplication.setAppName("client"); consumerConfig = new ConsumerConfig<DemoService>() .setInterfaceId(DemoService.class.getName()) .setDirectUrl("dubbo://127.0.0.1:20880") .setBootstrap("dubbo") .setTimeout(30000) .setRegister(false) .setProtocol("dubbo") .setApplication(clientApplication) .setGeneric(true); final GenericService demoService = (GenericService) consumerConfig.refer(); String result = (String) demoService.$invoke("sayHello", new String[] { "java.lang.String" }, new Object[] { "xxx" }); Assert.assertEquals(result, "Hello xxx"); }
Example #16
Source File: FailoverClusterTest.java From sofa-rpc with Apache License 2.0 | 5 votes |
@Test public void testRpcDirectInvokeFromContextWithAvailableProviders() { ServerConfig serverConfig = new ServerConfig() .setProtocol("bolt") .setHost("0.0.0.0") .setPort(13900); ProviderConfig<HelloService> provider = new ProviderConfig(); provider.setInterfaceId(HelloService.class.getName()) .setRef(new HelloServiceImpl("x-demo-invoke")) .setApplication(new ApplicationConfig().setAppName("x-test-server")) .setProxy("javassist") .setSerialization("hessian2") .setServer(serverConfig) .setTimeout(3000); provider.export(); ConsumerConfig<HelloService> consumer = new ConsumerConfig(); consumer.setInterfaceId(HelloService.class.getName()) .setApplication(new ApplicationConfig().setAppName("x-test-client")) // 模拟有可用服务 .setDirectUrl("bolt://127.0.0.1:65534") .setProxy("javassist"); HelloService proxy = consumer.refer(); for (int i = 0; i < 3; i++) { RpcInvokeContext.getContext().setTargetURL("127.0.0.1:13900"); Assert.assertEquals("x-demo-invoke", proxy.sayHello("x-demo-invoke", 1)); } provider.unExport(); consumer.unRefer(); }
Example #17
Source File: FailoverClusterTest.java From sofa-rpc with Apache License 2.0 | 5 votes |
@Test public void testRpcDirectInvokeFromContext() { ServerConfig serverConfig = new ServerConfig() .setProtocol("bolt") .setHost("0.0.0.0") .setPort(13900); ProviderConfig<HelloService> provider = new ProviderConfig(); provider.setInterfaceId(HelloService.class.getName()) .setRef(new HelloServiceImpl("x-demo-invoke")) .setApplication(new ApplicationConfig().setAppName("x-test-server")) .setProxy("javassist") .setSerialization("hessian2") .setServer(serverConfig) .setTimeout(3000); provider.export(); ConsumerConfig<HelloService> consumer = new ConsumerConfig(); consumer.setInterfaceId(HelloService.class.getName()) .setApplication(new ApplicationConfig().setAppName("x-test-client")) .setProxy("javassist"); HelloService proxy = consumer.refer(); for (int i = 0; i < 3; i++) { RpcInvokeContext.getContext().setTargetURL("127.0.0.1:13900"); Assert.assertEquals("x-demo-invoke", proxy.sayHello("x-demo-invoke", 1)); } provider.unExport(); consumer.unRefer(); }
Example #18
Source File: RestLookoutTest.java From sofa-rpc with Apache License 2.0 | 5 votes |
/** * invoke */ @Before public void beforeMethod() { // 发布一个服务,每个请求要执行1秒 ApplicationConfig serverApplication = new ApplicationConfig(); serverApplication.setAppName("TestLookOutServer"); providerConfig = new ProviderConfig<RestService>() .setInterfaceId(RestService.class.getName()) .setRef(new RestServiceImpl()) .setServer(serverConfig) .setBootstrap("rest") //.setParameter(RpcConstants.CONFIG_HIDDEN_KEY_WARNING, "false") .setRegister(false) .setApplication(serverApplication); providerConfig.export(); ApplicationConfig clientApplication = new ApplicationConfig(); clientApplication.setAppName("TestLookOutClient"); consumerConfig = new ConsumerConfig<RestService>() .setInterfaceId(RestService.class.getName()) .setDirectUrl( "rest://127.0.0.1:8802/xyz?uniqueId=&version=1.0&timeout=0&delay=-1&id=rpc-cfg-0&dynamic=true&weight=100&accepts=100000&startTime=1523240755024&appName=" + serverApplication.getAppName() + "&pid=22385&language=java&rpcVer=50300") .setProtocol("rest") .setBootstrap("rest") .setTimeout(30000) .setConnectionNum(5) .setRegister(false) .setApplication(clientApplication); final RestService helloService = consumerConfig.refer(); Assert.assertEquals(helloService.query(11), "hello world !null"); }
Example #19
Source File: ServerC.java From sofa-rpc with Apache License 2.0 | 5 votes |
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(); }
Example #20
Source File: TripleServerRegistryApplication.java From sofa-rpc with Apache License 2.0 | 5 votes |
public static void main(String[] args) { ApplicationConfig applicationConfig = new ApplicationConfig().setAppName("triple-server"); int port = 50051; if (args.length != 0) { LOGGER.debug("first arg is {}", args[0]); port = Integer.valueOf(args[0]); } /* RegistryConfig registryConfig = new RegistryConfig() .setProtocol("zookeeper") .setAddress("127.0.0.1:2181");*/ 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 TripleGreeterImpl()) .setServer(serverConfig) .setRegister(false); // .setRegistry(registryConfig); providerConfig.export(); synchronized (TripleServerRegistryApplication.class) { try { while (true) { TripleServerRegistryApplication.class.wait(); } } catch (InterruptedException e) { LOGGER.error("Exit by Interrupted"); } } }
Example #21
Source File: RestServerMain.java From sofa-rpc with Apache License 2.0 | 5 votes |
public static void main(String[] args) { ApplicationConfig application = new ApplicationConfig().setAppName("test-server"); /* 访问地址: POST http://127.0.0.1:8888/rest/hello/code/name GET http://127.0.0.1:8888/rest/hello/code PUT http://127.0.0.1:8888/rest/hello/code/name DELETE http://127.0.0.1:8888/rest/hello/code GET http://127.0.0.1:8888/rest/get/1234567890 POST http://127.0.0.1:8888/rest/post/1234567890 bodydddddd */ ServerConfig serverConfig = new ServerConfig() .setProtocol("rest") .setPort(8888) .setDaemon(false); ProviderConfig<RestService> providerConfig = new ProviderConfig<RestService>() .setInterfaceId(RestService.class.getName()) .setApplication(application) .setRef(new RestServiceImpl()) .setBootstrap("rest") .setServer(serverConfig) .setRegister(false); providerConfig.export(); LOGGER.warn("started at pid {}", RpcRuntimeContext.PID); }
Example #22
Source File: OnewayClientMain.java From sofa-rpc with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws InterruptedException { ApplicationConfig applicationConfig = new ApplicationConfig().setAppName("oneway-client"); ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>() .setApplication(applicationConfig) .setInterfaceId(HelloService.class.getName()) .setInvokeType(RpcConstants.INVOKER_TYPE_ONEWAY) .setTimeout(50000) .setDirectUrl("bolt://127.0.0.1:22222?appName=oneway-server"); HelloService helloService = consumerConfig.refer(); ConsumerConfig<EchoService> consumerConfig2 = new ConsumerConfig<EchoService>() .setApplication(applicationConfig) .setInterfaceId(EchoService.class.getName()) .setInvokeType(RpcConstants.INVOKER_TYPE_ONEWAY) .setTimeout(50000) .setDirectUrl("bolt://127.0.0.1:22222?appName=oneway-server"); EchoService echoService = consumerConfig2.refer(); LOGGER.warn("started at pid {}", RpcRuntimeContext.PID); while (true) { try { String s1 = helloService.sayHello("xxx", 22); LOGGER.warn("must null :{}", s1); String s2 = echoService.echoStr("yyy"); LOGGER.warn("must null :{}", s2); } catch (Exception e) { LOGGER.error(e.getMessage(), e); } try { Thread.sleep(2000); } catch (Exception ignore) { } } }
Example #23
Source File: DubooServerTest.java From sofa-rpc with Apache License 2.0 | 5 votes |
@Test //同步调用,直连 public void testSync() { try { // 只有1个线程 执行 ServerConfig serverConfig = new ServerConfig() .setStopTimeout(60000) .setPort(20880) .setProtocol("dubbo") .setQueues(100).setCoreThreads(1).setMaxThreads(2); // 发布一个服务,每个请求要执行1秒 ApplicationConfig serverApplacation = new ApplicationConfig(); serverApplacation.setAppName("server"); providerConfig = new ProviderConfig<DemoService>() .setInterfaceId(DemoService.class.getName()) .setRef(new DemoServiceImpl()) .setBootstrap("dubbo") .setServer(serverConfig) // .setParameter(RpcConstants.CONFIG_HIDDEN_KEY_WARNING, "false") .setRegister(false).setApplication(serverApplacation); providerConfig.export(); ApplicationConfig clientApplication = new ApplicationConfig(); clientApplication.setAppName("client"); consumerConfig = new ConsumerConfig<DemoService>() .setInterfaceId(DemoService.class.getName()) .setDirectUrl("dubbo://127.0.0.1:20880") .setBootstrap("dubbo") .setTimeout(30000) .setRegister(false).setProtocol("dubbo").setApplication(clientApplication); final DemoService demoService = consumerConfig.refer(); String result = demoService.sayHello("xxx"); Assert.assertTrue(result.equalsIgnoreCase("hello xxx")); } catch (Exception e) { e.printStackTrace(); Assert.assertTrue(false); } }
Example #24
Source File: GenericServerMain.java From sofa-rpc with Apache License 2.0 | 5 votes |
public static void main(String[] args) { ApplicationConfig applicationConfig = new ApplicationConfig().setAppName("generic-server"); ServerConfig serverConfig2 = new ServerConfig() .setPort(22222) .setDaemon(false); ProviderConfig<TestGenericService> providerConfig = new ProviderConfig<TestGenericService>() .setApplication(applicationConfig) .setInterfaceId(TestGenericService.class.getName()) .setRef(new TestGenericServiceImpl()) .setServer(serverConfig2); providerConfig.export(); }
Example #25
Source File: TripleHessianInvokeTest.java From sofa-rpc with Apache License 2.0 | 4 votes |
@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 = 50062; ServerConfig serverConfig = new ServerConfig() .setProtocol(RpcConstants.PROTOCOL_TYPE_TRIPLE) .setPort(port); TripleHessianInterfaceImpl ref = new TripleHessianInterfaceImpl(); ProviderConfig<TripleHessianInterface> providerConfig = new ProviderConfig<TripleHessianInterface>() .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(); Thread.sleep(10 * 1000); 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); }
Example #26
Source File: FaultBaseTest.java From sofa-rpc with Apache License 2.0 | 4 votes |
@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(); }
Example #27
Source File: ZookeeperRegistryTest.java From sofa-rpc with Apache License 2.0 | 4 votes |
/** * 测试Zookeeper Override Observer * * @throws Exception */ @Test public void testOverrideObserver() throws InterruptedException { ConsumerConfig<?> consumerConfig = new ConsumerConfig(); consumerConfig.setInterfaceId(TEST_SERVICE_NAME) .setUniqueId("unique123Id") .setApplication(new ApplicationConfig().setAppName("test-server")) .setProxy("javassist") .setSubscribe(true) .setSerialization("java") .setInvokeType("sync") .setTimeout(4444); // 订阅Consumer Config CountDownLatch latch = new CountDownLatch(1); MockConfigListener configListener = new MockConfigListener(); configListener.setCountDownLatch(latch); registry.subscribeOverride(consumerConfig, configListener); Map<String, String> attributes = new ConcurrentHashMap<String, String>(); attributes.put(RpcConstants.CONFIG_KEY_TIMEOUT, "3333"); attributes.put(RpcConstants.CONFIG_KEY_APP_NAME, "test-server"); attributes.put(RpcConstants.CONFIG_KEY_SERIALIZATION, "java"); configListener.attrUpdated(attributes); Map<String, String> configData = configListener.getData(); Assert.assertEquals(3, configData.size()); consumerConfig.setInterfaceId(TEST_SERVICE_NAME) .setUniqueId("unique123Id") .setApplication(new ApplicationConfig().setAppName("test-server1")) .setProxy("javassist") .setSubscribe(true) .setSerialization("java") .setInvokeType("sync") .setTimeout(5555); configListener = new MockConfigListener(); configListener.setCountDownLatch(latch); registry.subscribeOverride(consumerConfig, configListener); attributes.put(RpcConstants.CONFIG_KEY_TIMEOUT, "4444"); attributes.put(RpcConstants.CONFIG_KEY_APP_NAME, "test-server2"); configListener.attrUpdated(attributes); configData = configListener.getData(); Assert.assertEquals(3, configData.size()); latch.await(2000, TimeUnit.MILLISECONDS); Assert.assertEquals(3, configData.size()); }
Example #28
Source File: BoltProtobufBaggageSyncTest.java From sofa-rpc with Apache License 2.0 | 4 votes |
@Override void doTest() { ServerConfig serverConfig = new ServerConfig().setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setPort(12299); // C服务的服务端 CSampleServiceImpl refC = new CSampleServiceImpl(); ProviderConfig<SampleService> serviceBeanC = new ProviderConfig<SampleService>(); serviceBeanC.setInterfaceId(SampleService.class.getName()); serviceBeanC.setApplication(new ApplicationConfig().setAppName("CCC")); serviceBeanC.setUniqueId("C1"); serviceBeanC.setRef(refC); serviceBeanC.setServer(serverConfig); serviceBeanC.setRegister(false); serviceBeanC.export(); // D服务的服务端 DSampleServiceImpl refD = new DSampleServiceImpl(); ProviderConfig<SampleService> serviceBeanD = new ProviderConfig<SampleService>(); serviceBeanD.setInterfaceId(SampleService.class.getName()); serviceBeanD.setApplication(new ApplicationConfig().setAppName("DDD")); serviceBeanD.setUniqueId("D1"); serviceBeanD.setRef(refD); serviceBeanD.setServer(serverConfig); serviceBeanD.setRegister(false); serviceBeanD.export(); // B服务里的C服务客户端 ConsumerConfig referenceBeanC = new ConsumerConfig(); referenceBeanC.setApplication(new ApplicationConfig().setAppName("BBB")); referenceBeanC.setInterfaceId(SampleService.class.getName()); referenceBeanC.setUniqueId("C1"); referenceBeanC.setDirectUrl("localhost:12299"); referenceBeanC.setSerialization("protobuf"); referenceBeanC.setTimeout(1000); SampleService sampleServiceC = (SampleService) referenceBeanC.refer(); // B服务里的D服务客户端 ConsumerConfig referenceBeanD = new ConsumerConfig(); referenceBeanD.setApplication(new ApplicationConfig().setAppName("BBB")); referenceBeanD.setInterfaceId(SampleService.class.getName()); referenceBeanD.setUniqueId("D1"); referenceBeanD.setDirectUrl("localhost:12299?p=1&v=4.0"); referenceBeanD.setSerialization("protobuf"); referenceBeanD.setTimeout(1000); SampleService sampleServiceD = (SampleService) referenceBeanD.refer(); // B服务的服务端 BSampleServiceImpl refB = new BSampleServiceImpl(sampleServiceC, sampleServiceD); ProviderConfig<SampleService> ServiceBeanB = new ProviderConfig<SampleService>(); ServiceBeanB.setInterfaceId(SampleService.class.getName()); ServiceBeanB.setApplication(new ApplicationConfig().setAppName("BBB")); ServiceBeanB.setUniqueId("B1"); ServiceBeanB.setRef(refB); ServiceBeanB.setServer(serverConfig); ServiceBeanB.setRegister(false); ServiceBeanB.export(); // A 服务 ConsumerConfig referenceBeanA = new ConsumerConfig(); referenceBeanA.setApplication(new ApplicationConfig().setAppName("AAA")); referenceBeanA.setUniqueId("B1"); referenceBeanA.setInterfaceId(SampleService.class.getName()); referenceBeanA.setDirectUrl("localhost:12299"); referenceBeanA.setSerialization("protobuf"); referenceBeanA.setTimeout(3000); SampleService service = (SampleService) referenceBeanA.refer(); // 开始测试 RpcInvokeContext context = RpcInvokeContext.getContext(); context.putRequestBaggage("reqBaggageB", "a2bbb"); context.putRequestBaggage("reqBaggageC", "a2ccc"); context.putRequestBaggage("reqBaggageD", "a2ddd"); EchoRequest request = EchoRequest.newBuilder().setGroup(Group.A).setName("xxx").build(); EchoResponse ret = service.echoObj(request); Assert.assertEquals(ret.getMessage(), "hello world chello world d"); Assert.assertEquals(refB.getReqBaggage(), "a2bbb"); Assert.assertEquals(refC.getReqBaggage(), "a2ccc"); Assert.assertEquals(refD.getReqBaggage(), "a2ddd"); Assert.assertEquals(context.getResponseBaggage("respBaggageB"), "b2aaa"); Assert.assertEquals(context.getResponseBaggage("respBaggageC"), "c2aaa"); Assert.assertEquals(context.getResponseBaggage("respBaggageD"), "d2aaa"); Assert.assertNull(context.getResponseBaggage("respBaggageB_force")); Assert.assertNull(context.getResponseBaggage("respBaggageC_force")); Assert.assertNull(context.getResponseBaggage("respBaggageD_force")); RpcInvokeContext.removeContext(); context = RpcInvokeContext.getContext(); ret = service.echoObj(request); Assert.assertEquals(ret.getMessage(), "hello world chello world d"); Assert.assertNull(refB.getReqBaggage()); Assert.assertNull(refC.getReqBaggage()); Assert.assertNull(refD.getReqBaggage()); Assert.assertNull(context.getResponseBaggage("respBaggageB")); Assert.assertNull(context.getResponseBaggage("respBaggageC")); Assert.assertNull(context.getResponseBaggage("respBaggageD")); Assert.assertEquals(context.getResponseBaggage("respBaggageB_force"), "b2aaaff"); Assert.assertEquals(context.getResponseBaggage("respBaggageC_force"), "c2aaaff"); Assert.assertEquals(context.getResponseBaggage("respBaggageD_force"), "d2aaaff"); }
Example #29
Source File: DubooServerTest.java From sofa-rpc with Apache License 2.0 | 4 votes |
@Test //单向调用 public void testOneWay() { // 只有1个线程 执行 ServerConfig serverConfig = new ServerConfig() .setStopTimeout(60000) .setPort(20880) .setProtocol("dubbo") .setQueues(100).setCoreThreads(1).setMaxThreads(2); // 发布一个服务,每个请求要执行1秒 ApplicationConfig serverApplacation = new ApplicationConfig(); serverApplacation.setAppName("server"); providerConfig = new ProviderConfig<DemoService>() .setInterfaceId(DemoService.class.getName()) .setRef(new DemoServiceImpl()) .setServer(serverConfig) .setBootstrap("dubbo") // .setParameter(RpcConstants.CONFIG_HIDDEN_KEY_WARNING, "false") .setRegister(false).setApplication(serverApplacation); providerConfig.export(); ApplicationConfig clientApplication = new ApplicationConfig(); clientApplication.setAppName("client"); List<MethodConfig> methodConfigs = new ArrayList<MethodConfig>(); MethodConfig methodConfig = new MethodConfig(); methodConfig.setInvokeType(RpcConstants.INVOKER_TYPE_ONEWAY); methodConfig.setName("sayHello"); methodConfigs.add(methodConfig); consumerConfig = new ConsumerConfig<DemoService>() .setInterfaceId(DemoService.class.getName()) .setDirectUrl("dubbo://127.0.0.1:20880") .setTimeout(30000) .setRegister(false) .setProtocol("dubbo") .setBootstrap("dubbo") .setApplication(clientApplication) .setInvokeType(RpcConstants.INVOKER_TYPE_ONEWAY) .setMethods(methodConfigs); final DemoService demoService = consumerConfig.refer(); String tmp = demoService.sayHello("xxx"); Assert.assertEquals(null, tmp); }
Example #30
Source File: BaggageSyncTest.java From sofa-rpc with Apache License 2.0 | 4 votes |
@Override void doTest() { ServerConfig serverConfig = new ServerConfig().setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setPort(12299); // C服务的服务端 CSampleServiceImpl refC = new CSampleServiceImpl(); ProviderConfig<SampleService> serviceBeanC = new ProviderConfig<SampleService>(); serviceBeanC.setInterfaceId(SampleService.class.getName()); serviceBeanC.setApplication(new ApplicationConfig().setAppName("CCC")); serviceBeanC.setUniqueId("C1"); serviceBeanC.setRef(refC); serviceBeanC.setServer(serverConfig); serviceBeanC.setRegister(false); serviceBeanC.export(); // D服务的服务端 DSampleServiceImpl refD = new DSampleServiceImpl(); ProviderConfig<SampleService> serviceBeanD = new ProviderConfig<SampleService>(); serviceBeanD.setInterfaceId(SampleService.class.getName()); serviceBeanD.setApplication(new ApplicationConfig().setAppName("DDD")); serviceBeanD.setUniqueId("D1"); serviceBeanD.setRef(refD); serviceBeanD.setServer(serverConfig); serviceBeanD.setRegister(false); serviceBeanD.export(); // B服务里的C服务客户端 ConsumerConfig referenceBeanC = new ConsumerConfig(); referenceBeanC.setApplication(new ApplicationConfig().setAppName("BBB")); referenceBeanC.setInterfaceId(SampleService.class.getName()); referenceBeanC.setUniqueId("C1"); referenceBeanC.setDirectUrl("localhost:12299"); referenceBeanC.setTimeout(1000); SampleService sampleServiceC = (SampleService) referenceBeanC.refer(); // B服务里的D服务客户端 ConsumerConfig referenceBeanD = new ConsumerConfig(); referenceBeanD.setApplication(new ApplicationConfig().setAppName("BBB")); referenceBeanD.setInterfaceId(SampleService.class.getName()); referenceBeanD.setUniqueId("D1"); referenceBeanD.setDirectUrl("localhost:12299?p=1&v=4.0"); referenceBeanD.setTimeout(1000); SampleService sampleServiceD = (SampleService) referenceBeanD.refer(); // B服务的服务端 BSampleServiceImpl refB = new BSampleServiceImpl(sampleServiceC, sampleServiceD); ProviderConfig<SampleService> ServiceBeanB = new ProviderConfig<SampleService>(); ServiceBeanB.setInterfaceId(SampleService.class.getName()); ServiceBeanB.setApplication(new ApplicationConfig().setAppName("BBB")); ServiceBeanB.setUniqueId("B1"); ServiceBeanB.setRef(refB); ServiceBeanB.setServer(serverConfig); ServiceBeanB.setRegister(false); ServiceBeanB.export(); // A 服务 ConsumerConfig referenceBeanA = new ConsumerConfig(); referenceBeanA.setApplication(new ApplicationConfig().setAppName("AAA")); referenceBeanA.setUniqueId("B1"); referenceBeanA.setInterfaceId(SampleService.class.getName()); referenceBeanA.setDirectUrl("localhost:12299"); referenceBeanA.setTimeout(3000); SampleService service = (SampleService) referenceBeanA.refer(); // 开始测试 RpcInvokeContext context = RpcInvokeContext.getContext(); context.putRequestBaggage("reqBaggageB", "a2bbb"); context.putRequestBaggage("reqBaggageC", "a2ccc"); context.putRequestBaggage("reqBaggageD", "a2ddd"); String ret = service.hello(); Assert.assertEquals(ret, "hello world chello world d"); Assert.assertEquals(refB.getReqBaggage(), "a2bbb"); Assert.assertEquals(refC.getReqBaggage(), "a2ccc"); Assert.assertEquals(refD.getReqBaggage(), "a2ddd"); Assert.assertEquals(context.getResponseBaggage("respBaggageB"), "b2aaa"); Assert.assertEquals(context.getResponseBaggage("respBaggageC"), "c2aaa"); Assert.assertEquals(context.getResponseBaggage("respBaggageD"), "d2aaa"); Assert.assertNull(context.getResponseBaggage("respBaggageB_force")); Assert.assertNull(context.getResponseBaggage("respBaggageC_force")); Assert.assertNull(context.getResponseBaggage("respBaggageD_force")); RpcInvokeContext.removeContext(); context = RpcInvokeContext.getContext(); ret = service.hello(); Assert.assertEquals(ret, "hello world chello world d"); Assert.assertNull(refB.getReqBaggage()); Assert.assertNull(refC.getReqBaggage()); Assert.assertNull(refD.getReqBaggage()); Assert.assertNull(context.getResponseBaggage("respBaggageB")); Assert.assertNull(context.getResponseBaggage("respBaggageC")); Assert.assertNull(context.getResponseBaggage("respBaggageD")); Assert.assertEquals(context.getResponseBaggage("respBaggageB_force"), "b2aaaff"); Assert.assertEquals(context.getResponseBaggage("respBaggageC_force"), "c2aaaff"); Assert.assertEquals(context.getResponseBaggage("respBaggageD_force"), "d2aaaff"); }