From 081866613172fff525f482550780d5b3f9983074 Mon Sep 17 00:00:00 2001 From: "shengwei.psw" Date: Thu, 19 Jan 2023 20:14:36 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E5=A2=9E=E5=8A=A0OpenSergo=E6=B5=81?= =?UTF-8?q?=E9=87=8F=E8=B7=AF=E7=94=B1=E8=83=BD=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dubbo-samples-opensergo/README.md | 27 ++ .../case-configuration.yml | 68 +++++ .../case-versions.conf | 25 ++ 4-governance/dubbo-samples-opensergo/pom.xml | 163 ++++++++++++ .../samples/governance/BasicConsumer.java | 62 +++++ .../samples/governance/BasicProvider.java | 38 +++ .../governance/BasicProviderOtherPort.java | 36 +++ .../samples/governance/EmbeddedZooKeeper.java | 250 ++++++++++++++++++ .../dubbo/samples/governance/RuleUtil.java | 82 ++++++ .../samples/governance/api/DemoService.java | 26 ++ .../samples/governance/api/DemoService2.java | 26 ++ .../governance/impl/DemoServiceImpl.java | 36 +++ .../governance/impl/DemoServiceImpl2.java | 36 +++ .../src/main/resources/log4j.properties | 25 ++ .../resources/spring/dubbo-demo-consumer.xml | 37 +++ .../spring/dubbo-demo-provider-20880.xml | 47 ++++ .../spring/dubbo-demo-provider-20881.xml | 47 ++++ .../samples/governance/DemoServiceIT.java | 73 +++++ 18 files changed, 1104 insertions(+) create mode 100644 4-governance/dubbo-samples-opensergo/README.md create mode 100644 4-governance/dubbo-samples-opensergo/case-configuration.yml create mode 100644 4-governance/dubbo-samples-opensergo/case-versions.conf create mode 100644 4-governance/dubbo-samples-opensergo/pom.xml create mode 100644 4-governance/dubbo-samples-opensergo/src/main/java/org/apache/dubbo/samples/governance/BasicConsumer.java create mode 100644 4-governance/dubbo-samples-opensergo/src/main/java/org/apache/dubbo/samples/governance/BasicProvider.java create mode 100644 4-governance/dubbo-samples-opensergo/src/main/java/org/apache/dubbo/samples/governance/BasicProviderOtherPort.java create mode 100644 4-governance/dubbo-samples-opensergo/src/main/java/org/apache/dubbo/samples/governance/EmbeddedZooKeeper.java create mode 100644 4-governance/dubbo-samples-opensergo/src/main/java/org/apache/dubbo/samples/governance/RuleUtil.java create mode 100644 4-governance/dubbo-samples-opensergo/src/main/java/org/apache/dubbo/samples/governance/api/DemoService.java create mode 100644 4-governance/dubbo-samples-opensergo/src/main/java/org/apache/dubbo/samples/governance/api/DemoService2.java create mode 100644 4-governance/dubbo-samples-opensergo/src/main/java/org/apache/dubbo/samples/governance/impl/DemoServiceImpl.java create mode 100644 4-governance/dubbo-samples-opensergo/src/main/java/org/apache/dubbo/samples/governance/impl/DemoServiceImpl2.java create mode 100644 4-governance/dubbo-samples-opensergo/src/main/resources/log4j.properties create mode 100644 4-governance/dubbo-samples-opensergo/src/main/resources/spring/dubbo-demo-consumer.xml create mode 100644 4-governance/dubbo-samples-opensergo/src/main/resources/spring/dubbo-demo-provider-20880.xml create mode 100644 4-governance/dubbo-samples-opensergo/src/main/resources/spring/dubbo-demo-provider-20881.xml create mode 100644 4-governance/dubbo-samples-opensergo/src/test/java/org/apache/dubbo/samples/governance/DemoServiceIT.java diff --git a/4-governance/dubbo-samples-opensergo/README.md b/4-governance/dubbo-samples-opensergo/README.md new file mode 100644 index 0000000000..171122114b --- /dev/null +++ b/4-governance/dubbo-samples-opensergo/README.md @@ -0,0 +1,27 @@ +# governance dubbo using tag router +1. step1 run `BasicProvider.java` + +> it will start an zookeeper and register two service with dubbo port 20880 +2. step2 run `BasicProviderOtherPort.java` +> it will register another two service with dubbo port 20881 +3. step3 run `RuleUtil.java`, +> it will read the route rule defined by [dubbo-routers-tag.yml](src%2Fmain%2Fresources%2Fdubbo-routers-tag.yml) +```yaml +--- +force: false +runtime: true +enabled: true +priority: 1 +key: governance-tagrouter-provider +tags: +- name: tag1 + addresses: ["*.*.*.*:20881"] +- name: tag2 + addresses: ["*.*.*.*:20880"] +... +``` + +6. step4 run `BasicConsumer.java` +> it will consume service by rule defined by yml file +> * All consumer taged by `tag1` come from provider in port 20881 +> * All consumer taged by `tag2` come from provider in port 20880 \ No newline at end of file diff --git a/4-governance/dubbo-samples-opensergo/case-configuration.yml b/4-governance/dubbo-samples-opensergo/case-configuration.yml new file mode 100644 index 0000000000..00f10ec711 --- /dev/null +++ b/4-governance/dubbo-samples-opensergo/case-configuration.yml @@ -0,0 +1,68 @@ +# 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. + +services: + zookeeper: + image: zookeeper:latest + + dubbo-samples-tagrouter1: + type: app + basedir: . + mainClass: org.apache.dubbo.samples.governance.BasicProvider + systemProps: + - zookeeper.address=zookeeper + - zookeeper.port=2181 + - dubbo.port=20880 + waitPortsBeforeRun: + - zookeeper:2181 + checkPorts: + - 20880 + checkLog: "dubbo service started" + depends_on: + - zookeeper + + dubbo-samples-tagrouter2: + type: app + basedir: . + mainClass: org.apache.dubbo.samples.governance.BasicProvider + systemProps: + - zookeeper.address=zookeeper + - zookeeper.port=2181 + - dubbo.port=20881 + waitPortsBeforeRun: + - zookeeper:2181 + checkPorts: + - 20881 + checkLog: "dubbo service started" + depends_on: + - zookeeper + + dubbo-samples-tagrouter-test: + type: test + basedir: . + tests: + - "**/*IT.class" + systemProps: + - zookeeper.address=zookeeper + - zookeeper.port=2181 + waitPortsBeforeRun: + - zookeeper:2181 + - dubbo-samples-tagrouter1:20880 + - dubbo-samples-tagrouter2:20881 + depends_on: + - zookeeper + - dubbo-samples-tagrouter1 + - dubbo-samples-tagrouter2 diff --git a/4-governance/dubbo-samples-opensergo/case-versions.conf b/4-governance/dubbo-samples-opensergo/case-versions.conf new file mode 100644 index 0000000000..20c45c863f --- /dev/null +++ b/4-governance/dubbo-samples-opensergo/case-versions.conf @@ -0,0 +1,25 @@ +# +# +# 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. +# + + +# Supported component versions of the test case + +# Spring app +dubbo.version=2.7*, 3.* +spring.version=4.*, 5.* +java.version= [<= 11] diff --git a/4-governance/dubbo-samples-opensergo/pom.xml b/4-governance/dubbo-samples-opensergo/pom.xml new file mode 100644 index 0000000000..67b26ff258 --- /dev/null +++ b/4-governance/dubbo-samples-opensergo/pom.xml @@ -0,0 +1,163 @@ + + + + + org.apache.dubbo + 1.0-SNAPSHOT + + 4.0.0 + + dubbo-samples-opensergo + Dubbo Samples OpenSergo + Dubbo Samples OpenSergo + + 1.8 + 1.8 + 3.2.0-beta.3-SNAPSHOT + 4.3.29.RELEASE + 4.13.1 + 3.7.0 + + + + + + org.springframework + spring-framework-bom + ${spring.version} + pom + import + + + + org.apache.dubbo + dubbo-bom + ${dubbo.version} + pom + import + + + + org.apache.dubbo + dubbo-dependencies-zookeeper + ${dubbo.version} + pom + + + + org.apache.dubbo + dubbo-opensergo + ${dubbo.version} + pom + + + + junit + junit + ${junit.version} + + + io.envoyproxy.controlplane + api + 1.0.36 + + + + + + + org.apache.dubbo + dubbo + + + + org.apache.dubbo + dubbo-dependencies-zookeeper + pom + + + + org.apache.dubbo + dubbo-opensergo + pom + + + + junit + junit + test + + + + org.springframework + spring-test + test + + + + + + + javax.annotation + + [1.11,) + + + + javax.annotation + javax.annotation-api + 1.3.2 + + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + ${source.level} + ${target.level} + + + + + + + + apache.snapshots.https + Apache Development Snapshot Repository + https://repository.apache.org/content/repositories/snapshots + default + + false + + + true + daily + + + + diff --git a/4-governance/dubbo-samples-opensergo/src/main/java/org/apache/dubbo/samples/governance/BasicConsumer.java b/4-governance/dubbo-samples-opensergo/src/main/java/org/apache/dubbo/samples/governance/BasicConsumer.java new file mode 100644 index 0000000000..5e7334100d --- /dev/null +++ b/4-governance/dubbo-samples-opensergo/src/main/java/org/apache/dubbo/samples/governance/BasicConsumer.java @@ -0,0 +1,62 @@ +/* + * + * 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.samples.governance; + +import org.apache.dubbo.rpc.RpcContext; +import org.apache.dubbo.samples.governance.api.DemoService; +import org.apache.dubbo.samples.governance.api.DemoService2; + +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import static org.apache.dubbo.common.constants.CommonConstants.TAG_KEY; +import static org.apache.dubbo.rpc.Constants.FORCE_USE_TAG; + +public class BasicConsumer { + + public static void main(String[] args) { + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-demo-consumer.xml"); + context.start(); + DemoService demoService = context.getBean("demoService", DemoService.class); + DemoService2 demoService2 = context.getBean("demoService2", DemoService2.class); + + RpcContext.getContext().setAttachment("x-mse-tag", "v3"); + String hello = demoService.sayHello("world"); + +// System.out.println(hello); + + String hello2 = demoService2.sayHello("world again"); + System.out.println(hello2); + + + hello = demoService.sayHello("world"); + System.out.println(hello); + + + hello = demoService.sayHello("world"); + System.out.println(hello); + hello = demoService.sayHello("world"); + System.out.println(hello); + hello = demoService.sayHello("world"); + System.out.println(hello); + hello = demoService.sayHello("world"); + System.out.println(hello); + + } +} diff --git a/4-governance/dubbo-samples-opensergo/src/main/java/org/apache/dubbo/samples/governance/BasicProvider.java b/4-governance/dubbo-samples-opensergo/src/main/java/org/apache/dubbo/samples/governance/BasicProvider.java new file mode 100644 index 0000000000..bf3a4a3263 --- /dev/null +++ b/4-governance/dubbo-samples-opensergo/src/main/java/org/apache/dubbo/samples/governance/BasicProvider.java @@ -0,0 +1,38 @@ +/* + * + * 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.samples.governance; + +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import java.util.concurrent.CountDownLatch; + +public class BasicProvider { + + public static void main(String[] args) throws Exception { + new EmbeddedZooKeeper(2181, false).start(); + + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-demo-provider-20880.xml"); + context.start(); + + System.out.println("dubbo service started"); + new CountDownLatch(1).await(); + } + +} diff --git a/4-governance/dubbo-samples-opensergo/src/main/java/org/apache/dubbo/samples/governance/BasicProviderOtherPort.java b/4-governance/dubbo-samples-opensergo/src/main/java/org/apache/dubbo/samples/governance/BasicProviderOtherPort.java new file mode 100644 index 0000000000..b80e003ec3 --- /dev/null +++ b/4-governance/dubbo-samples-opensergo/src/main/java/org/apache/dubbo/samples/governance/BasicProviderOtherPort.java @@ -0,0 +1,36 @@ +/* + * + * 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.samples.governance; + +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import java.util.concurrent.CountDownLatch; + +public class BasicProviderOtherPort { + + public static void main(String[] args) throws Exception { + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-demo-provider-20881.xml"); + context.start(); + + System.out.println("dubbo service started"); + new CountDownLatch(1).await(); + } + +} diff --git a/4-governance/dubbo-samples-opensergo/src/main/java/org/apache/dubbo/samples/governance/EmbeddedZooKeeper.java b/4-governance/dubbo-samples-opensergo/src/main/java/org/apache/dubbo/samples/governance/EmbeddedZooKeeper.java new file mode 100644 index 0000000000..c4ed039972 --- /dev/null +++ b/4-governance/dubbo-samples-opensergo/src/main/java/org/apache/dubbo/samples/governance/EmbeddedZooKeeper.java @@ -0,0 +1,250 @@ +/* + * Copyright 2014 the original author or authors. + * + * Licensed 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.samples.governance; + +import org.apache.zookeeper.server.ServerConfig; +import org.apache.zookeeper.server.ZooKeeperServerMain; +import org.apache.zookeeper.server.quorum.QuorumPeerConfig; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.SmartLifecycle; +import org.springframework.util.ErrorHandler; +import org.springframework.util.SocketUtils; + +import java.io.File; +import java.lang.reflect.Method; +import java.util.Properties; +import java.util.UUID; + +/** + * from: https://github.com/spring-projects/spring-xd/blob/v1.3.1.RELEASE/spring-xd-dirt/src/main/java/org/springframework/xd/dirt/zookeeper/ZooKeeperUtils.java + *

+ * Helper class to start an embedded instance of standalone (non clustered) ZooKeeper. + *

+ * NOTE: at least an external standalone server (if not an ensemble) are recommended, even for + * {@link org.springframework.xd.dirt.server.singlenode.SingleNodeApplication} + * + * @author Patrick Peralta + * @author Mark Fisher + * @author David Turanski + */ +public class EmbeddedZooKeeper implements SmartLifecycle { + + /** + * Logger. + */ + private static final Logger logger = LoggerFactory.getLogger(EmbeddedZooKeeper.class); + + /** + * ZooKeeper client port. This will be determined dynamically upon startup. + */ + private final int clientPort; + + /** + * Whether to auto-start. Default is true. + */ + private boolean autoStartup = true; + + /** + * Lifecycle phase. Default is 0. + */ + private int phase = 0; + + /** + * Thread for running the ZooKeeper server. + */ + private volatile Thread zkServerThread; + + /** + * ZooKeeper server. + */ + private volatile ZooKeeperServerMain zkServer; + + /** + * {@link ErrorHandler} to be invoked if an Exception is thrown from the ZooKeeper server thread. + */ + private ErrorHandler errorHandler; + + private boolean daemon = true; + + /** + * Construct an EmbeddedZooKeeper with a random port. + */ + public EmbeddedZooKeeper() { + clientPort = SocketUtils.findAvailableTcpPort(); + } + + /** + * Construct an EmbeddedZooKeeper with the provided port. + * + * @param clientPort port for ZooKeeper server to bind to + */ + public EmbeddedZooKeeper(int clientPort, boolean daemon) { + this.clientPort = clientPort; + this.daemon = daemon; + } + + /** + * Returns the port that clients should use to connect to this embedded server. + * + * @return dynamically determined client port + */ + public int getClientPort() { + return this.clientPort; + } + + /** + * Specify whether to start automatically. Default is true. + * + * @param autoStartup whether to start automatically + */ + public void setAutoStartup(boolean autoStartup) { + this.autoStartup = autoStartup; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean isAutoStartup() { + return this.autoStartup; + } + + /** + * Specify the lifecycle phase for the embedded server. + * + * @param phase the lifecycle phase + */ + public void setPhase(int phase) { + this.phase = phase; + } + + /** + * {@inheritDoc} + */ + @Override + public int getPhase() { + return this.phase; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean isRunning() { + return (zkServerThread != null); + } + + /** + * Start the ZooKeeper server in a background thread. + *

+ * Register an error handler via {@link #setErrorHandler} in order to handle + * any exceptions thrown during startup or execution. + */ + @Override + public synchronized void start() { + if (zkServerThread == null) { + zkServerThread = new Thread(new ServerRunnable(), "ZooKeeper Server Starter"); + zkServerThread.setDaemon(daemon); + zkServerThread.start(); + } + } + + /** + * Shutdown the ZooKeeper server. + */ + @Override + public synchronized void stop() { + if (zkServerThread != null) { + // The shutdown method is protected...thus this hack to invoke it. + // This will log an exception on shutdown; see + // https://issues.apache.org/jira/browse/ZOOKEEPER-1873 for details. + try { + Method shutdown = ZooKeeperServerMain.class.getDeclaredMethod("shutdown"); + shutdown.setAccessible(true); + shutdown.invoke(zkServer); + } catch (Exception e) { + throw new RuntimeException(e); + } + + // It is expected that the thread will exit after + // the server is shutdown; this will block until + // the shutdown is complete. + try { + zkServerThread.join(5000); + zkServerThread = null; + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + logger.warn("Interrupted while waiting for embedded ZooKeeper to exit"); + // abandoning zk thread + zkServerThread = null; + } + } + } + + /** + * Stop the server if running and invoke the callback when complete. + */ + @Override + public void stop(Runnable callback) { + stop(); + callback.run(); + } + + /** + * Provide an {@link ErrorHandler} to be invoked if an Exception is thrown from the ZooKeeper server thread. If none + * is provided, only error-level logging will occur. + * + * @param errorHandler the {@link ErrorHandler} to be invoked + */ + public void setErrorHandler(ErrorHandler errorHandler) { + this.errorHandler = errorHandler; + } + + /** + * Runnable implementation that starts the ZooKeeper server. + */ + private class ServerRunnable implements Runnable { + + @Override + public void run() { + try { + Properties properties = new Properties(); + File file = new File(System.getProperty("java.io.tmpdir") + + File.separator + UUID.randomUUID()); + file.deleteOnExit(); + properties.setProperty("dataDir", file.getAbsolutePath()); + properties.setProperty("clientPort", String.valueOf(clientPort)); + + QuorumPeerConfig quorumPeerConfig = new QuorumPeerConfig(); + quorumPeerConfig.parseProperties(properties); + + zkServer = new ZooKeeperServerMain(); + ServerConfig configuration = new ServerConfig(); + configuration.readFrom(quorumPeerConfig); + + zkServer.runFromConfig(configuration); + } catch (Exception e) { + if (errorHandler != null) { + errorHandler.handleError(e); + } else { + logger.error("Exception running embedded ZooKeeper", e); + } + } + } + } + +} diff --git a/4-governance/dubbo-samples-opensergo/src/main/java/org/apache/dubbo/samples/governance/RuleUtil.java b/4-governance/dubbo-samples-opensergo/src/main/java/org/apache/dubbo/samples/governance/RuleUtil.java new file mode 100644 index 0000000000..53d4a2da6b --- /dev/null +++ b/4-governance/dubbo-samples-opensergo/src/main/java/org/apache/dubbo/samples/governance/RuleUtil.java @@ -0,0 +1,82 @@ +/* + * 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.samples.governance; + +import org.apache.dubbo.common.utils.StringUtils; + +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.retry.ExponentialBackoffRetry; + +import java.io.IOException; +import java.io.InputStream; + +public class RuleUtil { + 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(); + generateRule(); + System.in.read(); + deleteRule(); + } + + public static void initClient() { + client = CuratorFrameworkFactory.newClient(zookeeperHost + ":2181", 60 * 1000, 60 * 1000, + new ExponentialBackoffRetry(1000, 3)); + client.start(); + } + + public static void generateRule() { + try (InputStream yamlStream = RuleUtil.class.getResourceAsStream("/dubbo-routers-tag.yml")) { + String path = "/dubbo/config/dubbo/governance-tagrouter-provider.tag-router"; + if (client.checkExists().forPath(path) == null) { + client.create().creatingParentsIfNeeded().forPath(path); + } + setData(path, streamToString(yamlStream)); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static void deleteRule() throws Exception { + String path = "/dubbo/config/dubbo/governance-tagrouter-provider.tag-router"; + if (client.checkExists().forPath(path) == null) { + client.create().creatingParentsIfNeeded().forPath(path); + } + setData(path, ""); + } + + private static String streamToString(InputStream stream) throws IOException { + byte[] bytes = new byte[stream.available()]; + stream.read(bytes); + return new String(bytes); + } + + private static void setData(String path, String data) throws Exception { + client.setData().forPath(path, data.getBytes()); + } + + private static String pathToKey(String path) { + if (StringUtils.isEmpty(path)) { + return path; + } + return path.replace("/dubbo/config/", "").replaceAll("/", "."); + } + +} diff --git a/4-governance/dubbo-samples-opensergo/src/main/java/org/apache/dubbo/samples/governance/api/DemoService.java b/4-governance/dubbo-samples-opensergo/src/main/java/org/apache/dubbo/samples/governance/api/DemoService.java new file mode 100644 index 0000000000..bde5f6eb6a --- /dev/null +++ b/4-governance/dubbo-samples-opensergo/src/main/java/org/apache/dubbo/samples/governance/api/DemoService.java @@ -0,0 +1,26 @@ +/* + * + * 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.samples.governance.api; + +public interface DemoService { + + String sayHello(String name); + +} diff --git a/4-governance/dubbo-samples-opensergo/src/main/java/org/apache/dubbo/samples/governance/api/DemoService2.java b/4-governance/dubbo-samples-opensergo/src/main/java/org/apache/dubbo/samples/governance/api/DemoService2.java new file mode 100644 index 0000000000..828d87c919 --- /dev/null +++ b/4-governance/dubbo-samples-opensergo/src/main/java/org/apache/dubbo/samples/governance/api/DemoService2.java @@ -0,0 +1,26 @@ +/* + * + * 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.samples.governance.api; + +public interface DemoService2 { + + String sayHello(String name); + +} diff --git a/4-governance/dubbo-samples-opensergo/src/main/java/org/apache/dubbo/samples/governance/impl/DemoServiceImpl.java b/4-governance/dubbo-samples-opensergo/src/main/java/org/apache/dubbo/samples/governance/impl/DemoServiceImpl.java new file mode 100644 index 0000000000..87ed7417b0 --- /dev/null +++ b/4-governance/dubbo-samples-opensergo/src/main/java/org/apache/dubbo/samples/governance/impl/DemoServiceImpl.java @@ -0,0 +1,36 @@ +/* + * + * 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.samples.governance.impl; + +import org.apache.dubbo.rpc.RpcContext; +import org.apache.dubbo.samples.governance.api.DemoService; + +import java.text.SimpleDateFormat; +import java.util.Date; + +public class DemoServiceImpl implements DemoService { + + @Override + public String sayHello(String name) { + System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress()); + return "Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress(); + } +} diff --git a/4-governance/dubbo-samples-opensergo/src/main/java/org/apache/dubbo/samples/governance/impl/DemoServiceImpl2.java b/4-governance/dubbo-samples-opensergo/src/main/java/org/apache/dubbo/samples/governance/impl/DemoServiceImpl2.java new file mode 100644 index 0000000000..31fa42bff1 --- /dev/null +++ b/4-governance/dubbo-samples-opensergo/src/main/java/org/apache/dubbo/samples/governance/impl/DemoServiceImpl2.java @@ -0,0 +1,36 @@ +/* + * + * 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.samples.governance.impl; + +import org.apache.dubbo.rpc.RpcContext; +import org.apache.dubbo.samples.governance.api.DemoService2; + +import java.text.SimpleDateFormat; +import java.util.Date; + +public class DemoServiceImpl2 implements DemoService2 { + + @Override + public String sayHello(String name) { + System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + + ", request from consumer: " + RpcContext.getContext().getRemoteAddress()); + return "Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress(); + } +} diff --git a/4-governance/dubbo-samples-opensergo/src/main/resources/log4j.properties b/4-governance/dubbo-samples-opensergo/src/main/resources/log4j.properties new file mode 100644 index 0000000000..f30fda0d71 --- /dev/null +++ b/4-governance/dubbo-samples-opensergo/src/main/resources/log4j.properties @@ -0,0 +1,25 @@ +# +# +# 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. +# +# +###set log levels### +log4j.rootLogger=info, stdout +###output to the console### +log4j.appender.stdout=org.apache.log4j.ConsoleAppender +log4j.appender.stdout.Target=System.out +log4j.appender.stdout.layout=org.apache.log4j.PatternLayout +log4j.appender.stdout.layout.ConversionPattern=[%d{dd/MM/yy hh:mm:ss:sss z}] %t %5p %c{2}: %m%n \ No newline at end of file diff --git a/4-governance/dubbo-samples-opensergo/src/main/resources/spring/dubbo-demo-consumer.xml b/4-governance/dubbo-samples-opensergo/src/main/resources/spring/dubbo-demo-consumer.xml new file mode 100644 index 0000000000..dae469f14a --- /dev/null +++ b/4-governance/dubbo-samples-opensergo/src/main/resources/spring/dubbo-demo-consumer.xml @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + + diff --git a/4-governance/dubbo-samples-opensergo/src/main/resources/spring/dubbo-demo-provider-20880.xml b/4-governance/dubbo-samples-opensergo/src/main/resources/spring/dubbo-demo-provider-20880.xml new file mode 100644 index 0000000000..ce7b7bfada --- /dev/null +++ b/4-governance/dubbo-samples-opensergo/src/main/resources/spring/dubbo-demo-provider-20880.xml @@ -0,0 +1,47 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/4-governance/dubbo-samples-opensergo/src/main/resources/spring/dubbo-demo-provider-20881.xml b/4-governance/dubbo-samples-opensergo/src/main/resources/spring/dubbo-demo-provider-20881.xml new file mode 100644 index 0000000000..542f4f2a95 --- /dev/null +++ b/4-governance/dubbo-samples-opensergo/src/main/resources/spring/dubbo-demo-provider-20881.xml @@ -0,0 +1,47 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/4-governance/dubbo-samples-opensergo/src/test/java/org/apache/dubbo/samples/governance/DemoServiceIT.java b/4-governance/dubbo-samples-opensergo/src/test/java/org/apache/dubbo/samples/governance/DemoServiceIT.java new file mode 100644 index 0000000000..9551d00939 --- /dev/null +++ b/4-governance/dubbo-samples-opensergo/src/test/java/org/apache/dubbo/samples/governance/DemoServiceIT.java @@ -0,0 +1,73 @@ +/* + * 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.samples.governance; + +import org.apache.dubbo.rpc.RpcContext; +import org.apache.dubbo.rpc.RpcException; +import org.apache.dubbo.samples.governance.api.DemoService; + +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import static org.apache.dubbo.common.constants.CommonConstants.TAG_KEY; +import static org.apache.dubbo.rpc.Constants.FORCE_USE_TAG; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = {"classpath:/spring/dubbo-demo-consumer.xml"}) +public class DemoServiceIT { + @Autowired + @Qualifier("demoService") + private DemoService demoService; + + @BeforeClass + public static void setUp() throws Exception { + RuleUtil.initClient(); + RuleUtil.generateRule(); + Thread.sleep(1000); + } + + @Test + public void testDemoService1() throws Exception { + for (int i = 0; i < 10; i++) { + RpcContext.getContext().setAttachment(FORCE_USE_TAG, "true"); + RpcContext.getContext().setAttachment(TAG_KEY, "tag1"); + Assert.assertTrue(demoService.sayHello("world").contains("20881")); + } + } + + @Test + public void testDemoService2() throws Exception { + for (int i = 0; i < 10; i++) { + RpcContext.getContext().setAttachment(FORCE_USE_TAG, "true"); + RpcContext.getContext().setAttachment(TAG_KEY, "tag2"); + Assert.assertTrue(demoService.sayHello("world").contains("20880")); + } + } + + @Test(expected = RpcException.class) + public void testDemoService3() throws Exception { + RpcContext.getContext().setAttachment(TAG_KEY, "tag3"); + demoService.sayHello("world"); + } +} From 29cb985f0224a31f7547adf14bb5abdedba346dc Mon Sep 17 00:00:00 2001 From: "shengwei.psw" Date: Mon, 30 Jan 2023 13:29:34 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E8=B0=83=E6=95=B4opensergo=20demo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 4-governance/dubbo-samples-opensergo/pom.xml | 18 ++++++++---------- .../samples/governance/BasicConsumer.java | 5 ++--- .../resources/spring/dubbo-demo-consumer.xml | 4 ++++ 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/4-governance/dubbo-samples-opensergo/pom.xml b/4-governance/dubbo-samples-opensergo/pom.xml index 67b26ff258..d0150cfa15 100644 --- a/4-governance/dubbo-samples-opensergo/pom.xml +++ b/4-governance/dubbo-samples-opensergo/pom.xml @@ -33,6 +33,7 @@ 1.8 1.8 3.2.0-beta.3-SNAPSHOT + 1.0.0-SNAPSHOT 4.3.29.RELEASE 4.13.1 3.7.0 @@ -64,10 +65,9 @@ - org.apache.dubbo - dubbo-opensergo - ${dubbo.version} - pom + org.apache.dubbo.extensions + dubbo-cluster-opensergo + ${dubbo-opensergo.version} @@ -85,19 +85,17 @@ - org.apache.dubbo - dubbo + org.apache.dubbo.extensions + dubbo-cluster-opensergo - org.apache.dubbo - dubbo-dependencies-zookeeper - pom + dubbo org.apache.dubbo - dubbo-opensergo + dubbo-dependencies-zookeeper pom diff --git a/4-governance/dubbo-samples-opensergo/src/main/java/org/apache/dubbo/samples/governance/BasicConsumer.java b/4-governance/dubbo-samples-opensergo/src/main/java/org/apache/dubbo/samples/governance/BasicConsumer.java index 5e7334100d..0065e0ef16 100644 --- a/4-governance/dubbo-samples-opensergo/src/main/java/org/apache/dubbo/samples/governance/BasicConsumer.java +++ b/4-governance/dubbo-samples-opensergo/src/main/java/org/apache/dubbo/samples/governance/BasicConsumer.java @@ -36,10 +36,9 @@ public static void main(String[] args) { DemoService demoService = context.getBean("demoService", DemoService.class); DemoService2 demoService2 = context.getBean("demoService2", DemoService2.class); - RpcContext.getContext().setAttachment("x-mse-tag", "v3"); + RpcContext.getContext().setAttachment("x-opensergo-tag", "v3"); String hello = demoService.sayHello("world"); - -// System.out.println(hello); + System.out.println(hello); String hello2 = demoService2.sayHello("world again"); System.out.println(hello2); diff --git a/4-governance/dubbo-samples-opensergo/src/main/resources/spring/dubbo-demo-consumer.xml b/4-governance/dubbo-samples-opensergo/src/main/resources/spring/dubbo-demo-consumer.xml index dae469f14a..9724dd0387 100644 --- a/4-governance/dubbo-samples-opensergo/src/main/resources/spring/dubbo-demo-consumer.xml +++ b/4-governance/dubbo-samples-opensergo/src/main/resources/spring/dubbo-demo-consumer.xml @@ -31,6 +31,10 @@ + + + + From 1a9d98dbb1b801d97445986ac20eb23023083b8f Mon Sep 17 00:00:00 2001 From: "shengwei.psw" Date: Mon, 30 Jan 2023 13:40:16 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E8=B0=83=E6=95=B4opensergo=20traffic=20sam?= =?UTF-8?q?ples?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dubbo-samples-opensergo/README.md | 53 +++++++----- .../samples/governance/BasicConsumer.java | 2 +- .../dubbo/samples/governance/RuleUtil.java | 82 ------------------- .../resources/spring/dubbo-demo-consumer.xml | 2 +- .../spring/dubbo-demo-provider-20880.xml | 2 +- .../spring/dubbo-demo-provider-20881.xml | 2 +- 6 files changed, 38 insertions(+), 105 deletions(-) delete mode 100644 4-governance/dubbo-samples-opensergo/src/main/java/org/apache/dubbo/samples/governance/RuleUtil.java diff --git a/4-governance/dubbo-samples-opensergo/README.md b/4-governance/dubbo-samples-opensergo/README.md index 171122114b..2f9394f34d 100644 --- a/4-governance/dubbo-samples-opensergo/README.md +++ b/4-governance/dubbo-samples-opensergo/README.md @@ -1,27 +1,42 @@ -# governance dubbo using tag router +# governance dubbo using OpenSergo Tag router 1. step1 run `BasicProvider.java` > it will start an zookeeper and register two service with dubbo port 20880 2. step2 run `BasicProviderOtherPort.java` > it will register another two service with dubbo port 20881 -3. step3 run `RuleUtil.java`, -> it will read the route rule defined by [dubbo-routers-tag.yml](src%2Fmain%2Fresources%2Fdubbo-routers-tag.yml) -```yaml ---- -force: false -runtime: true -enabled: true -priority: 1 -key: governance-tagrouter-provider -tags: -- name: tag1 - addresses: ["*.*.*.*:20881"] -- name: tag2 - addresses: ["*.*.*.*:20880"] -... +3. step2 Start OpenSergoControlPlane and apply traffic config +[First start OpenSergo control plane](https://opensergo.io/docs/quick-start/opensergo-control-plane/) , Then we publish the label routing rules through the OpenSergo control plane. We publish a TrafficRouter rule. +```YAML +kubectl apply -f - << EOF +apiVersion: traffic.opensergo.io/v1alpha1 +kind: TrafficRouter +metadata: + name: governance-opensergo-traffic-provider + namespace: default + labels: + app: governance-opensergo-traffic-provider +spec: + hosts: + - governance-opensergo-traffic-provider + http: + - match: + - headers: + tag: + exact: v2 + route: + - destination: + host: governance-opensergo-traffic-provider + subset: v2 + - route: + - destination: + host: governance-opensergo-traffic-provider + subset: v1 +EOF ``` +This [TrafficRouter](https://github.com/opensergo/opensergo-specification/blob/main/specification/en/traffic-routing.md) +specifies the simplest label routing rule. Dubbo requests with the v2 header are routed to v2, and the rest of the traffic is routed to v1. -6. step4 run `BasicConsumer.java` +4. step4 run `BasicConsumer.java` > it will consume service by rule defined by yml file -> * All consumer taged by `tag1` come from provider in port 20881 -> * All consumer taged by `tag2` come from provider in port 20880 \ No newline at end of file +> * All consumer taged by `v2` come from provider in port 20881 +> * All consumer taged by `v1` come from provider in port 20880 \ No newline at end of file diff --git a/4-governance/dubbo-samples-opensergo/src/main/java/org/apache/dubbo/samples/governance/BasicConsumer.java b/4-governance/dubbo-samples-opensergo/src/main/java/org/apache/dubbo/samples/governance/BasicConsumer.java index 0065e0ef16..9d0091d3d0 100644 --- a/4-governance/dubbo-samples-opensergo/src/main/java/org/apache/dubbo/samples/governance/BasicConsumer.java +++ b/4-governance/dubbo-samples-opensergo/src/main/java/org/apache/dubbo/samples/governance/BasicConsumer.java @@ -36,7 +36,7 @@ public static void main(String[] args) { DemoService demoService = context.getBean("demoService", DemoService.class); DemoService2 demoService2 = context.getBean("demoService2", DemoService2.class); - RpcContext.getContext().setAttachment("x-opensergo-tag", "v3"); + RpcContext.getContext().setAttachment("tag", "v2"); String hello = demoService.sayHello("world"); System.out.println(hello); diff --git a/4-governance/dubbo-samples-opensergo/src/main/java/org/apache/dubbo/samples/governance/RuleUtil.java b/4-governance/dubbo-samples-opensergo/src/main/java/org/apache/dubbo/samples/governance/RuleUtil.java deleted file mode 100644 index 53d4a2da6b..0000000000 --- a/4-governance/dubbo-samples-opensergo/src/main/java/org/apache/dubbo/samples/governance/RuleUtil.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * 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.samples.governance; - -import org.apache.dubbo.common.utils.StringUtils; - -import org.apache.curator.framework.CuratorFramework; -import org.apache.curator.framework.CuratorFrameworkFactory; -import org.apache.curator.retry.ExponentialBackoffRetry; - -import java.io.IOException; -import java.io.InputStream; - -public class RuleUtil { - 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(); - generateRule(); - System.in.read(); - deleteRule(); - } - - public static void initClient() { - client = CuratorFrameworkFactory.newClient(zookeeperHost + ":2181", 60 * 1000, 60 * 1000, - new ExponentialBackoffRetry(1000, 3)); - client.start(); - } - - public static void generateRule() { - try (InputStream yamlStream = RuleUtil.class.getResourceAsStream("/dubbo-routers-tag.yml")) { - String path = "/dubbo/config/dubbo/governance-tagrouter-provider.tag-router"; - if (client.checkExists().forPath(path) == null) { - client.create().creatingParentsIfNeeded().forPath(path); - } - setData(path, streamToString(yamlStream)); - } catch (Exception e) { - e.printStackTrace(); - } - } - - public static void deleteRule() throws Exception { - String path = "/dubbo/config/dubbo/governance-tagrouter-provider.tag-router"; - if (client.checkExists().forPath(path) == null) { - client.create().creatingParentsIfNeeded().forPath(path); - } - setData(path, ""); - } - - private static String streamToString(InputStream stream) throws IOException { - byte[] bytes = new byte[stream.available()]; - stream.read(bytes); - return new String(bytes); - } - - private static void setData(String path, String data) throws Exception { - client.setData().forPath(path, data.getBytes()); - } - - private static String pathToKey(String path) { - if (StringUtils.isEmpty(path)) { - return path; - } - return path.replace("/dubbo/config/", "").replaceAll("/", "."); - } - -} diff --git a/4-governance/dubbo-samples-opensergo/src/main/resources/spring/dubbo-demo-consumer.xml b/4-governance/dubbo-samples-opensergo/src/main/resources/spring/dubbo-demo-consumer.xml index 9724dd0387..dd6dc1c22b 100644 --- a/4-governance/dubbo-samples-opensergo/src/main/resources/spring/dubbo-demo-consumer.xml +++ b/4-governance/dubbo-samples-opensergo/src/main/resources/spring/dubbo-demo-consumer.xml @@ -25,7 +25,7 @@ http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> - + diff --git a/4-governance/dubbo-samples-opensergo/src/main/resources/spring/dubbo-demo-provider-20880.xml b/4-governance/dubbo-samples-opensergo/src/main/resources/spring/dubbo-demo-provider-20880.xml index ce7b7bfada..5886159318 100644 --- a/4-governance/dubbo-samples-opensergo/src/main/resources/spring/dubbo-demo-provider-20880.xml +++ b/4-governance/dubbo-samples-opensergo/src/main/resources/spring/dubbo-demo-provider-20880.xml @@ -25,7 +25,7 @@ http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> - + diff --git a/4-governance/dubbo-samples-opensergo/src/main/resources/spring/dubbo-demo-provider-20881.xml b/4-governance/dubbo-samples-opensergo/src/main/resources/spring/dubbo-demo-provider-20881.xml index 542f4f2a95..c413563eeb 100644 --- a/4-governance/dubbo-samples-opensergo/src/main/resources/spring/dubbo-demo-provider-20881.xml +++ b/4-governance/dubbo-samples-opensergo/src/main/resources/spring/dubbo-demo-provider-20881.xml @@ -25,7 +25,7 @@ http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> - + From fcda66ca8986a87d9789ed9b56c98653ad73a6f3 Mon Sep 17 00:00:00 2001 From: "shengwei.psw" Date: Mon, 30 Jan 2023 13:42:11 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E8=B0=83=E6=95=B4readme?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 4-governance/dubbo-samples-opensergo/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/4-governance/dubbo-samples-opensergo/README.md b/4-governance/dubbo-samples-opensergo/README.md index 2f9394f34d..867d61e543 100644 --- a/4-governance/dubbo-samples-opensergo/README.md +++ b/4-governance/dubbo-samples-opensergo/README.md @@ -4,7 +4,7 @@ > it will start an zookeeper and register two service with dubbo port 20880 2. step2 run `BasicProviderOtherPort.java` > it will register another two service with dubbo port 20881 -3. step2 Start OpenSergoControlPlane and apply traffic config +3. step3 Start OpenSergoControlPlane and apply traffic config [First start OpenSergo control plane](https://opensergo.io/docs/quick-start/opensergo-control-plane/) , Then we publish the label routing rules through the OpenSergo control plane. We publish a TrafficRouter rule. ```YAML kubectl apply -f - << EOF From c158f316a962dc314ff4999988ee60404c23e44b Mon Sep 17 00:00:00 2001 From: "shengwei.psw" Date: Mon, 30 Jan 2023 13:45:22 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=97=A0=E7=94=A8test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../samples/governance/DemoServiceIT.java | 73 ------------------- 1 file changed, 73 deletions(-) delete mode 100644 4-governance/dubbo-samples-opensergo/src/test/java/org/apache/dubbo/samples/governance/DemoServiceIT.java diff --git a/4-governance/dubbo-samples-opensergo/src/test/java/org/apache/dubbo/samples/governance/DemoServiceIT.java b/4-governance/dubbo-samples-opensergo/src/test/java/org/apache/dubbo/samples/governance/DemoServiceIT.java deleted file mode 100644 index 9551d00939..0000000000 --- a/4-governance/dubbo-samples-opensergo/src/test/java/org/apache/dubbo/samples/governance/DemoServiceIT.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * 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.samples.governance; - -import org.apache.dubbo.rpc.RpcContext; -import org.apache.dubbo.rpc.RpcException; -import org.apache.dubbo.samples.governance.api.DemoService; - -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; - -import static org.apache.dubbo.common.constants.CommonConstants.TAG_KEY; -import static org.apache.dubbo.rpc.Constants.FORCE_USE_TAG; - -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(locations = {"classpath:/spring/dubbo-demo-consumer.xml"}) -public class DemoServiceIT { - @Autowired - @Qualifier("demoService") - private DemoService demoService; - - @BeforeClass - public static void setUp() throws Exception { - RuleUtil.initClient(); - RuleUtil.generateRule(); - Thread.sleep(1000); - } - - @Test - public void testDemoService1() throws Exception { - for (int i = 0; i < 10; i++) { - RpcContext.getContext().setAttachment(FORCE_USE_TAG, "true"); - RpcContext.getContext().setAttachment(TAG_KEY, "tag1"); - Assert.assertTrue(demoService.sayHello("world").contains("20881")); - } - } - - @Test - public void testDemoService2() throws Exception { - for (int i = 0; i < 10; i++) { - RpcContext.getContext().setAttachment(FORCE_USE_TAG, "true"); - RpcContext.getContext().setAttachment(TAG_KEY, "tag2"); - Assert.assertTrue(demoService.sayHello("world").contains("20880")); - } - } - - @Test(expected = RpcException.class) - public void testDemoService3() throws Exception { - RpcContext.getContext().setAttachment(TAG_KEY, "tag3"); - demoService.sayHello("world"); - } -}