This is an automated email from the ASF dual-hosted git repository. liujun pushed a commit to branch cloud-native in repository https://gitbox.apache.org/repos/asf/dubbo.git
commit c8ca5b6d839f7759d09543a5a95e5082278d9860 Author: ken.lj <ken.lj...@gmail.com> AuthorDate: Tue Aug 13 20:20:43 2019 +0800 demos --- dubbo-demo/dubbo-call-sc&dubbo/README.md | 7 ++ dubbo-demo/dubbo-call-sc&dubbo/consumer/pom.xml | 81 +++++++++++++++++++++ .../samples/microservices/sc/Application.java | 56 +++++++++++++++ .../dubbo/samples/microservices/sc/rest/User.java | 77 ++++++++++++++++++++ .../samples/microservices/sc/rest/UserService.java | 45 ++++++++++++ .../microservices/sc/rest/UserServiceImpl.java | 32 +++++++++ .../consumer/src/main/resources/log4j.properties | 7 ++ dubbo-demo/dubbo-call-sc&dubbo/pom.xml | 61 ++++++++++++++++ dubbo-demo/dubbo-call-sc&dubbo/provider/pom.xml | 83 +++++++++++++++++++++ .../samples/microservices/sc/Application.java | 15 ++++ .../configuration/RestTemplateConfiguration.java | 16 +++++ .../microservices/sc/controller/MeController.java | 23 ++++++ .../sc/controller/PropertyController.java | 45 ++++++++++++ .../sc/controller/TestController.java | 46 ++++++++++++ .../samples/microservices/sc/feign/TestFeign.java | 13 ++++ .../provider/src/main/resources/application.yml | 6 ++ .../provider/src/main/resources/bootstrap.yml | 24 +++++++ dubbo-demo/dubbo-call-sc/README.md | 7 ++ dubbo-demo/dubbo-call-sc/consumer/pom.xml | 81 +++++++++++++++++++++ .../samples/microservices/sc/Application.java | 55 ++++++++++++++ .../dubbo/samples/microservices/sc/rest/User.java | 77 ++++++++++++++++++++ .../samples/microservices/sc/rest/UserService.java | 45 ++++++++++++ .../microservices/sc/rest/UserServiceImpl.java | 32 +++++++++ .../consumer/src/main/resources/log4j.properties | 7 ++ dubbo-demo/dubbo-call-sc/pom.xml | 61 ++++++++++++++++ dubbo-demo/dubbo-call-sc/provider/pom.xml | 83 +++++++++++++++++++++ .../samples/microservices/sc/Application.java | 15 ++++ .../configuration/RestTemplateConfiguration.java | 16 +++++ .../microservices/sc/controller/MeController.java | 23 ++++++ .../sc/controller/PropertyController.java | 45 ++++++++++++ .../sc/controller/TestController.java | 54 ++++++++++++++ .../samples/microservices/sc/controller/User.java | 60 ++++++++++++++++ .../samples/microservices/sc/feign/TestFeign.java | 13 ++++ .../provider/src/main/resources/application.yml | 6 ++ .../provider/src/main/resources/bootstrap.yml | 24 +++++++ dubbo-demo/pom.xml | 1 + dubbo-demo/sc-call-dubbo/consumer/pom.xml | 84 ++++++++++++++++++++++ .../com/bettercloud/spring/cloud/Application.java | 15 ++++ .../configuration/RestTemplateConfiguration.java | 17 +++++ .../spring/cloud/controller/ClientController.java | 29 ++++++++ .../bettercloud/spring/cloud/feign/TestFeign.java | 12 ++++ .../src/main/resources/application-holding.yml | 3 + .../consumer/src/main/resources/application.yml | 7 ++ .../consumer/src/main/resources/bootstrap.yml | 26 +++++++ dubbo-demo/sc-call-dubbo/pom.xml | 61 ++++++++++++++++ dubbo-demo/sc-call-dubbo/provider/pom.xml | 33 +++++++++ .../apache/dubbo/demo/provider/Application.java | 36 ++++++++++ .../dubbo/demo/provider/DemoServiceImpl.java | 34 +++++++++ .../provider/src/main/resources/log4j.properties | 7 ++ .../dubbo/registry/dubbo/RegistryProtocolTest.java | 19 +++-- 50 files changed, 1718 insertions(+), 7 deletions(-) diff --git a/dubbo-demo/dubbo-call-sc&dubbo/README.md b/dubbo-demo/dubbo-call-sc&dubbo/README.md new file mode 100644 index 0000000..e42c3e7 --- /dev/null +++ b/dubbo-demo/dubbo-call-sc&dubbo/README.md @@ -0,0 +1,7 @@ +Dubbo自动发现并消费原生Spring Cloud服务,以Consul为注册中心。 + +* Spring MVC方式开发服务端 +* 以标准Dubbo方式开发客户端 + * 首先,定义标准JAX-RS注解的接口(与Provider端相对应) + * Registry指定 + * 同时消费,要使用Multiple Registry策略 \ No newline at end of file diff --git a/dubbo-demo/dubbo-call-sc&dubbo/consumer/pom.xml b/dubbo-demo/dubbo-call-sc&dubbo/consumer/pom.xml new file mode 100644 index 0000000..f1d9dfd --- /dev/null +++ b/dubbo-demo/dubbo-call-sc&dubbo/consumer/pom.xml @@ -0,0 +1,81 @@ +<?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"> + <parent> + <groupId>org.apache.dubbo</groupId> + <artifactId>dubbo-call-sc</artifactId> + <version>${revision}</version> + <relativePath>../pom.xml</relativePath> + </parent> + <modelVersion>4.0.0</modelVersion> + + <artifactId>consumer</artifactId> + + <properties> + <skip_maven_deploy>true</skip_maven_deploy> + </properties> + + <dependencies> + <dependency> + <groupId>org.apache.dubbo</groupId> + <artifactId>dubbo-config-api</artifactId> + <version>${project.parent.version}</version> + </dependency> + <dependency> + <groupId>org.apache.dubbo</groupId> + <artifactId>dubbo-demo-interface</artifactId> + <version>${project.parent.version}</version> + </dependency> + <dependency> + <groupId>org.apache.dubbo</groupId> + <artifactId>dubbo-registry-multicast</artifactId> + </dependency> + <dependency> + <groupId>org.apache.dubbo</groupId> + <artifactId>dubbo-rpc-dubbo</artifactId> + </dependency> + <dependency> + <groupId>org.apache.dubbo</groupId> + <artifactId>dubbo-remoting-netty4</artifactId> + </dependency> + <dependency> + <groupId>org.apache.dubbo</groupId> + <artifactId>dubbo-serialization-hessian2</artifactId> + </dependency> + <dependency> + <groupId>org.apache.dubbo</groupId> + <artifactId>dubbo-bootstrap</artifactId> + <version>${project.parent.version}</version> + </dependency> + <dependency> + <groupId>org.apache.dubbo</groupId> + <artifactId>dubbo-registry-consul</artifactId> + <version>${project.parent.version}</version> + </dependency> + <dependency> + <groupId>org.apache.dubbo</groupId> + <artifactId>dubbo-rpc-rest</artifactId> + <version>${project.parent.version}</version> + </dependency> + </dependencies> + +</project> diff --git a/dubbo-demo/dubbo-call-sc&dubbo/consumer/src/main/java/org/apache/dubbo/samples/microservices/sc/Application.java b/dubbo-demo/dubbo-call-sc&dubbo/consumer/src/main/java/org/apache/dubbo/samples/microservices/sc/Application.java new file mode 100644 index 0000000..4164f60 --- /dev/null +++ b/dubbo-demo/dubbo-call-sc&dubbo/consumer/src/main/java/org/apache/dubbo/samples/microservices/sc/Application.java @@ -0,0 +1,56 @@ +/* + * 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.microservices.sc; + +import org.apache.dubbo.bootstrap.DubboBootstrap; +import org.apache.dubbo.config.ApplicationConfig; +import org.apache.dubbo.config.ReferenceConfig; +import org.apache.dubbo.config.RegistryConfig; +import org.apache.dubbo.config.context.ConfigManager; +import org.apache.dubbo.samples.microservices.sc.rest.UserService; + +public class Application { + public static void main(String[] args) { + RegistryConfig interfaceRegistry = new RegistryConfig(); + interfaceRegistry.setId("interfaceRegistry"); + interfaceRegistry.setAddress("zookeeper://127.0.0.1:2181"); + + new DubboBootstrap() + .application("dubbo-consumer-demo") + // Zookeeper + .registry(interfaceRegistry) + // Nacos +// .registry("consul", builder -> builder.address("consul://127.0.0.1:8500?registry.type=service&subscribed.services=dubbo-provider-demo")) + .reference("user", builder -> builder.interfaceClass(UserService.class).protocol("rest")) + .onlyRegisterProvider(true) + .start() + .await(); + + ConfigManager configManager = ConfigManager.getInstance(); + + ReferenceConfig<UserService> referenceConfig1 = configManager.getReferenceConfig("user"); + UserService userService = referenceConfig1.get(); + + for (int i = 0; i < 500; i++) { + Thread.sleep(2000L); + System.out.println(echoService.echo("Hello,World")); + System.out.println(userService.getUser(1L)); + } + } +} diff --git a/dubbo-demo/dubbo-call-sc&dubbo/consumer/src/main/java/org/apache/dubbo/samples/microservices/sc/rest/User.java b/dubbo-demo/dubbo-call-sc&dubbo/consumer/src/main/java/org/apache/dubbo/samples/microservices/sc/rest/User.java new file mode 100644 index 0000000..c365f8f --- /dev/null +++ b/dubbo-demo/dubbo-call-sc&dubbo/consumer/src/main/java/org/apache/dubbo/samples/microservices/sc/rest/User.java @@ -0,0 +1,77 @@ +/* + * + * 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.microservices.sc.rest; + +import org.codehaus.jackson.annotate.JsonProperty; + +import javax.validation.constraints.Min; +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Size; +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; +import java.io.Serializable; + +@XmlRootElement +@XmlAccessorType(XmlAccessType.FIELD) +public class User implements Serializable { + + @NotNull + @Min(1L) + private Long id; + + @JsonProperty("username") + @XmlElement(name = "username") + @NotNull + @Size(min = 6, max = 50) + private String name; + + public User() { + } + + public User(Long id, String name) { + this.id = id; + this.name = name; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public String toString() { + return "User (" + + "id=" + id + + ", name='" + name + '\'' + + ')'; + } +} diff --git a/dubbo-demo/dubbo-call-sc&dubbo/consumer/src/main/java/org/apache/dubbo/samples/microservices/sc/rest/UserService.java b/dubbo-demo/dubbo-call-sc&dubbo/consumer/src/main/java/org/apache/dubbo/samples/microservices/sc/rest/UserService.java new file mode 100644 index 0000000..06dcb2c --- /dev/null +++ b/dubbo-demo/dubbo-call-sc&dubbo/consumer/src/main/java/org/apache/dubbo/samples/microservices/sc/rest/UserService.java @@ -0,0 +1,45 @@ +/* + * + * 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.microservices.sc.rest; + + +import org.apache.dubbo.rpc.protocol.rest.support.ContentType; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; + +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; + +@Path("users") +@Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_XML}) +@Produces({ContentType.APPLICATION_JSON_UTF_8, ContentType.TEXT_XML_UTF_8}) +@Api(value = "UserService") +public interface UserService { + + @GET + @Path("{id : \\d+}") + @ApiOperation(value = "getUser") + User getUser(@ApiParam(value = "id") @PathParam("id") Long id); +} diff --git a/dubbo-demo/dubbo-call-sc&dubbo/consumer/src/main/java/org/apache/dubbo/samples/microservices/sc/rest/UserServiceImpl.java b/dubbo-demo/dubbo-call-sc&dubbo/consumer/src/main/java/org/apache/dubbo/samples/microservices/sc/rest/UserServiceImpl.java new file mode 100644 index 0000000..fcf8875 --- /dev/null +++ b/dubbo-demo/dubbo-call-sc&dubbo/consumer/src/main/java/org/apache/dubbo/samples/microservices/sc/rest/UserServiceImpl.java @@ -0,0 +1,32 @@ +/* + * + * 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.microservices.sc.rest; + +import java.util.concurrent.atomic.AtomicLong; + +public class UserServiceImpl implements UserService { + + private final AtomicLong idGen = new AtomicLong(); + + @Override + public User getUser(Long id) { + return new User(id, "username" + id); + } + +} diff --git a/dubbo-demo/dubbo-call-sc&dubbo/consumer/src/main/resources/log4j.properties b/dubbo-demo/dubbo-call-sc&dubbo/consumer/src/main/resources/log4j.properties new file mode 100644 index 0000000..15a0900 --- /dev/null +++ b/dubbo-demo/dubbo-call-sc&dubbo/consumer/src/main/resources/log4j.properties @@ -0,0 +1,7 @@ +###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-demo/dubbo-call-sc&dubbo/pom.xml b/dubbo-demo/dubbo-call-sc&dubbo/pom.xml new file mode 100644 index 0000000..46625c4 --- /dev/null +++ b/dubbo-demo/dubbo-call-sc&dubbo/pom.xml @@ -0,0 +1,61 @@ +<?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"> + <parent> + <groupId>org.apache.dubbo</groupId> + <artifactId>dubbo-demo</artifactId> + <version>${revision}</version> + <relativePath>../pom.xml</relativePath> + </parent> + <modelVersion>4.0.0</modelVersion> + <packaging>pom</packaging> + + <modules> + <module>provider</module> + <module>consumer</module> + </modules> + + <properties> + <skip_maven_deploy>true</skip_maven_deploy> + <spring-boot-maven-plugin.version>2.1.4.RELEASE</spring-boot-maven-plugin.version> + </properties> + + <artifactId>dubbo-call-sc</artifactId> + + <build> + <plugins> + <plugin> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-maven-plugin</artifactId> + <version>${spring-boot-maven-plugin.version}</version> + <executions> + <execution> + <goals> + <goal>repackage</goal> + </goals> + </execution> + </executions> + </plugin> + </plugins> + </build> + +</project> diff --git a/dubbo-demo/dubbo-call-sc&dubbo/provider/pom.xml b/dubbo-demo/dubbo-call-sc&dubbo/provider/pom.xml new file mode 100644 index 0000000..f3a3ea0 --- /dev/null +++ b/dubbo-demo/dubbo-call-sc&dubbo/provider/pom.xml @@ -0,0 +1,83 @@ +<?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"> + <parent> + <groupId>org.apache.dubbo</groupId> + <artifactId>dubbo-call-sc</artifactId> + <version>${revision}</version> + <relativePath>../pom.xml</relativePath> + </parent> + <modelVersion>4.0.0</modelVersion> + + <artifactId>provider</artifactId> + + <dependencyManagement> + <dependencies> + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-dependencies</artifactId> + <version>${spring-cloud-dependencies.version}</version> + <type>pom</type> + <scope>import</scope> + </dependency> + </dependencies> + </dependencyManagement> + + <dependencies> + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-context</artifactId> + </dependency> + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-starter-feign</artifactId> + </dependency> + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-starter-ribbon</artifactId> + </dependency> + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-starter-consul-all</artifactId> + <version>${spring-cloud-starter-consul.version}</version> + </dependency> + + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-starter-consul-config</artifactId> + <version>${spring-cloud-starter-consul.version}</version> + </dependency> + + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-test</artifactId> + <version>${spring-boot-starter-test.version}</version> + <scope>test</scope> + </dependency> + </dependencies> + + <properties> + <spring-cloud-starter-consul.version>1.3.0.RELEASE</spring-cloud-starter-consul.version> + <spring-boot-starter-test.version>1.5.10.RELEASE</spring-boot-starter-test.version> + <spring-cloud-dependencies.version>Dalston.SR4</spring-cloud-dependencies.version> + </properties> +</project> diff --git a/dubbo-demo/dubbo-call-sc&dubbo/provider/src/main/java/org/apache/dubbo/samples/microservices/sc/Application.java b/dubbo-demo/dubbo-call-sc&dubbo/provider/src/main/java/org/apache/dubbo/samples/microservices/sc/Application.java new file mode 100644 index 0000000..7244b58 --- /dev/null +++ b/dubbo-demo/dubbo-call-sc&dubbo/provider/src/main/java/org/apache/dubbo/samples/microservices/sc/Application.java @@ -0,0 +1,15 @@ +package org.apache.dubbo.samples.microservices.sc; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +import org.springframework.cloud.netflix.feign.EnableFeignClients; + +@SpringBootApplication +@EnableDiscoveryClient +@EnableFeignClients() +public class Application { + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} \ No newline at end of file diff --git a/dubbo-demo/dubbo-call-sc&dubbo/provider/src/main/java/org/apache/dubbo/samples/microservices/sc/configuration/RestTemplateConfiguration.java b/dubbo-demo/dubbo-call-sc&dubbo/provider/src/main/java/org/apache/dubbo/samples/microservices/sc/configuration/RestTemplateConfiguration.java new file mode 100644 index 0000000..85cc275 --- /dev/null +++ b/dubbo-demo/dubbo-call-sc&dubbo/provider/src/main/java/org/apache/dubbo/samples/microservices/sc/configuration/RestTemplateConfiguration.java @@ -0,0 +1,16 @@ +package org.apache.dubbo.samples.microservices.sc.configuration; + +import org.springframework.cloud.client.loadbalancer.LoadBalanced; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.client.RestTemplate; + +@Configuration +public class RestTemplateConfiguration { + + @Bean + @LoadBalanced + RestTemplate loadBalancedRestTemplate() { + return new RestTemplate(); + } +} diff --git a/dubbo-demo/dubbo-call-sc&dubbo/provider/src/main/java/org/apache/dubbo/samples/microservices/sc/controller/MeController.java b/dubbo-demo/dubbo-call-sc&dubbo/provider/src/main/java/org/apache/dubbo/samples/microservices/sc/controller/MeController.java new file mode 100644 index 0000000..2fcf3e0 --- /dev/null +++ b/dubbo-demo/dubbo-call-sc&dubbo/provider/src/main/java/org/apache/dubbo/samples/microservices/sc/controller/MeController.java @@ -0,0 +1,23 @@ +package org.apache.dubbo.samples.microservices.sc.controller; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class MeController { + + private String instanceId; + + @Autowired + public MeController(@Value("${spring.cloud.consul.discovery.instance-id:${random.value}}") String instanceId) { + this.instanceId = instanceId; + } + + @GetMapping("/me") + public String me() { + return instanceId; + } + +} diff --git a/dubbo-demo/dubbo-call-sc&dubbo/provider/src/main/java/org/apache/dubbo/samples/microservices/sc/controller/PropertyController.java b/dubbo-demo/dubbo-call-sc&dubbo/provider/src/main/java/org/apache/dubbo/samples/microservices/sc/controller/PropertyController.java new file mode 100644 index 0000000..cb24179 --- /dev/null +++ b/dubbo-demo/dubbo-call-sc&dubbo/provider/src/main/java/org/apache/dubbo/samples/microservices/sc/controller/PropertyController.java @@ -0,0 +1,45 @@ +package org.apache.dubbo.samples.microservices.sc.controller; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.cloud.context.config.annotation.RefreshScope; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RefreshScope +@RestController +@RequestMapping("/property") +public class PropertyController { + + @Value("${applicationProp:default value}") + private String applicationProperty; + + @Value("${property:default value}") + private String property; + + @Value("${property2:default value}") + private String property2; + + @Value("${infrastructureDesc:default value}") + private String infrastructureDesc; + + @Value("${infrastructureUserName:default value}") + private String infrastructureUserName; + + @Value("${infrastructurePassword:default value}") + private String infrastructurePassword; + + @Value("${password:mypassword}") + private String secret; + + @RequestMapping + public String getProperty() { + return "<h3>applicationProperty:</h3> " + applicationProperty + "</br>" + + "<h3>property:</h3> " + property + "</br>" + + "<h3>property2:</h3> " + property2 + "</br>" + + "<h3>infrastructureDesc:</h3> " + infrastructureDesc + "</br>" + + "<h3>infrastructureUserName:</h3> " + infrastructureUserName + "</br>" + + "<h3>infrastructurePassword:</h3> " + infrastructurePassword + "</br>" + + "<h3>vault password:</h3>" + secret + "</br>"; + } + +} diff --git a/dubbo-demo/dubbo-call-sc&dubbo/provider/src/main/java/org/apache/dubbo/samples/microservices/sc/controller/TestController.java b/dubbo-demo/dubbo-call-sc&dubbo/provider/src/main/java/org/apache/dubbo/samples/microservices/sc/controller/TestController.java new file mode 100644 index 0000000..615e998 --- /dev/null +++ b/dubbo-demo/dubbo-call-sc&dubbo/provider/src/main/java/org/apache/dubbo/samples/microservices/sc/controller/TestController.java @@ -0,0 +1,46 @@ +package org.apache.dubbo.samples.microservices.sc.controller; + +import org.apache.dubbo.samples.microservices.sc.feign.TestFeign; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.RestTemplate; + +@RestController +@RequestMapping("/ping") +public class TestController { + + @Value("${spring.application.name}") + private String appName; + + private final RestTemplate restTemplate; + private final TestFeign testFeign; + + public TestController(RestTemplate restTemplate, + TestFeign testFeign) { + this.restTemplate = restTemplate; + this.testFeign = testFeign; + } + + @RequestMapping + public String doAlive() { + return "Alive!"; + } + + @RequestMapping("/rest") + public String doRestAlive() { + return new RestTemplate().getForObject("http://localhost:8099/ping", String.class); + } + + @RequestMapping("/rest/ribbon") + public String doRestAliveUsingEurekaAndRibbon() { + String url = "http://dubbo-provider-demo/users/1"; + System.out.println("url: "+url); + return restTemplate.getForObject(url, String.class); + } + + @RequestMapping("/rest/feign") + public String doRestAliveUsingFeign() { + return testFeign.doAlive(); + } +} \ No newline at end of file diff --git a/dubbo-demo/dubbo-call-sc&dubbo/provider/src/main/java/org/apache/dubbo/samples/microservices/sc/feign/TestFeign.java b/dubbo-demo/dubbo-call-sc&dubbo/provider/src/main/java/org/apache/dubbo/samples/microservices/sc/feign/TestFeign.java new file mode 100644 index 0000000..6451c89 --- /dev/null +++ b/dubbo-demo/dubbo-call-sc&dubbo/provider/src/main/java/org/apache/dubbo/samples/microservices/sc/feign/TestFeign.java @@ -0,0 +1,13 @@ +package org.apache.dubbo.samples.microservices.sc.feign; + +import org.springframework.cloud.netflix.feign.FeignClient; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +@FeignClient(name = "dubbo-provider-demo") +public interface TestFeign { + + @RequestMapping(value="/users/1", method = RequestMethod.GET) + String doAlive(); + +} diff --git a/dubbo-demo/dubbo-call-sc&dubbo/provider/src/main/resources/application.yml b/dubbo-demo/dubbo-call-sc&dubbo/provider/src/main/resources/application.yml new file mode 100644 index 0000000..869e5ae --- /dev/null +++ b/dubbo-demo/dubbo-call-sc&dubbo/provider/src/main/resources/application.yml @@ -0,0 +1,6 @@ +management: + security: + enabled: false +endpoints: + shutdown: + enabled: true diff --git a/dubbo-demo/dubbo-call-sc&dubbo/provider/src/main/resources/bootstrap.yml b/dubbo-demo/dubbo-call-sc&dubbo/provider/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..cabb49f --- /dev/null +++ b/dubbo-demo/dubbo-call-sc&dubbo/provider/src/main/resources/bootstrap.yml @@ -0,0 +1,24 @@ +server: + port: 8099 + +spring: + application: + name: spring-cloud-provider-for-dubbo + cloud: + consul: + host: localhost + port: 8500 + enabled: true + discovery: + instance-id: ${spring.application.name}:${random.value} + service-name: ${spring.application.name} + config: + format: yaml + prefix: config + defaultContext: application + data-key: data + profileSeparator: '-' + +management: + security: + enabled: false \ No newline at end of file diff --git a/dubbo-demo/dubbo-call-sc/README.md b/dubbo-demo/dubbo-call-sc/README.md new file mode 100644 index 0000000..e42c3e7 --- /dev/null +++ b/dubbo-demo/dubbo-call-sc/README.md @@ -0,0 +1,7 @@ +Dubbo自动发现并消费原生Spring Cloud服务,以Consul为注册中心。 + +* Spring MVC方式开发服务端 +* 以标准Dubbo方式开发客户端 + * 首先,定义标准JAX-RS注解的接口(与Provider端相对应) + * Registry指定 + * 同时消费,要使用Multiple Registry策略 \ No newline at end of file diff --git a/dubbo-demo/dubbo-call-sc/consumer/pom.xml b/dubbo-demo/dubbo-call-sc/consumer/pom.xml new file mode 100644 index 0000000..f1d9dfd --- /dev/null +++ b/dubbo-demo/dubbo-call-sc/consumer/pom.xml @@ -0,0 +1,81 @@ +<?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"> + <parent> + <groupId>org.apache.dubbo</groupId> + <artifactId>dubbo-call-sc</artifactId> + <version>${revision}</version> + <relativePath>../pom.xml</relativePath> + </parent> + <modelVersion>4.0.0</modelVersion> + + <artifactId>consumer</artifactId> + + <properties> + <skip_maven_deploy>true</skip_maven_deploy> + </properties> + + <dependencies> + <dependency> + <groupId>org.apache.dubbo</groupId> + <artifactId>dubbo-config-api</artifactId> + <version>${project.parent.version}</version> + </dependency> + <dependency> + <groupId>org.apache.dubbo</groupId> + <artifactId>dubbo-demo-interface</artifactId> + <version>${project.parent.version}</version> + </dependency> + <dependency> + <groupId>org.apache.dubbo</groupId> + <artifactId>dubbo-registry-multicast</artifactId> + </dependency> + <dependency> + <groupId>org.apache.dubbo</groupId> + <artifactId>dubbo-rpc-dubbo</artifactId> + </dependency> + <dependency> + <groupId>org.apache.dubbo</groupId> + <artifactId>dubbo-remoting-netty4</artifactId> + </dependency> + <dependency> + <groupId>org.apache.dubbo</groupId> + <artifactId>dubbo-serialization-hessian2</artifactId> + </dependency> + <dependency> + <groupId>org.apache.dubbo</groupId> + <artifactId>dubbo-bootstrap</artifactId> + <version>${project.parent.version}</version> + </dependency> + <dependency> + <groupId>org.apache.dubbo</groupId> + <artifactId>dubbo-registry-consul</artifactId> + <version>${project.parent.version}</version> + </dependency> + <dependency> + <groupId>org.apache.dubbo</groupId> + <artifactId>dubbo-rpc-rest</artifactId> + <version>${project.parent.version}</version> + </dependency> + </dependencies> + +</project> diff --git a/dubbo-demo/dubbo-call-sc/consumer/src/main/java/org/apache/dubbo/samples/microservices/sc/Application.java b/dubbo-demo/dubbo-call-sc/consumer/src/main/java/org/apache/dubbo/samples/microservices/sc/Application.java new file mode 100644 index 0000000..3a017a2 --- /dev/null +++ b/dubbo-demo/dubbo-call-sc/consumer/src/main/java/org/apache/dubbo/samples/microservices/sc/Application.java @@ -0,0 +1,55 @@ +/* + * 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.microservices.sc; + +import org.apache.dubbo.bootstrap.DubboBootstrap; +import org.apache.dubbo.config.ReferenceConfig; +import org.apache.dubbo.config.RegistryConfig; +import org.apache.dubbo.config.context.ConfigManager; +import org.apache.dubbo.samples.microservices.sc.rest.UserService; + +public class Application { + public static void main(String[] args) throws Exception{ + RegistryConfig interfaceRegistry = new RegistryConfig(); + interfaceRegistry.setId("interfaceRegistry"); + interfaceRegistry.setAddress("zookeeper://127.0.0.1:2181?" + + "registry.type=service&subscribed.services=spring-cloud-provider-for-dubbo"); + + new DubboBootstrap() + .application("dubbo-consumer-demo") + // Zookeeper + .registry(interfaceRegistry) + // Nacos +// .registry("consul", builder -> builder.address("consul://127.0.0.1:8500?registry.type=service&subscribed.services=dubbo-provider-demo")) + .reference("user", builder -> builder.interfaceClass(UserService.class).protocol("rest")) + .onlyRegisterProvider(true) + .start() + .await(); + + ConfigManager configManager = ConfigManager.getInstance(); + + ReferenceConfig<UserService> referenceConfig1 = configManager.getReferenceConfig("user"); + UserService userService = referenceConfig1.get(); + + for (int i = 0; i < 500; i++) { + Thread.sleep(2000L); + System.out.println(userService.getUser(1L)); + } + } +} diff --git a/dubbo-demo/dubbo-call-sc/consumer/src/main/java/org/apache/dubbo/samples/microservices/sc/rest/User.java b/dubbo-demo/dubbo-call-sc/consumer/src/main/java/org/apache/dubbo/samples/microservices/sc/rest/User.java new file mode 100644 index 0000000..c365f8f --- /dev/null +++ b/dubbo-demo/dubbo-call-sc/consumer/src/main/java/org/apache/dubbo/samples/microservices/sc/rest/User.java @@ -0,0 +1,77 @@ +/* + * + * 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.microservices.sc.rest; + +import org.codehaus.jackson.annotate.JsonProperty; + +import javax.validation.constraints.Min; +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Size; +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; +import java.io.Serializable; + +@XmlRootElement +@XmlAccessorType(XmlAccessType.FIELD) +public class User implements Serializable { + + @NotNull + @Min(1L) + private Long id; + + @JsonProperty("username") + @XmlElement(name = "username") + @NotNull + @Size(min = 6, max = 50) + private String name; + + public User() { + } + + public User(Long id, String name) { + this.id = id; + this.name = name; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public String toString() { + return "User (" + + "id=" + id + + ", name='" + name + '\'' + + ')'; + } +} diff --git a/dubbo-demo/dubbo-call-sc/consumer/src/main/java/org/apache/dubbo/samples/microservices/sc/rest/UserService.java b/dubbo-demo/dubbo-call-sc/consumer/src/main/java/org/apache/dubbo/samples/microservices/sc/rest/UserService.java new file mode 100644 index 0000000..06dcb2c --- /dev/null +++ b/dubbo-demo/dubbo-call-sc/consumer/src/main/java/org/apache/dubbo/samples/microservices/sc/rest/UserService.java @@ -0,0 +1,45 @@ +/* + * + * 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.microservices.sc.rest; + + +import org.apache.dubbo.rpc.protocol.rest.support.ContentType; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; + +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; + +@Path("users") +@Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_XML}) +@Produces({ContentType.APPLICATION_JSON_UTF_8, ContentType.TEXT_XML_UTF_8}) +@Api(value = "UserService") +public interface UserService { + + @GET + @Path("{id : \\d+}") + @ApiOperation(value = "getUser") + User getUser(@ApiParam(value = "id") @PathParam("id") Long id); +} diff --git a/dubbo-demo/dubbo-call-sc/consumer/src/main/java/org/apache/dubbo/samples/microservices/sc/rest/UserServiceImpl.java b/dubbo-demo/dubbo-call-sc/consumer/src/main/java/org/apache/dubbo/samples/microservices/sc/rest/UserServiceImpl.java new file mode 100644 index 0000000..fcf8875 --- /dev/null +++ b/dubbo-demo/dubbo-call-sc/consumer/src/main/java/org/apache/dubbo/samples/microservices/sc/rest/UserServiceImpl.java @@ -0,0 +1,32 @@ +/* + * + * 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.microservices.sc.rest; + +import java.util.concurrent.atomic.AtomicLong; + +public class UserServiceImpl implements UserService { + + private final AtomicLong idGen = new AtomicLong(); + + @Override + public User getUser(Long id) { + return new User(id, "username" + id); + } + +} diff --git a/dubbo-demo/dubbo-call-sc/consumer/src/main/resources/log4j.properties b/dubbo-demo/dubbo-call-sc/consumer/src/main/resources/log4j.properties new file mode 100644 index 0000000..15a0900 --- /dev/null +++ b/dubbo-demo/dubbo-call-sc/consumer/src/main/resources/log4j.properties @@ -0,0 +1,7 @@ +###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-demo/dubbo-call-sc/pom.xml b/dubbo-demo/dubbo-call-sc/pom.xml new file mode 100644 index 0000000..46625c4 --- /dev/null +++ b/dubbo-demo/dubbo-call-sc/pom.xml @@ -0,0 +1,61 @@ +<?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"> + <parent> + <groupId>org.apache.dubbo</groupId> + <artifactId>dubbo-demo</artifactId> + <version>${revision}</version> + <relativePath>../pom.xml</relativePath> + </parent> + <modelVersion>4.0.0</modelVersion> + <packaging>pom</packaging> + + <modules> + <module>provider</module> + <module>consumer</module> + </modules> + + <properties> + <skip_maven_deploy>true</skip_maven_deploy> + <spring-boot-maven-plugin.version>2.1.4.RELEASE</spring-boot-maven-plugin.version> + </properties> + + <artifactId>dubbo-call-sc</artifactId> + + <build> + <plugins> + <plugin> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-maven-plugin</artifactId> + <version>${spring-boot-maven-plugin.version}</version> + <executions> + <execution> + <goals> + <goal>repackage</goal> + </goals> + </execution> + </executions> + </plugin> + </plugins> + </build> + +</project> diff --git a/dubbo-demo/dubbo-call-sc/provider/pom.xml b/dubbo-demo/dubbo-call-sc/provider/pom.xml new file mode 100644 index 0000000..f3a3ea0 --- /dev/null +++ b/dubbo-demo/dubbo-call-sc/provider/pom.xml @@ -0,0 +1,83 @@ +<?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"> + <parent> + <groupId>org.apache.dubbo</groupId> + <artifactId>dubbo-call-sc</artifactId> + <version>${revision}</version> + <relativePath>../pom.xml</relativePath> + </parent> + <modelVersion>4.0.0</modelVersion> + + <artifactId>provider</artifactId> + + <dependencyManagement> + <dependencies> + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-dependencies</artifactId> + <version>${spring-cloud-dependencies.version}</version> + <type>pom</type> + <scope>import</scope> + </dependency> + </dependencies> + </dependencyManagement> + + <dependencies> + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-context</artifactId> + </dependency> + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-starter-feign</artifactId> + </dependency> + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-starter-ribbon</artifactId> + </dependency> + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-starter-consul-all</artifactId> + <version>${spring-cloud-starter-consul.version}</version> + </dependency> + + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-starter-consul-config</artifactId> + <version>${spring-cloud-starter-consul.version}</version> + </dependency> + + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-test</artifactId> + <version>${spring-boot-starter-test.version}</version> + <scope>test</scope> + </dependency> + </dependencies> + + <properties> + <spring-cloud-starter-consul.version>1.3.0.RELEASE</spring-cloud-starter-consul.version> + <spring-boot-starter-test.version>1.5.10.RELEASE</spring-boot-starter-test.version> + <spring-cloud-dependencies.version>Dalston.SR4</spring-cloud-dependencies.version> + </properties> +</project> diff --git a/dubbo-demo/dubbo-call-sc/provider/src/main/java/org/apache/dubbo/samples/microservices/sc/Application.java b/dubbo-demo/dubbo-call-sc/provider/src/main/java/org/apache/dubbo/samples/microservices/sc/Application.java new file mode 100644 index 0000000..7244b58 --- /dev/null +++ b/dubbo-demo/dubbo-call-sc/provider/src/main/java/org/apache/dubbo/samples/microservices/sc/Application.java @@ -0,0 +1,15 @@ +package org.apache.dubbo.samples.microservices.sc; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +import org.springframework.cloud.netflix.feign.EnableFeignClients; + +@SpringBootApplication +@EnableDiscoveryClient +@EnableFeignClients() +public class Application { + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} \ No newline at end of file diff --git a/dubbo-demo/dubbo-call-sc/provider/src/main/java/org/apache/dubbo/samples/microservices/sc/configuration/RestTemplateConfiguration.java b/dubbo-demo/dubbo-call-sc/provider/src/main/java/org/apache/dubbo/samples/microservices/sc/configuration/RestTemplateConfiguration.java new file mode 100644 index 0000000..85cc275 --- /dev/null +++ b/dubbo-demo/dubbo-call-sc/provider/src/main/java/org/apache/dubbo/samples/microservices/sc/configuration/RestTemplateConfiguration.java @@ -0,0 +1,16 @@ +package org.apache.dubbo.samples.microservices.sc.configuration; + +import org.springframework.cloud.client.loadbalancer.LoadBalanced; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.client.RestTemplate; + +@Configuration +public class RestTemplateConfiguration { + + @Bean + @LoadBalanced + RestTemplate loadBalancedRestTemplate() { + return new RestTemplate(); + } +} diff --git a/dubbo-demo/dubbo-call-sc/provider/src/main/java/org/apache/dubbo/samples/microservices/sc/controller/MeController.java b/dubbo-demo/dubbo-call-sc/provider/src/main/java/org/apache/dubbo/samples/microservices/sc/controller/MeController.java new file mode 100644 index 0000000..2fcf3e0 --- /dev/null +++ b/dubbo-demo/dubbo-call-sc/provider/src/main/java/org/apache/dubbo/samples/microservices/sc/controller/MeController.java @@ -0,0 +1,23 @@ +package org.apache.dubbo.samples.microservices.sc.controller; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class MeController { + + private String instanceId; + + @Autowired + public MeController(@Value("${spring.cloud.consul.discovery.instance-id:${random.value}}") String instanceId) { + this.instanceId = instanceId; + } + + @GetMapping("/me") + public String me() { + return instanceId; + } + +} diff --git a/dubbo-demo/dubbo-call-sc/provider/src/main/java/org/apache/dubbo/samples/microservices/sc/controller/PropertyController.java b/dubbo-demo/dubbo-call-sc/provider/src/main/java/org/apache/dubbo/samples/microservices/sc/controller/PropertyController.java new file mode 100644 index 0000000..cb24179 --- /dev/null +++ b/dubbo-demo/dubbo-call-sc/provider/src/main/java/org/apache/dubbo/samples/microservices/sc/controller/PropertyController.java @@ -0,0 +1,45 @@ +package org.apache.dubbo.samples.microservices.sc.controller; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.cloud.context.config.annotation.RefreshScope; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RefreshScope +@RestController +@RequestMapping("/property") +public class PropertyController { + + @Value("${applicationProp:default value}") + private String applicationProperty; + + @Value("${property:default value}") + private String property; + + @Value("${property2:default value}") + private String property2; + + @Value("${infrastructureDesc:default value}") + private String infrastructureDesc; + + @Value("${infrastructureUserName:default value}") + private String infrastructureUserName; + + @Value("${infrastructurePassword:default value}") + private String infrastructurePassword; + + @Value("${password:mypassword}") + private String secret; + + @RequestMapping + public String getProperty() { + return "<h3>applicationProperty:</h3> " + applicationProperty + "</br>" + + "<h3>property:</h3> " + property + "</br>" + + "<h3>property2:</h3> " + property2 + "</br>" + + "<h3>infrastructureDesc:</h3> " + infrastructureDesc + "</br>" + + "<h3>infrastructureUserName:</h3> " + infrastructureUserName + "</br>" + + "<h3>infrastructurePassword:</h3> " + infrastructurePassword + "</br>" + + "<h3>vault password:</h3>" + secret + "</br>"; + } + +} diff --git a/dubbo-demo/dubbo-call-sc/provider/src/main/java/org/apache/dubbo/samples/microservices/sc/controller/TestController.java b/dubbo-demo/dubbo-call-sc/provider/src/main/java/org/apache/dubbo/samples/microservices/sc/controller/TestController.java new file mode 100644 index 0000000..96801f8 --- /dev/null +++ b/dubbo-demo/dubbo-call-sc/provider/src/main/java/org/apache/dubbo/samples/microservices/sc/controller/TestController.java @@ -0,0 +1,54 @@ +package org.apache.dubbo.samples.microservices.sc.controller; + +import org.apache.dubbo.samples.microservices.sc.feign.TestFeign; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.RestTemplate; + +@RestController +@RequestMapping("/users") +//@RequestMapping("/ping") +public class TestController { + + @Value("${spring.application.name}") + private String appName; + + private final RestTemplate restTemplate; + private final TestFeign testFeign; + + public TestController(RestTemplate restTemplate, + TestFeign testFeign) { + this.restTemplate = restTemplate; + this.testFeign = testFeign; + } + + @RequestMapping("{id}") + public User getUser(@PathVariable("id") Long id) { + return new User(id, "username-" + id); + } + + + @RequestMapping + public String doAlive() { + return "Alive!"; + } + + @RequestMapping("/rest") + public String doRestAlive() { + return new RestTemplate().getForObject("http://localhost:8099/ping", String.class); + } + + @RequestMapping("/rest/ribbon") + public String doRestAliveUsingEurekaAndRibbon() { + String url = "http://dubbo-provider-demo/users/1"; + System.out.println("url: "+url); + return restTemplate.getForObject(url, String.class); + } + + @RequestMapping("/rest/feign") + public String doRestAliveUsingFeign() { + return testFeign.doAlive(); + } +} \ No newline at end of file diff --git a/dubbo-demo/dubbo-call-sc/provider/src/main/java/org/apache/dubbo/samples/microservices/sc/controller/User.java b/dubbo-demo/dubbo-call-sc/provider/src/main/java/org/apache/dubbo/samples/microservices/sc/controller/User.java new file mode 100644 index 0000000..dd163f1 --- /dev/null +++ b/dubbo-demo/dubbo-call-sc/provider/src/main/java/org/apache/dubbo/samples/microservices/sc/controller/User.java @@ -0,0 +1,60 @@ +/* + * + * 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.microservices.sc.controller; + +import java.io.Serializable; + +public class User implements Serializable { + + private Long id; + + private String name; + + public User() { + } + + public User(Long id, String name) { + this.id = id; + this.name = name; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public String toString() { + return "User (" + + "id=" + id + + ", name='" + name + '\'' + + ')'; + } +} diff --git a/dubbo-demo/dubbo-call-sc/provider/src/main/java/org/apache/dubbo/samples/microservices/sc/feign/TestFeign.java b/dubbo-demo/dubbo-call-sc/provider/src/main/java/org/apache/dubbo/samples/microservices/sc/feign/TestFeign.java new file mode 100644 index 0000000..6451c89 --- /dev/null +++ b/dubbo-demo/dubbo-call-sc/provider/src/main/java/org/apache/dubbo/samples/microservices/sc/feign/TestFeign.java @@ -0,0 +1,13 @@ +package org.apache.dubbo.samples.microservices.sc.feign; + +import org.springframework.cloud.netflix.feign.FeignClient; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +@FeignClient(name = "dubbo-provider-demo") +public interface TestFeign { + + @RequestMapping(value="/users/1", method = RequestMethod.GET) + String doAlive(); + +} diff --git a/dubbo-demo/dubbo-call-sc/provider/src/main/resources/application.yml b/dubbo-demo/dubbo-call-sc/provider/src/main/resources/application.yml new file mode 100644 index 0000000..869e5ae --- /dev/null +++ b/dubbo-demo/dubbo-call-sc/provider/src/main/resources/application.yml @@ -0,0 +1,6 @@ +management: + security: + enabled: false +endpoints: + shutdown: + enabled: true diff --git a/dubbo-demo/dubbo-call-sc/provider/src/main/resources/bootstrap.yml b/dubbo-demo/dubbo-call-sc/provider/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..cabb49f --- /dev/null +++ b/dubbo-demo/dubbo-call-sc/provider/src/main/resources/bootstrap.yml @@ -0,0 +1,24 @@ +server: + port: 8099 + +spring: + application: + name: spring-cloud-provider-for-dubbo + cloud: + consul: + host: localhost + port: 8500 + enabled: true + discovery: + instance-id: ${spring.application.name}:${random.value} + service-name: ${spring.application.name} + config: + format: yaml + prefix: config + defaultContext: application + data-key: data + profileSeparator: '-' + +management: + security: + enabled: false \ No newline at end of file diff --git a/dubbo-demo/pom.xml b/dubbo-demo/pom.xml index e636a23..4c03eda 100644 --- a/dubbo-demo/pom.xml +++ b/dubbo-demo/pom.xml @@ -35,6 +35,7 @@ <module>dubbo-demo-xml</module> <module>dubbo-demo-annotation</module> <module>dubbo-demo-api</module> + <!-- <module>dubbo-call-sc</module>--> </modules> <dependencyManagement> diff --git a/dubbo-demo/sc-call-dubbo/consumer/pom.xml b/dubbo-demo/sc-call-dubbo/consumer/pom.xml new file mode 100644 index 0000000..e938869 --- /dev/null +++ b/dubbo-demo/sc-call-dubbo/consumer/pom.xml @@ -0,0 +1,84 @@ +<?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"> + <parent> + <groupId>org.apache.dubbo</groupId> + <artifactId>dubbo-call-sc</artifactId> + <version>${revision}</version> + <relativePath>../pom.xml</relativePath> + </parent> + <modelVersion>4.0.0</modelVersion> + + <artifactId>consumer</artifactId> + + + <dependencyManagement> + <dependencies> + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-dependencies</artifactId> + <version>${spring-cloud-dependencies.version}</version> + <type>pom</type> + <scope>import</scope> + </dependency> + </dependencies> + </dependencyManagement> + + <dependencies> + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-context</artifactId> + </dependency> + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-starter-feign</artifactId> + </dependency> + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-starter-ribbon</artifactId> + </dependency> + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-starter-consul-all</artifactId> + <version>${spring-cloud-starter-consul.version}</version> + </dependency> + + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-starter-consul-config</artifactId> + <version>${spring-cloud-starter-consul.version}</version> + </dependency> + + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-test</artifactId> + <version>${spring-boot-starter-test.version}</version> + <scope>test</scope> + </dependency> + </dependencies> + + <properties> + <spring-cloud-starter-consul.version>1.3.0.RELEASE</spring-cloud-starter-consul.version> + <spring-boot-starter-test.version>1.5.10.RELEASE</spring-boot-starter-test.version> + <spring-cloud-dependencies.version>Dalston.SR4</spring-cloud-dependencies.version> + </properties> +</project> diff --git a/dubbo-demo/sc-call-dubbo/consumer/src/main/java/com/bettercloud/spring/cloud/Application.java b/dubbo-demo/sc-call-dubbo/consumer/src/main/java/com/bettercloud/spring/cloud/Application.java new file mode 100644 index 0000000..e1f07cb --- /dev/null +++ b/dubbo-demo/sc-call-dubbo/consumer/src/main/java/com/bettercloud/spring/cloud/Application.java @@ -0,0 +1,15 @@ +package com.bettercloud.spring.cloud; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +import org.springframework.cloud.netflix.feign.EnableFeignClients; + +@SpringBootApplication +@EnableDiscoveryClient +@EnableFeignClients +public class Application { + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} \ No newline at end of file diff --git a/dubbo-demo/sc-call-dubbo/consumer/src/main/java/com/bettercloud/spring/cloud/configuration/RestTemplateConfiguration.java b/dubbo-demo/sc-call-dubbo/consumer/src/main/java/com/bettercloud/spring/cloud/configuration/RestTemplateConfiguration.java new file mode 100644 index 0000000..cf0a2b1 --- /dev/null +++ b/dubbo-demo/sc-call-dubbo/consumer/src/main/java/com/bettercloud/spring/cloud/configuration/RestTemplateConfiguration.java @@ -0,0 +1,17 @@ +package com.bettercloud.spring.cloud.configuration; + +import org.springframework.cloud.client.loadbalancer.LoadBalanced; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.client.RestTemplate; + +@Configuration +public class RestTemplateConfiguration { + + @Bean + @LoadBalanced + RestTemplate loadBalancedRestTemplate() { + return new RestTemplate(); + } + +} diff --git a/dubbo-demo/sc-call-dubbo/consumer/src/main/java/com/bettercloud/spring/cloud/controller/ClientController.java b/dubbo-demo/sc-call-dubbo/consumer/src/main/java/com/bettercloud/spring/cloud/controller/ClientController.java new file mode 100644 index 0000000..b38edf3 --- /dev/null +++ b/dubbo-demo/sc-call-dubbo/consumer/src/main/java/com/bettercloud/spring/cloud/controller/ClientController.java @@ -0,0 +1,29 @@ +package com.bettercloud.spring.cloud.controller; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.RestTemplate; + + +@RestController +@RequestMapping("/client") +public class ClientController { + + private String serviceEndpoint; + private RestTemplate restTemplate; + + @Autowired + public ClientController(@Value("${service.me}") final String serviceEndpoint, final RestTemplate restTemplate) { + this.serviceEndpoint = serviceEndpoint; + this.restTemplate = restTemplate; + } + + @GetMapping + public String getService() { + return restTemplate.getForEntity(serviceEndpoint, String.class).getBody(); + } + +} diff --git a/dubbo-demo/sc-call-dubbo/consumer/src/main/java/com/bettercloud/spring/cloud/feign/TestFeign.java b/dubbo-demo/sc-call-dubbo/consumer/src/main/java/com/bettercloud/spring/cloud/feign/TestFeign.java new file mode 100644 index 0000000..e8663ec --- /dev/null +++ b/dubbo-demo/sc-call-dubbo/consumer/src/main/java/com/bettercloud/spring/cloud/feign/TestFeign.java @@ -0,0 +1,12 @@ +package com.bettercloud.spring.cloud.feign; + +import org.springframework.cloud.netflix.feign.FeignClient; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +@FeignClient(name = "springcloudwebtest") +public interface TestFeign { + + @RequestMapping(value="/ping", method = RequestMethod.GET) + String doAlive(); +} diff --git a/dubbo-demo/sc-call-dubbo/consumer/src/main/resources/application-holding.yml b/dubbo-demo/sc-call-dubbo/consumer/src/main/resources/application-holding.yml new file mode 100644 index 0000000..7a8a421 --- /dev/null +++ b/dubbo-demo/sc-call-dubbo/consumer/src/main/resources/application-holding.yml @@ -0,0 +1,3 @@ +service: + host: http://service-holding + me: ${service.host}/me diff --git a/dubbo-demo/sc-call-dubbo/consumer/src/main/resources/application.yml b/dubbo-demo/sc-call-dubbo/consumer/src/main/resources/application.yml new file mode 100644 index 0000000..bb7bfea --- /dev/null +++ b/dubbo-demo/sc-call-dubbo/consumer/src/main/resources/application.yml @@ -0,0 +1,7 @@ +management: + security: + enabled: false +endpoints: + shutdown: + enabled: true + diff --git a/dubbo-demo/sc-call-dubbo/consumer/src/main/resources/bootstrap.yml b/dubbo-demo/sc-call-dubbo/consumer/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..73dacfc --- /dev/null +++ b/dubbo-demo/sc-call-dubbo/consumer/src/main/resources/bootstrap.yml @@ -0,0 +1,26 @@ +server: + port: 8080 + +spring: + application: + name: client + cloud: + consul: + host: consul + port: 8500 + enabled: true + config: + format: yaml + prefix: config + defaultContext: application + data-key: data + profileSeparator: '-' + vault: + scheme: http + host: vault + authentication: TOKEN + token: ${VAULT_APP_TOKEN} + +management: + security: + enabled: false \ No newline at end of file diff --git a/dubbo-demo/sc-call-dubbo/pom.xml b/dubbo-demo/sc-call-dubbo/pom.xml new file mode 100644 index 0000000..46625c4 --- /dev/null +++ b/dubbo-demo/sc-call-dubbo/pom.xml @@ -0,0 +1,61 @@ +<?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"> + <parent> + <groupId>org.apache.dubbo</groupId> + <artifactId>dubbo-demo</artifactId> + <version>${revision}</version> + <relativePath>../pom.xml</relativePath> + </parent> + <modelVersion>4.0.0</modelVersion> + <packaging>pom</packaging> + + <modules> + <module>provider</module> + <module>consumer</module> + </modules> + + <properties> + <skip_maven_deploy>true</skip_maven_deploy> + <spring-boot-maven-plugin.version>2.1.4.RELEASE</spring-boot-maven-plugin.version> + </properties> + + <artifactId>dubbo-call-sc</artifactId> + + <build> + <plugins> + <plugin> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-maven-plugin</artifactId> + <version>${spring-boot-maven-plugin.version}</version> + <executions> + <execution> + <goals> + <goal>repackage</goal> + </goals> + </execution> + </executions> + </plugin> + </plugins> + </build> + +</project> diff --git a/dubbo-demo/sc-call-dubbo/provider/pom.xml b/dubbo-demo/sc-call-dubbo/provider/pom.xml new file mode 100644 index 0000000..9f98d0e --- /dev/null +++ b/dubbo-demo/sc-call-dubbo/provider/pom.xml @@ -0,0 +1,33 @@ +<?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"> + <parent> + <groupId>org.apache.dubbo</groupId> + <artifactId>dubbo-call-sc</artifactId> + <version>${revision}</version> + <relativePath>../pom.xml</relativePath> + </parent> + <modelVersion>4.0.0</modelVersion> + + <artifactId>provider</artifactId> + +</project> diff --git a/dubbo-demo/sc-call-dubbo/provider/src/main/java/org/apache/dubbo/demo/provider/Application.java b/dubbo-demo/sc-call-dubbo/provider/src/main/java/org/apache/dubbo/demo/provider/Application.java new file mode 100644 index 0000000..5990d75 --- /dev/null +++ b/dubbo-demo/sc-call-dubbo/provider/src/main/java/org/apache/dubbo/demo/provider/Application.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.dubbo.demo.provider; + +import org.apache.dubbo.config.ApplicationConfig; +import org.apache.dubbo.config.RegistryConfig; +import org.apache.dubbo.config.ServiceConfig; +import org.apache.dubbo.demo.DemoService; + +public class Application { + public static void main(String[] args) throws Exception { + ServiceConfig<DemoServiceImpl> service = new ServiceConfig<>(); + service.setApplication(new ApplicationConfig("dubbo-demo-api-provider")); + service.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181")); + service.setInterface(DemoService.class); + service.setRef(new DemoServiceImpl()); + service.export(); + System.in.read(); + } +} diff --git a/dubbo-demo/sc-call-dubbo/provider/src/main/java/org/apache/dubbo/demo/provider/DemoServiceImpl.java b/dubbo-demo/sc-call-dubbo/provider/src/main/java/org/apache/dubbo/demo/provider/DemoServiceImpl.java new file mode 100644 index 0000000..5e2ef23 --- /dev/null +++ b/dubbo-demo/sc-call-dubbo/provider/src/main/java/org/apache/dubbo/demo/provider/DemoServiceImpl.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.demo.provider; + +import org.apache.dubbo.demo.DemoService; +import org.apache.dubbo.rpc.RpcContext; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class DemoServiceImpl implements DemoService { + private static final Logger logger = LoggerFactory.getLogger(DemoServiceImpl.class); + + @Override + public String sayHello(String name) { + logger.info("Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress()); + return "Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress(); + } + +} diff --git a/dubbo-demo/sc-call-dubbo/provider/src/main/resources/log4j.properties b/dubbo-demo/sc-call-dubbo/provider/src/main/resources/log4j.properties new file mode 100644 index 0000000..15a0900 --- /dev/null +++ b/dubbo-demo/sc-call-dubbo/provider/src/main/resources/log4j.properties @@ -0,0 +1,7 @@ +###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-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/RegistryProtocolTest.java b/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/RegistryProtocolTest.java index 192ed58..ff3f130 100644 --- a/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/RegistryProtocolTest.java +++ b/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/RegistryProtocolTest.java @@ -41,6 +41,7 @@ import org.junit.jupiter.api.Test; import java.util.ArrayList; import java.util.List; +import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_PROTOCOL; import static org.apache.dubbo.registry.integration.RegistryProtocol.DEFAULT_REGISTER_PROVIDER_KEYS; import static org.apache.dubbo.rpc.cluster.Constants.EXPORT_KEY; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -62,16 +63,20 @@ public class RegistryProtocolTest { final URL registryUrl = URL.valueOf("registry://127.0.0.1:9090/"); final private Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension(); + public static RegistryProtocol getRegistryProtocol() { + return (RegistryProtocol) ExtensionLoader.getExtensionLoader(Protocol.class).getExtension(REGISTRY_PROTOCOL); + } + @Test public void testDefaultPort() { - RegistryProtocol registryProtocol = RegistryProtocol.getRegistryProtocol(); + RegistryProtocol registryProtocol = getRegistryProtocol(); assertEquals(9090, registryProtocol.getDefaultPort()); } @Test public void testExportUrlNull() { Assertions.assertThrows(IllegalArgumentException.class, () -> { - RegistryProtocol registryProtocol = RegistryProtocol.getRegistryProtocol(); + RegistryProtocol registryProtocol = getRegistryProtocol(); registryProtocol.setCluster(new FailfastCluster()); Protocol dubboProtocol = DubboProtocol.getDubboProtocol(); @@ -84,7 +89,7 @@ public class RegistryProtocolTest { @Test public void testExport() { - RegistryProtocol registryProtocol = RegistryProtocol.getRegistryProtocol(); + RegistryProtocol registryProtocol = getRegistryProtocol(); registryProtocol.setCluster(new FailfastCluster()); registryProtocol.setRegistryFactory(ExtensionLoader.getExtensionLoader(RegistryFactory.class).getAdaptiveExtension()); @@ -107,7 +112,7 @@ public class RegistryProtocolTest { URL newRegistryUrl = registryUrl.addParameter(EXPORT_KEY, serviceUrl); Invoker<RegistryProtocolTest> invoker = new MockInvoker<RegistryProtocolTest>(RegistryProtocolTest.class, newRegistryUrl); Exporter<?> exporter = protocol.export(invoker); - RegistryProtocol rprotocol = RegistryProtocol.getRegistryProtocol(); + RegistryProtocol rprotocol = getRegistryProtocol(); NotifyListener listener = getListener(rprotocol); List<URL> urls = new ArrayList<URL>(); urls.add(URL.valueOf("override://0.0.0.0/?timeout=1000")); @@ -137,7 +142,7 @@ public class RegistryProtocolTest { URL newRegistryUrl = registryUrl.addParameter(EXPORT_KEY, serviceUrl); Invoker<RegistryProtocolTest> invoker = new MockInvoker<RegistryProtocolTest>(RegistryProtocolTest.class, newRegistryUrl); Exporter<?> exporter = protocol.export(invoker); - RegistryProtocol rprotocol = RegistryProtocol.getRegistryProtocol(); + RegistryProtocol rprotocol = getRegistryProtocol(); NotifyListener listener = getListener(rprotocol); List<URL> urls = new ArrayList<URL>(); urls.add(URL.valueOf("override://0.0.0.0/org.apache.dubbo.registry.protocol.HackService?timeout=100")); @@ -168,7 +173,7 @@ public class RegistryProtocolTest { @Test public void testGetParamsToRegistry() { - RegistryProtocol registryProtocol = RegistryProtocol.getRegistryProtocol(); + RegistryProtocol registryProtocol = getRegistryProtocol(); String[] additionalParams = new String[]{"key1", "key2"}; String[] registryParams = registryProtocol.getParamsToRegistry(DEFAULT_REGISTER_PROVIDER_KEYS, additionalParams); String[] expectParams = ArrayUtils.addAll(DEFAULT_REGISTER_PROVIDER_KEYS, additionalParams); @@ -176,7 +181,7 @@ public class RegistryProtocolTest { } private void destroyRegistryProtocol() { - Protocol registry = RegistryProtocol.getRegistryProtocol(); + Protocol registry = getRegistryProtocol(); registry.destroy(); }