diff --git a/dubbo-samples-rpccontext/README.md b/dubbo-samples-rpccontext/README.md new file mode 100644 index 0000000000..2bf4ab19de --- /dev/null +++ b/dubbo-samples-rpccontext/README.md @@ -0,0 +1,2 @@ +## 隐式参数 +可以通过 RpcContext 上的 setAttachment 和 getAttachment 在服务消费方和提供方之间进行参数的隐式传递。 \ No newline at end of file diff --git a/dubbo-samples-rpccontext/case-configuration.yml b/dubbo-samples-rpccontext/case-configuration.yml new file mode 100644 index 0000000000..709e741d9c --- /dev/null +++ b/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 diff --git a/dubbo-samples-rpccontext/case-versions.conf b/dubbo-samples-rpccontext/case-versions.conf new file mode 100644 index 0000000000..a226141fc7 --- /dev/null +++ b/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/dubbo-samples-rpccontext/pom.xml b/dubbo-samples-rpccontext/pom.xml new file mode 100644 index 0000000000..ee604b3815 --- /dev/null +++ b/dubbo-samples-rpccontext/pom.xml @@ -0,0 +1,128 @@ + + + + + org.apache.dubbo + 1.0-SNAPSHOT + + 4.0.0 + + dubbo-samples-rpccontext + + + 1.8 + 1.8 + 3.2.0-beta.2 + 4.3.16.RELEASE + 4.12 + 3.7.0 + + + + + org.apache.dubbo + dubbo + + + + org.apache.dubbo + dubbo-dependencies-zookeeper + pom + + + + junit + junit + test + + + + org.springframework + spring-test + test + + + + + + + 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 + + + + junit + junit + ${junit.version} + + + + + + + + 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} + + + + + diff --git a/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/EmbeddedZooKeeper.java b/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/EmbeddedZooKeeper.java new file mode 100644 index 0000000000..6be3bdca12 --- /dev/null +++ b/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. + *

+ * 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/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/RpcContextConsumer.java b/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/RpcContextConsumer.java new file mode 100644 index 0000000000..44f01b19f4 --- /dev/null +++ b/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/RpcContextConsumer.java @@ -0,0 +1,41 @@ +/* + * + * 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.config.ApplicationConfig; +import org.apache.dubbo.config.ProtocolConfig; +import org.apache.dubbo.config.ReferenceConfig; +import org.apache.dubbo.config.RegistryConfig; +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.utils.RpcContextUtils; +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/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/RpcContextProvider1.java b/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/RpcContextProvider1.java new file mode 100644 index 0000000000..8129e70011 --- /dev/null +++ b/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/RpcContextProvider1.java @@ -0,0 +1,41 @@ +/* + * + * 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.config.*; +import org.apache.dubbo.config.bootstrap.DubboBootstrap; +import org.apache.dubbo.samples.rpccontext.api.RpcContextService1; +import org.apache.dubbo.samples.rpccontext.impl.RpcContextImpl1; +import org.apache.dubbo.samples.rpccontext.utils.RpcContextUtils; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import java.io.IOException; +import java.util.concurrent.CompletableFuture; +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/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/RpcContextProvider2.java b/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/RpcContextProvider2.java new file mode 100644 index 0000000000..b0cc6360b7 --- /dev/null +++ b/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/RpcContextProvider2.java @@ -0,0 +1,44 @@ +/* + * + * 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.config.*; +import org.apache.dubbo.config.bootstrap.DubboBootstrap; +import org.apache.dubbo.samples.rpccontext.api.RpcContextService1; +import org.apache.dubbo.samples.rpccontext.api.RpcContextService2; +import org.apache.dubbo.samples.rpccontext.impl.RpcContextImpl1; +import org.apache.dubbo.samples.rpccontext.impl.RpcContextImpl2; +import org.apache.dubbo.samples.rpccontext.utils.RpcContextUtils; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import java.io.IOException; +import java.util.concurrent.CompletableFuture; +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(); + } +} diff --git a/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/api/RpcContextService1.java b/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/api/RpcContextService1.java new file mode 100644 index 0000000000..00dabe94f7 --- /dev/null +++ b/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/api/RpcContextService1.java @@ -0,0 +1,27 @@ +/* + * + * 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(); +} diff --git a/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/api/RpcContextService2.java b/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/api/RpcContextService2.java new file mode 100644 index 0000000000..453f269911 --- /dev/null +++ b/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/api/RpcContextService2.java @@ -0,0 +1,27 @@ +/* + * + * 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(); +} diff --git a/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/dto/Service1DTO.java b/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/dto/Service1DTO.java new file mode 100644 index 0000000000..e09b8e6b23 --- /dev/null +++ b/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; + } +} diff --git a/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/dto/Service2DTO.java b/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/dto/Service2DTO.java new file mode 100644 index 0000000000..954f093be5 --- /dev/null +++ b/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; + } +} diff --git a/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/impl/PenetrateAttachmentSelectorImpl1.java b/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/impl/PenetrateAttachmentSelectorImpl1.java new file mode 100644 index 0000000000..056c4c1259 --- /dev/null +++ b/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 select(Invocation invocation, RpcContextAttachment clientAttachment, RpcContextAttachment serverAttachment) { + return null; + } + + @Override + public Map selectReverse(Invocation invocation, RpcContextAttachment clientResponse, RpcContextAttachment serverResponse) { + return null; + } +} diff --git a/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/impl/RpcContextImpl1.java b/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/impl/RpcContextImpl1.java new file mode 100644 index 0000000000..74e7ee6a0c --- /dev/null +++ b/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/impl/RpcContextImpl1.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.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; +import org.springframework.util.Assert; + +import java.text.SimpleDateFormat; +import java.util.Date; + +@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 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; + } +} diff --git a/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/impl/RpcContextImpl2.java b/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/impl/RpcContextImpl2.java new file mode 100644 index 0000000000..813fc26689 --- /dev/null +++ b/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/impl/RpcContextImpl2.java @@ -0,0 +1,47 @@ +/* + * + * 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.RpcContextService1; +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; + +import java.text.SimpleDateFormat; +import java.util.Date; + +@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; + } +} diff --git a/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/utils/RpcContextUtils.java b/dubbo-samples-rpccontext/src/main/java/org/apache/dubbo/samples/rpccontext/utils/RpcContextUtils.java new file mode 100644 index 0000000000..27ab0f9270 --- /dev/null +++ b/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"; + +} diff --git a/dubbo-samples-rpccontext/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.PenetrateAttachmentSelector b/dubbo-samples-rpccontext/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.PenetrateAttachmentSelector new file mode 100644 index 0000000000..b2948cdc00 --- /dev/null +++ b/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/dubbo-samples-rpccontext/src/main/resources/log4j.properties b/dubbo-samples-rpccontext/src/main/resources/log4j.properties new file mode 100644 index 0000000000..d6ecd5ce34 --- /dev/null +++ b/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/dubbo-samples-rpccontext/src/main/resources/spring/dubbo-rpccontext-consumer.xml b/dubbo-samples-rpccontext/src/main/resources/spring/dubbo-rpccontext-consumer.xml new file mode 100644 index 0000000000..08560ab4f2 --- /dev/null +++ b/dubbo-samples-rpccontext/src/main/resources/spring/dubbo-rpccontext-consumer.xml @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + + diff --git a/dubbo-samples-rpccontext/src/main/resources/spring/dubbo-rpccontext-provider1.xml b/dubbo-samples-rpccontext/src/main/resources/spring/dubbo-rpccontext-provider1.xml new file mode 100644 index 0000000000..8cdea6299e --- /dev/null +++ b/dubbo-samples-rpccontext/src/main/resources/spring/dubbo-rpccontext-provider1.xml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/dubbo-samples-rpccontext/src/main/resources/spring/dubbo-rpccontext-provider2.xml b/dubbo-samples-rpccontext/src/main/resources/spring/dubbo-rpccontext-provider2.xml new file mode 100644 index 0000000000..09f3eca4ab --- /dev/null +++ b/dubbo-samples-rpccontext/src/main/resources/spring/dubbo-rpccontext-provider2.xml @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + + + + + + diff --git a/dubbo-samples-rpccontext/src/test/java/org/apache/dubbo/samples/rpccontext/RpcContextServiceIT.java b/dubbo-samples-rpccontext/src/test/java/org/apache/dubbo/samples/rpccontext/RpcContextServiceIT.java new file mode 100644 index 0000000000..02e73f73d4 --- /dev/null +++ b/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()); + } +} diff --git a/pom.xml b/pom.xml index 68cb4beb7a..a0aeaac38f 100644 --- a/pom.xml +++ b/pom.xml @@ -58,6 +58,7 @@ dubbo-samples-notify dubbo-samples-resilience4j dubbo-samples-rest + dubbo-samples-rpccontext dubbo-samples-scala dubbo-samples-simplified-registry dubbo-samples-spi-compatible