From b87b9a2ff98bbeac156e6fea8213a83e27148d2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=81=AA=E6=B4=8B?= <1601081222@qq.com> Date: Fri, 21 Jun 2024 17:15:55 +0800 Subject: [PATCH] temp test --- .../dubbo-demo-xml-consumer/pom.xml | 10 +++ .../apache/dubbo/demo/consumer/ZKTools.java | 69 +++++++++++++++++++ .../src/test/java/DemoServiceIT.java | 57 +++++++++++++++ .../dubbo/demo/provider/DemoServiceImpl.java | 2 +- .../demo/provider/GreetingServiceImpl.java | 3 +- .../main/resources/spring/dubbo-provider.xml | 4 ++ 6 files changed, 143 insertions(+), 2 deletions(-) create mode 100644 dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-consumer/src/main/java/org/apache/dubbo/demo/consumer/ZKTools.java create mode 100644 dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-consumer/src/test/java/DemoServiceIT.java diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-consumer/pom.xml b/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-consumer/pom.xml index 25a3ee5e53..bcd363101b 100644 --- a/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-consumer/pom.xml +++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-consumer/pom.xml @@ -114,5 +114,15 @@ org.apache.logging.log4j log4j-slf4j-impl + + junit + junit + test + + + org.springframework + spring-test + test + diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-consumer/src/main/java/org/apache/dubbo/demo/consumer/ZKTools.java b/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-consumer/src/main/java/org/apache/dubbo/demo/consumer/ZKTools.java new file mode 100644 index 0000000000..57b411e1de --- /dev/null +++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-consumer/src/main/java/org/apache/dubbo/demo/consumer/ZKTools.java @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.dubbo.demo.consumer; + +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.retry.ExponentialBackoffRetry; + +public class ZKTools { + private static String zookeeperHost = System.getProperty("zookeeper.address", "127.0.0.1"); + private static CuratorFramework client; + + public static void main(String[] args) throws Exception { + initClient(); + generateAppevelRouter(); + } + + public static void initClient() { + client = CuratorFrameworkFactory.newClient(zookeeperHost + ":2181", 60 * 1000, 60 * 1000, + new ExponentialBackoffRetry(1000, 3)); + client.start(); + } + + public static void generateAppevelRouter() { + String str = "" + + "---\n" + + "scope: application\n" + + "force: true\n" + + "runtime: true\n" + + "enabled: true\n" + + "priority: 2\n" + + "key: demo-consumer\n" + + "conditions:\n" + + " - interface=org.apache.dubbo.demo.GreetingService=>address=*:20880\n" + + " - interface=org.apache.dubbo.demo.DemoService=>address=*:20881\n" + + "..."; + + System.out.println(str); + + try { + String path = "/dubbo/config/dubbo/demo-consumer.condition-router"; + if (client.checkExists().forPath(path) == null) { + client.create().creatingParentsIfNeeded().forPath(path); + } + setData(path, str); + } catch (Exception e) { + e.printStackTrace(); + } + } + + private static void setData(String path, String data) throws Exception { + client.setData().forPath(path, data.getBytes()); + } + +} diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-consumer/src/test/java/DemoServiceIT.java b/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-consumer/src/test/java/DemoServiceIT.java new file mode 100644 index 0000000000..d64ae9c681 --- /dev/null +++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-consumer/src/test/java/DemoServiceIT.java @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.dubbo.demo.DemoService; +import org.apache.dubbo.demo.GreetingService; +import org.apache.dubbo.demo.consumer.ZKTools; + +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.jupiter.api.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = {"classpath:/spring/dubbo-consumer.xml"}) +public class DemoServiceIT { + @Autowired + private DemoService demoService; + + @Autowired + private GreetingService greetingService; + + @BeforeClass + public static void setUp() throws Exception { + ZKTools.initClient(); + ZKTools.generateAppevelRouter(); + Thread.sleep(2000); + } + + @Test + public void testDemoService() throws Exception { + String result = demoService.sayHello("world"); + Assert.assertTrue(result.contains("20880")); + } + + @Test + public void testDemoService2() throws Exception { + String result = greetingService.hello(); + Assert.assertTrue(result.contains("20881")); + } +} diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/java/org/apache/dubbo/demo/provider/DemoServiceImpl.java b/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/java/org/apache/dubbo/demo/provider/DemoServiceImpl.java index 3467051454..602e7830e6 100644 --- a/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/java/org/apache/dubbo/demo/provider/DemoServiceImpl.java +++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/java/org/apache/dubbo/demo/provider/DemoServiceImpl.java @@ -37,7 +37,7 @@ public class DemoServiceImpl implements DemoService { e.printStackTrace(); } return "Hello " + name + ", response from provider: " - + RpcContext.getServiceContext().getLocalAddress(); + + RpcContext.getServiceContext().getLocalAddress() + RpcContext.getServiceContext().getLocalPort(); } @Override diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/java/org/apache/dubbo/demo/provider/GreetingServiceImpl.java b/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/java/org/apache/dubbo/demo/provider/GreetingServiceImpl.java index cc1b5de413..83c5698194 100644 --- a/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/java/org/apache/dubbo/demo/provider/GreetingServiceImpl.java +++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/java/org/apache/dubbo/demo/provider/GreetingServiceImpl.java @@ -17,6 +17,7 @@ package org.apache.dubbo.demo.provider; import org.apache.dubbo.demo.GreetingService; +import org.apache.dubbo.rpc.RpcContext; /** * @@ -24,6 +25,6 @@ import org.apache.dubbo.demo.GreetingService; public class GreetingServiceImpl implements GreetingService { @Override public String hello() { - return "Greetings!"; + return "Greetings!" + RpcContext.getServiceContext().getLocalPort(); } } diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/resources/spring/dubbo-provider.xml b/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/resources/spring/dubbo-provider.xml index ad402bc068..a9b8686be8 100644 --- a/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/resources/spring/dubbo-provider.xml +++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/resources/spring/dubbo-provider.xml @@ -32,6 +32,10 @@ + + + +