This is an automated email from the ASF dual-hosted git repository. albumenj pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/dubbo-samples.git
The following commit(s) were added to refs/heads/master by this push: new 81486f76 Feature/split rpccontext (#700) 81486f76 is described below commit 81486f76693c214c26075438e490b0182492771f Author: aamingaa <49740762+aamin...@users.noreply.github.com> AuthorDate: Tue Jan 17 14:26:33 2023 +0800 Feature/split rpccontext (#700) * feat: split rpccontext test * feat: split rpccontext test * feat: split rpccontext test * feat: split rpccontext test * feat: remove parent label * feat: adjust pom.xml * feat: remove duplicate version --- 2-advanced/dubbo-samples-rpccontext/README.md | 2 + .../case-configuration.yml | 65 ++++++ .../dubbo-samples-rpccontext/case-versions.conf | 24 ++ 2-advanced/dubbo-samples-rpccontext/pom.xml | 128 +++++++++++ .../samples/rpccontext/EmbeddedZooKeeper.java | 255 +++++++++++++++++++++ .../samples/rpccontext/RpcContextConsumer.java | 33 +++ .../samples/rpccontext/RpcContextProvider1.java | 34 +++ .../samples/rpccontext/RpcContextProvider2.java | 34 +++ .../samples/rpccontext/api/RpcContextService1.java | 28 +++ .../samples/rpccontext/api/RpcContextService2.java | 28 +++ .../dubbo/samples/rpccontext/dto/Service1DTO.java | 53 +++++ .../dubbo/samples/rpccontext/dto/Service2DTO.java | 43 ++++ .../impl/PenetrateAttachmentSelectorImpl1.java | 39 ++++ .../samples/rpccontext/impl/RpcContextImpl1.java | 58 +++++ .../samples/rpccontext/impl/RpcContextImpl2.java | 43 ++++ .../samples/rpccontext/utils/RpcContextUtils.java | 34 +++ ...rg.apache.dubbo.rpc.PenetrateAttachmentSelector | 1 + .../src/main/resources/log4j.properties | 26 +++ .../resources/spring/dubbo-rpccontext-consumer.xml | 35 +++ .../spring/dubbo-rpccontext-provider1.xml | 38 +++ .../spring/dubbo-rpccontext-provider2.xml | 36 +++ .../samples/rpccontext/RpcContextServiceIT.java | 64 ++++++ 2-advanced/pom.xml | 1 + 23 files changed, 1102 insertions(+) diff --git a/2-advanced/dubbo-samples-rpccontext/README.md b/2-advanced/dubbo-samples-rpccontext/README.md new file mode 100644 index 00000000..98200fd7 --- /dev/null +++ b/2-advanced/dubbo-samples-rpccontext/README.md @@ -0,0 +1,2 @@ +## 隐式参数 +可以通过 PenetrateAttachmentSelector 接口和 PenetrateAttachmentSelector 接口 控制rpc context服务消费方和提供方之间参数的隐式传递的层数。 \ No newline at end of file diff --git a/2-advanced/dubbo-samples-rpccontext/case-configuration.yml b/2-advanced/dubbo-samples-rpccontext/case-configuration.yml new file mode 100644 index 00000000..eb8d1385 --- /dev/null +++ b/2-advanced/dubbo-samples-rpccontext/case-configuration.yml @@ -0,0 +1,65 @@ +# 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-rpccontext-provider2: + type: app + basedir: . + mainClass: org.apache.dubbo.samples.rpccontext.RpcContextProvider2 + systemProps: + - zookeeper.address=zookeeper + waitPortsBeforeRun: + - zookeeper:2181 + checkPorts: + - 20882 + checkLog: "Rpc context provider2 started" + depends_on: + - zookeeper + + + dubbo-samples-rpccontext-provider: + type: app + basedir: . + mainClass: org.apache.dubbo.samples.rpccontext.RpcContextProvider1 + systemProps: + - zookeeper.address=zookeeper + waitPortsBeforeRun: + - zookeeper:2181 + checkPorts: + - 20880 + checkLog: "Rpc context provider1 started" + depends_on: + - zookeeper + - dubbo-samples-rpccontext-provider2 + + dubbo-samples-rpccontext-consumer: + type: test + basedir: . + tests: + - "**/*IT.class" + systemProps: + - zookeeper.address=zookeeper + waitPortsBeforeRun: + - zookeeper:2181 + - dubbo-samples-rpccontext-provider:20880 + - dubbo-samples-rpccontext-provider2:20882 + depends_on: + - zookeeper + - dubbo-samples-rpccontext-provider + - dubbo-samples-rpccontext-provider2 \ No newline at end of file diff --git a/2-advanced/dubbo-samples-rpccontext/case-versions.conf b/2-advanced/dubbo-samples-rpccontext/case-versions.conf new file mode 100644 index 00000000..a226141f --- /dev/null +++ b/2-advanced/dubbo-samples-rpccontext/case-versions.conf @@ -0,0 +1,24 @@ +# +# +# 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= [ >= 3.2.0 ] +spring.version=4.*, 5.* diff --git a/2-advanced/dubbo-samples-rpccontext/pom.xml b/2-advanced/dubbo-samples-rpccontext/pom.xml new file mode 100644 index 00000000..70203140 --- /dev/null +++ b/2-advanced/dubbo-samples-rpccontext/pom.xml @@ -0,0 +1,128 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. + --> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <groupId>org.apache.dubbo</groupId> + <version>1.0-SNAPSHOT</version> + + <modelVersion>4.0.0</modelVersion> + + <artifactId>dubbo-samples-rpccontext</artifactId> + <name>Dubbo Samples Context</name> + <description>Dubbo Samples Context</description> + + <properties> + <source.level>1.8</source.level> + <target.level>1.8</target.level> + <dubbo.version>3.2.0-beta.2</dubbo.version> + <spring.version>4.3.29.RELEASE</spring.version> + <junit.version>4.13.1</junit.version> + <maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version> + </properties> + + <dependencyManagement> + <dependencies> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-framework-bom</artifactId> + <version>${spring.version}</version> + <type>pom</type> + <scope>import</scope> + </dependency> + <dependency> + <groupId>org.apache.dubbo</groupId> + <artifactId>dubbo-bom</artifactId> + <version>${dubbo.version}</version> + <type>pom</type> + <scope>import</scope> + </dependency> + <dependency> + <groupId>org.apache.dubbo</groupId> + <artifactId>dubbo-dependencies-zookeeper</artifactId> + <version>${dubbo.version}</version> + <type>pom</type> + </dependency> + </dependencies> + </dependencyManagement> + + <dependencies> + <dependency> + <groupId>org.apache.dubbo</groupId> + <artifactId>dubbo</artifactId> + </dependency> + + <dependency> + <groupId>org.apache.dubbo</groupId> + <artifactId>dubbo-dependencies-zookeeper</artifactId> + <type>pom</type> + </dependency> + + + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>${junit.version}</version> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-test</artifactId> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-test</artifactId> + <scope>test</scope> + </dependency> + + </dependencies> + + <profiles> + <!-- For jdk 11 above JavaEE annotation --> + <profile> + <id>javax.annotation</id> + <activation> + <jdk>[1.11,)</jdk> + </activation> + <dependencies> + <dependency> + <groupId>javax.annotation</groupId> + <artifactId>javax.annotation-api</artifactId> + <version>1.3.2</version> + </dependency> + </dependencies> + </profile> + </profiles> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <version>${maven-compiler-plugin.version}</version> + <configuration> + <source>${source.level}</source> + <target>${target.level}</target> + </configuration> + </plugin> + </plugins> + </build> +</project> diff --git a/2-advanced/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/EmbeddedZooKeeper.java b/2-advanced/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/EmbeddedZooKeeper.java new file mode 100644 index 00000000..91e4a650 --- /dev/null +++ b/2-advanced/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/EmbeddedZooKeeper.java @@ -0,0 +1,255 @@ +/* + * 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.rpccontext; + +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. + * <p> + * 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); + } + } + } + } + +} \ No newline at end of file diff --git a/2-advanced/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/RpcContextConsumer.java b/2-advanced/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/RpcContextConsumer.java new file mode 100644 index 00000000..e792e1d6 --- /dev/null +++ b/2-advanced/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/RpcContextConsumer.java @@ -0,0 +1,33 @@ +/* + * + * 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.rpccontext; + +import org.apache.dubbo.samples.rpccontext.api.RpcContextService1; +import org.springframework.context.support.ClassPathXmlApplicationContext; + + +public class RpcContextConsumer { + public static void main(String[] args) { + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-rpccontext-consumer.xml"); + context.start(); + RpcContextService1 rpcContextService1 = context.getBean("consumerService", RpcContextService1.class); + rpcContextService1.sayHello(); + } +} diff --git a/2-advanced/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/RpcContextProvider1.java b/2-advanced/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/RpcContextProvider1.java new file mode 100644 index 00000000..2dfac99b --- /dev/null +++ b/2-advanced/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/RpcContextProvider1.java @@ -0,0 +1,34 @@ +/* + * + * 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.rpccontext; + +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import java.util.concurrent.CountDownLatch; + + +public class RpcContextProvider1 { + public static void main(String[] args) throws Exception { + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-rpccontext-provider1.xml"); + context.start(); + System.out.println("Rpc context provider1 started"); + new CountDownLatch(1).await(); + } +} diff --git a/2-advanced/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/RpcContextProvider2.java b/2-advanced/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/RpcContextProvider2.java new file mode 100644 index 00000000..a05da11b --- /dev/null +++ b/2-advanced/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/RpcContextProvider2.java @@ -0,0 +1,34 @@ +package org.apache.dubbo.samples.rpccontext; +/* + * + * 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.springframework.context.support.ClassPathXmlApplicationContext; + +import java.util.concurrent.CountDownLatch; + + +public class RpcContextProvider2 { + public static void main(String[] args) throws Exception { + new EmbeddedZooKeeper(2181, false).start(); + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-rpccontext-provider2.xml"); + context.start(); + System.out.println("Rpc context provider2 started"); + new CountDownLatch(1).await(); + } +} \ No newline at end of file diff --git a/2-advanced/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/api/RpcContextService1.java b/2-advanced/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/api/RpcContextService1.java new file mode 100644 index 00000000..46e14d0b --- /dev/null +++ b/2-advanced/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/api/RpcContextService1.java @@ -0,0 +1,28 @@ + +/* + * + * 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.rpccontext.api; + + +import org.apache.dubbo.samples.rpccontext.dto.Service1DTO; + +public interface RpcContextService1 { + Service1DTO sayHello(); +} \ No newline at end of file diff --git a/2-advanced/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/api/RpcContextService2.java b/2-advanced/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/api/RpcContextService2.java new file mode 100644 index 00000000..97f10ad0 --- /dev/null +++ b/2-advanced/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/api/RpcContextService2.java @@ -0,0 +1,28 @@ + +/* + * + * 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.rpccontext.api; + + +import org.apache.dubbo.samples.rpccontext.dto.Service2DTO; + +public interface RpcContextService2 { + Service2DTO sayHi(); +} \ No newline at end of file diff --git a/2-advanced/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/dto/Service1DTO.java b/2-advanced/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/dto/Service1DTO.java new file mode 100644 index 00000000..2efe969c --- /dev/null +++ b/2-advanced/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/dto/Service1DTO.java @@ -0,0 +1,53 @@ +/* + * + * 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.rpccontext.dto; + +import java.io.Serializable; + +public class Service1DTO implements Serializable { + private String consumerReq; + private String provider2Res; + + private Service2DTO service2DTO; + + public Service2DTO getService2DTO() { + return service2DTO; + } + + public void setService2DTO(Service2DTO service2DTO) { + this.service2DTO = service2DTO; + } + + public String getConsumerReq() { + return consumerReq; + } + + public void setConsumerReq(String consumerReq) { + this.consumerReq = consumerReq; + } + + public String getProvider2Res() { + return provider2Res; + } + + public void setProvider2Res(String provider2Res) { + this.provider2Res = provider2Res; + } +} \ No newline at end of file diff --git a/2-advanced/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/dto/Service2DTO.java b/2-advanced/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/dto/Service2DTO.java new file mode 100644 index 00000000..98109fc8 --- /dev/null +++ b/2-advanced/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/dto/Service2DTO.java @@ -0,0 +1,43 @@ +/* + * + * 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.rpccontext.dto; + +import java.io.Serializable; + +public class Service2DTO implements Serializable { + private String consumerReq; + private String provider1Req; + + public String getConsumerReq() { + return consumerReq; + } + + public void setConsumerReq(String consumerReq) { + this.consumerReq = consumerReq; + } + + public String getProvider1Req() { + return provider1Req; + } + + public void setProvider1Req(String provider1Req) { + this.provider1Req = provider1Req; + } +} \ No newline at end of file diff --git a/2-advanced/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/impl/PenetrateAttachmentSelectorImpl1.java b/2-advanced/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/impl/PenetrateAttachmentSelectorImpl1.java new file mode 100644 index 00000000..c38b6fc3 --- /dev/null +++ b/2-advanced/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/impl/PenetrateAttachmentSelectorImpl1.java @@ -0,0 +1,39 @@ +/* + * + * 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.rpccontext.impl; + +import org.apache.dubbo.rpc.Invocation; +import org.apache.dubbo.rpc.PenetrateAttachmentSelector; +import org.apache.dubbo.rpc.RpcContextAttachment; + +import java.util.Map; + +public class PenetrateAttachmentSelectorImpl1 implements PenetrateAttachmentSelector { + + @Override + public Map<String, Object> select(Invocation invocation, RpcContextAttachment clientAttachment, RpcContextAttachment serverAttachment) { + return null; + } + + @Override + public Map<String, Object> selectReverse(Invocation invocation, RpcContextAttachment clientResponse, RpcContextAttachment serverResponse) { + return null; + } +} \ No newline at end of file diff --git a/2-advanced/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/impl/RpcContextImpl1.java b/2-advanced/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/impl/RpcContextImpl1.java new file mode 100644 index 00000000..29d218c7 --- /dev/null +++ b/2-advanced/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/impl/RpcContextImpl1.java @@ -0,0 +1,58 @@ +/* + * + * 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.rpccontext.impl; + +import org.apache.dubbo.config.ReferenceConfig; +import org.apache.dubbo.config.bootstrap.DubboBootstrap; +import org.apache.dubbo.rpc.RpcContext; +import org.apache.dubbo.samples.rpccontext.api.RpcContextService1; +import org.apache.dubbo.samples.rpccontext.api.RpcContextService2; +import org.apache.dubbo.samples.rpccontext.dto.Service1DTO; +import org.apache.dubbo.samples.rpccontext.dto.Service2DTO; +import org.apache.dubbo.samples.rpccontext.utils.RpcContextUtils; +import org.springframework.stereotype.Component; +import org.springframework.stereotype.Service; + +@Service +@Component("demoService") +public class RpcContextImpl1 implements RpcContextService1 { + + @Override + public Service1DTO sayHello(){ + Service1DTO service1DTO = new Service1DTO(); + String consumerReq = RpcContext.getServerAttachment().getAttachment(RpcContextUtils.consumer_req_key); + System.out.println("get request from consumer:"+ consumerReq); + ReferenceConfig<RpcContextService2> ref = new ReferenceConfig<>(); + ref.setInterface(RpcContextService2.class); + ref.setProtocol(RpcContextUtils.dubbo_protocol); + ref.setTimeout(30000000); + DubboBootstrap bootstrap = DubboBootstrap.getInstance(); + RpcContextService2 greeterSayHi = bootstrap.getCache().get(ref); + RpcContext.getClientAttachment().setObjectAttachment(RpcContextUtils.provider1_req_key, RpcContextUtils.provider1_req_key); + Service2DTO service2DTO = greeterSayHi.sayHi(); + String provider2Res = (String)RpcContext.getClientResponseContext().getObjectAttachment(RpcContextUtils.provider2_res_key); + System.out.println("get response from provider2:"+ provider2Res); + RpcContext.getServerResponseContext().setObjectAttachment(RpcContextUtils.provider1_res_key, RpcContextUtils.provider1_res_key); + service1DTO.setConsumerReq(consumerReq); + service1DTO.setProvider2Res(provider2Res); + service1DTO.setService2DTO(service2DTO); + return service1DTO; + } +} \ No newline at end of file diff --git a/2-advanced/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/impl/RpcContextImpl2.java b/2-advanced/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/impl/RpcContextImpl2.java new file mode 100644 index 00000000..687ad0f2 --- /dev/null +++ b/2-advanced/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/impl/RpcContextImpl2.java @@ -0,0 +1,43 @@ +/* + * + * 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.rpccontext.impl; + +import org.apache.dubbo.rpc.RpcContext; +import org.apache.dubbo.samples.rpccontext.api.RpcContextService2; +import org.apache.dubbo.samples.rpccontext.dto.Service2DTO; +import org.apache.dubbo.samples.rpccontext.utils.RpcContextUtils; +import org.springframework.stereotype.Service; + +@Service +public class RpcContextImpl2 implements RpcContextService2 { + + @Override + public Service2DTO sayHi(){ + Service2DTO service2DTO = new Service2DTO(); + String consumerReq = RpcContext.getServerAttachment().getAttachment(RpcContextUtils.consumer_req_key); + System.out.println("get request from consumer:"+ consumerReq); + String provider1Req = (String) RpcContext.getServerAttachment().getObjectAttachment(RpcContextUtils.provider1_req_key); + System.out.println("get request from provider1:"+ provider1Req); + RpcContext.getServerResponseContext().setAttachment(RpcContextUtils.provider2_res_key, RpcContextUtils.provider2_res_key); + service2DTO.setConsumerReq(consumerReq); + service2DTO.setProvider1Req(provider1Req); + return service2DTO; + } +} \ No newline at end of file diff --git a/2-advanced/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/utils/RpcContextUtils.java b/2-advanced/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/utils/RpcContextUtils.java new file mode 100644 index 00000000..7245075c --- /dev/null +++ b/2-advanced/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/utils/RpcContextUtils.java @@ -0,0 +1,34 @@ +/* + * + * 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.rpccontext.utils; + +import org.apache.dubbo.common.constants.CommonConstants; + +public class RpcContextUtils { + public static String dubbo_protocol = CommonConstants.DUBBO; + + public static String consumer_req_key = "consumer_req_key"; + + public static String provider1_req_key = "provider1_req_key"; + + public static String provider2_res_key = "provider2_res_key"; + public static String provider1_res_key = "provider1_res_key"; + +} \ No newline at end of file diff --git a/2-advanced/dubbo-samples-rpccontext/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.PenetrateAttachmentSelector b/2-advanced/dubbo-samples-rpccontext/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.PenetrateAttachmentSelector new file mode 100644 index 00000000..b2948cdc --- /dev/null +++ b/2-advanced/dubbo-samples-rpccontext/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.PenetrateAttachmentSelector @@ -0,0 +1 @@ +mock=org.apache.dubbo.samples.rpccontext.impl.PenetrateAttachmentSelectorImpl1 diff --git a/2-advanced/dubbo-samples-rpccontext/src/main/resources/log4j.properties b/2-advanced/dubbo-samples-rpccontext/src/main/resources/log4j.properties new file mode 100644 index 00000000..d6ecd5ce --- /dev/null +++ b/2-advanced/dubbo-samples-rpccontext/src/main/resources/log4j.properties @@ -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. +# +# + +###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/2-advanced/dubbo-samples-rpccontext/src/main/resources/spring/dubbo-rpccontext-consumer.xml b/2-advanced/dubbo-samples-rpccontext/src/main/resources/spring/dubbo-rpccontext-consumer.xml new file mode 100644 index 00000000..dac249e5 --- /dev/null +++ b/2-advanced/dubbo-samples-rpccontext/src/main/resources/spring/dubbo-rpccontext-consumer.xml @@ -0,0 +1,35 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. + --> + +<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" + xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" + xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + 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"> + <context:property-placeholder/> + + <!-- consumer's application name, used for tracing dependency relationship (not a matching criterion), + don't set it same as provider --> + <dubbo:application name="rpccontext-consumer"/> + + <!-- use multicast registry center to discover service --> + <dubbo:registry address="zookeeper://${zookeeper.address:127.0.0.1}:2181"/> + + <!-- generate proxy for the remote service, then demoService can be used in the same way as the + local regular interface --> + <dubbo:reference id="consumerService" check="false" interface="org.apache.dubbo.samples.rpccontext.api.RpcContextService1" timeout="30000"/> + +</beans> \ No newline at end of file diff --git a/2-advanced/dubbo-samples-rpccontext/src/main/resources/spring/dubbo-rpccontext-provider1.xml b/2-advanced/dubbo-samples-rpccontext/src/main/resources/spring/dubbo-rpccontext-provider1.xml new file mode 100644 index 00000000..45cfab0a --- /dev/null +++ b/2-advanced/dubbo-samples-rpccontext/src/main/resources/spring/dubbo-rpccontext-provider1.xml @@ -0,0 +1,38 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. + --> + +<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" + xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" + xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + 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"> + <context:property-placeholder/> + + <dubbo:application name="rpccontext-provider-1"/> + + <dubbo:registry address="zookeeper://${zookeeper.address:127.0.0.1}:2181"/> + + <dubbo:protocol name="dubbo" port="20880"/> + + <dubbo:provider token="true"/> + + <bean id="rpcContextImpl1" class="org.apache.dubbo.samples.rpccontext.impl.RpcContextImpl1"/> + + <dubbo:service interface="org.apache.dubbo.samples.rpccontext.api.RpcContextService1" ref="rpcContextImpl1"/> + + <dubbo:reference id="rpccontextService1" check="false" interface="org.apache.dubbo.samples.rpccontext.api.RpcContextService2" timeout="30000"/> + +</beans> \ No newline at end of file diff --git a/2-advanced/dubbo-samples-rpccontext/src/main/resources/spring/dubbo-rpccontext-provider2.xml b/2-advanced/dubbo-samples-rpccontext/src/main/resources/spring/dubbo-rpccontext-provider2.xml new file mode 100644 index 00000000..72783936 --- /dev/null +++ b/2-advanced/dubbo-samples-rpccontext/src/main/resources/spring/dubbo-rpccontext-provider2.xml @@ -0,0 +1,36 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. + --> + +<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" + xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" + xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + 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"> + <context:property-placeholder/> + + <dubbo:application name="rpccontext-provider-2"/> + + <dubbo:registry address="zookeeper://${zookeeper.address:127.0.0.1}:2181"/> + + <dubbo:protocol name="dubbo" port="20882"/> + + <dubbo:provider token="true"/> + + <bean id="rpcContextImpl2" class="org.apache.dubbo.samples.rpccontext.impl.RpcContextImpl2"/> + + <dubbo:service interface="org.apache.dubbo.samples.rpccontext.api.RpcContextService2" ref="rpcContextImpl2"/> + +</beans> \ No newline at end of file diff --git a/2-advanced/dubbo-samples-rpccontext/src/test/java/org/apache/dubbo/samples/rpccontext/RpcContextServiceIT.java b/2-advanced/dubbo-samples-rpccontext/src/test/java/org/apache/dubbo/samples/rpccontext/RpcContextServiceIT.java new file mode 100644 index 00000000..fbac4c4a --- /dev/null +++ b/2-advanced/dubbo-samples-rpccontext/src/test/java/org/apache/dubbo/samples/rpccontext/RpcContextServiceIT.java @@ -0,0 +1,64 @@ +/* + * 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.rpccontext; + +import org.apache.dubbo.rpc.RpcContext; +import org.apache.dubbo.samples.rpccontext.api.RpcContextService1; +import org.apache.dubbo.samples.rpccontext.dto.Service1DTO; +import org.apache.dubbo.samples.rpccontext.dto.Service2DTO; +import org.apache.dubbo.samples.rpccontext.utils.RpcContextUtils; +import org.junit.Assert; +import org.junit.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-rpccontext-consumer.xml"}) +public class RpcContextServiceIT { + + @Autowired + private RpcContextService1 service; + + /** + * A -> B -> C + * Both B and C can receive the client attachment from A + * consumer_req_key means the A request data + * provider1_req_key 、provider1_res_key respectively means the B request data and response data + * provider2_res_key means the C response data + */ + + @Test + public void testRpcContext1() { + RpcContext.getClientAttachment().setAttachment(RpcContextUtils.consumer_req_key, RpcContextUtils.consumer_req_key); + Service1DTO service1DTO = service.sayHello(); + Service2DTO service2DTO = service1DTO.getService2DTO(); + String provider1Res = RpcContext.getClientResponseContext().getAttachment(RpcContextUtils.provider1_res_key); + Assert.assertEquals(RpcContextUtils.provider1_res_key, provider1Res); + + Assert.assertEquals(RpcContextUtils.consumer_req_key, service1DTO.getConsumerReq()); + Assert.assertEquals(null, service2DTO.getConsumerReq()); + + Assert.assertEquals(RpcContextUtils.provider1_req_key, service2DTO.getProvider1Req()); + + String provider2Res = (String) RpcContext.getClientResponseContext().getObjectAttachment(RpcContextUtils.provider2_res_key); + Assert.assertEquals(null, provider2Res); + Assert.assertEquals(RpcContextUtils.provider2_res_key, service1DTO.getProvider2Res()); + } +} \ No newline at end of file diff --git a/2-advanced/pom.xml b/2-advanced/pom.xml index b179f855..3fbf4b7c 100644 --- a/2-advanced/pom.xml +++ b/2-advanced/pom.xml @@ -54,5 +54,6 @@ <module>dubbo-samples-tengine</module> <module>dubbo-samples-validation</module> <module>dubbo-samples-version</module> + <module>dubbo-samples-rpccontext</module> </modules> </project> \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@dubbo.apache.org For additional commands, e-mail: notifications-h...@dubbo.apache.org