From 2e26a2ddb4954ca5736ed5e3725086962b0d4356 Mon Sep 17 00:00:00 2001 From: ganluCode Date: Tue, 11 Aug 2020 17:29:05 +0800 Subject: [PATCH 1/5] ignore --- .gitignore | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.gitignore b/.gitignore index a1c2a23..84c4fb3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,9 @@ + +*.iml +.idea +*/.idea +target + # Compiled class file *.class -- Gitee From ac22bc2801cd6c3e4c1cfde30679700399540f59 Mon Sep 17 00:00:00 2001 From: ganluCode Date: Tue, 11 Aug 2020 17:30:07 +0800 Subject: [PATCH 2/5] ignore --- .gitignore | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.gitignore b/.gitignore index a1c2a23..0277f0a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,9 @@ +*.iml +.idea +*/.idea +target + + # Compiled class file *.class -- Gitee From 049acae0817543c31971e0fb262812beb8e59392 Mon Sep 17 00:00:00 2001 From: ganluCode Date: Tue, 11 Aug 2020 17:31:19 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E5=88=9B=E5=BB=BA=E9=A1=B9=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cloud/spring-cloud-demo/pom.xml | 28 +++++ pom.xml | 215 ++++++++++++++++++++++++++++++++ 2 files changed, 243 insertions(+) create mode 100644 cloud/spring-cloud-demo/pom.xml create mode 100644 pom.xml diff --git a/cloud/spring-cloud-demo/pom.xml b/cloud/spring-cloud-demo/pom.xml new file mode 100644 index 0000000..3387a9d --- /dev/null +++ b/cloud/spring-cloud-demo/pom.xml @@ -0,0 +1,28 @@ + + + + spring-demo + com.ganlucode.demo + 0.0.1-SNAPSHOT + ../../pom.xml + + 4.0.0 + + spring-cloud-demo + + + + + + org.springframework.cloud + spring-cloud-dependencies + Greenwich.SR1 + pom + import + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..455052f --- /dev/null +++ b/pom.xml @@ -0,0 +1,215 @@ + + + 4.0.0 + + com.ganlucode.demo + spring-demo + pom + 0.0.1-SNAPSHOT + + cloud/spring-cloud-demo + + + org.springframework.boot + spring-boot-starter-parent + 2.1.5.RELEASE + + + + UTF-8 + 8 + 20.0 + 2.1.5.RELEASE + 3.3.1 + 2.4 + 3.2.2 + 1.2.56 + 3.3.1.tmp + 1.1.10 + 2.0.1 + 1.0.0-SNAPSHOT + 1.0.0-SNAPSHOT + 2.9.2 + 2.0.1.Final + 5.2.0 + + 1.5.20 + 1.9.6 + + + + + + org.projectlombok + lombok + + + + + org.springframework.boot + spring-boot-starter-log4j2 + + + com.lmax + disruptor + 3.3.4 + + + + com.fasterxml.jackson.dataformat + jackson-dataformat-yaml + + + + org.apache.logging.log4j + log4j-1.2-api + + + + junit + junit + test + + + + + com.alibaba + fastjson + ${fastjson.version} + + + com.google.guava + guava + ${guava.version} + + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + + + commons-collections + commons-collections + ${commons-collections.version} + + + + io.swagger + swagger-annotations + ${swagger.annotions.version} + + + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.jupiter + junit-jupiter-params + ${junit.jupiter.version} + test + + + + + + + + + + io.springfox + springfox-swagger2 + ${swagger.version} + + + io.springfox + springfox-swagger-ui + ${swagger.version} + + + com.github.xiaoymin + swagger-bootstrap-ui + ${swagger.bootstrap.version} + + + + + commons-io + commons-io + ${commons-io.version} + + + javax.validation + validation-api + ${validation.version} + + + de.codecentric + spring-boot-admin-starter-client + 2.1.5 + + + + + com.baomidou + mybatis-plus-boot-starter + ${mybatis-plus.version} + + + com.baomidou + mybatis-plus-generator + ${mybatis-plus.version} + + + com.baomidou + mybatis-plus-annotation + ${mybatis-plus.version} + + + + com.alibaba + druid-spring-boot-starter + ${druid.version} + + + org.springframework.boot + spring-boot-autoconfigure + + + org.springframework.boot + spring-boot-configuration-processor + + + + + mysql + mysql-connector-java + + 5.1.35 + + + + + + + org.springframework.boot + spring-boot-starter + ${springboot.version} + + + org.springframework.boot + spring-boot-starter-logging + + + + + + + + + \ No newline at end of file -- Gitee From 5bab351837cdad338045142af4a033918cdeaed9 Mon Sep 17 00:00:00 2001 From: ganluCode Date: Tue, 11 Aug 2020 20:19:21 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E5=88=9B=E5=BB=BAEureka,demo=20=E7=94=A8Re?= =?UTF-8?q?stTemplate=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cloud-parent}/pom.xml | 14 +++- cloud/common/common-api/pom.xml | 32 ++++++++ cloud/common/common-dao/pom.xml | 37 +++++++++ cloud/common/common-pojo/pom.xml | 41 ++++++++++ cloud/common/common-service/pom.xml | 30 +++++++ cloud/common/common-web/pom.xml | 63 +++++++++++++++ cloud/domain/a/service-a-api/pom.xml | 25 ++++++ cloud/domain/a/service-a-dao/pom.xml | 25 ++++++ cloud/domain/a/service-a-pojo/pom.xml | 16 ++++ cloud/domain/a/service-a-service/pom.xml | 29 +++++++ cloud/domain/a/service-a-web/pom.xml | 26 ++++++ .../springcloud/ServiceAApplication.java | 29 +++++++ .../controller/EurekaController.java | 79 +++++++++++++++++++ .../src/main/resources/application.yml | 23 ++++++ cloud/domain/b/service-b-api/pom.xml | 25 ++++++ cloud/domain/b/service-b-dao/pom.xml | 16 ++++ cloud/domain/b/service-b-pojo/pom.xml | 16 ++++ cloud/domain/b/service-b-service/pom.xml | 30 +++++++ cloud/domain/b/service-b-web/pom.xml | 25 ++++++ .../springcloud/ServiceBApplication.java | 29 +++++++ .../controller/EurekaController.java | 79 +++++++++++++++++++ .../src/main/resources/application.yml | 23 ++++++ cloud/platform/eureka-server/pom.xml | 22 ++++++ .../springcloud/EurekaServerApplication.java | 23 ++++++ .../src/main/resources/application.yml | 13 +++ pom.xml | 3 +- 26 files changed, 769 insertions(+), 4 deletions(-) rename cloud/{spring-cloud-demo => common/cloud-parent}/pom.xml (67%) create mode 100644 cloud/common/common-api/pom.xml create mode 100644 cloud/common/common-dao/pom.xml create mode 100644 cloud/common/common-pojo/pom.xml create mode 100644 cloud/common/common-service/pom.xml create mode 100644 cloud/common/common-web/pom.xml create mode 100644 cloud/domain/a/service-a-api/pom.xml create mode 100644 cloud/domain/a/service-a-dao/pom.xml create mode 100644 cloud/domain/a/service-a-pojo/pom.xml create mode 100644 cloud/domain/a/service-a-service/pom.xml create mode 100644 cloud/domain/a/service-a-web/pom.xml create mode 100644 cloud/domain/a/service-a-web/src/main/java/com/ganlucode/springcloud/ServiceAApplication.java create mode 100644 cloud/domain/a/service-a-web/src/main/java/com/ganlucode/springcloud/controller/EurekaController.java create mode 100644 cloud/domain/a/service-a-web/src/main/resources/application.yml create mode 100644 cloud/domain/b/service-b-api/pom.xml create mode 100644 cloud/domain/b/service-b-dao/pom.xml create mode 100644 cloud/domain/b/service-b-pojo/pom.xml create mode 100644 cloud/domain/b/service-b-service/pom.xml create mode 100644 cloud/domain/b/service-b-web/pom.xml create mode 100644 cloud/domain/b/service-b-web/src/main/java/com/ganlucode/springcloud/ServiceBApplication.java create mode 100644 cloud/domain/b/service-b-web/src/main/java/com/ganlucode/springcloud/controller/EurekaController.java create mode 100644 cloud/domain/b/service-b-web/src/main/resources/application.yml create mode 100644 cloud/platform/eureka-server/pom.xml create mode 100644 cloud/platform/eureka-server/src/main/java/com/ganlucode/springcloud/EurekaServerApplication.java create mode 100644 cloud/platform/eureka-server/src/main/resources/application.yml diff --git a/cloud/spring-cloud-demo/pom.xml b/cloud/common/cloud-parent/pom.xml similarity index 67% rename from cloud/spring-cloud-demo/pom.xml rename to cloud/common/cloud-parent/pom.xml index 3387a9d..da4ba10 100644 --- a/cloud/spring-cloud-demo/pom.xml +++ b/cloud/common/cloud-parent/pom.xml @@ -6,12 +6,20 @@ spring-demo com.ganlucode.demo 0.0.1-SNAPSHOT - ../../pom.xml + ../../../pom.xml 4.0.0 + pom + + ../../domain/a/service-a-api + ../common-api + ../common-dao + ../common-pojo + ../common-service + ../common-web + - spring-cloud-demo - + cloud-parent diff --git a/cloud/common/common-api/pom.xml b/cloud/common/common-api/pom.xml new file mode 100644 index 0000000..95710b3 --- /dev/null +++ b/cloud/common/common-api/pom.xml @@ -0,0 +1,32 @@ + + + + cloud-parent + com.ganlucode.demo + 0.0.1-SNAPSHOT + ../cloud-parent/pom.xml + + 4.0.0 + pom + + ../../domain/b/service-b-api + + + common-api + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.cloud + spring-cloud-starter-openfeign + + + + \ No newline at end of file diff --git a/cloud/common/common-dao/pom.xml b/cloud/common/common-dao/pom.xml new file mode 100644 index 0000000..b59ed90 --- /dev/null +++ b/cloud/common/common-dao/pom.xml @@ -0,0 +1,37 @@ + + + + cloud-parent + com.ganlucode.demo + 0.0.1-SNAPSHOT + ../cloud-parent/pom.xml + + 4.0.0 + pom + + ../../domain/a/service-a-dao + ../../domain/b/service-b-dao + + + common-dao + + + + + com.baomidou + mybatis-plus-boot-starter + + + mysql + mysql-connector-java + + + com.alibaba + druid-spring-boot-starter + + + + + \ No newline at end of file diff --git a/cloud/common/common-pojo/pom.xml b/cloud/common/common-pojo/pom.xml new file mode 100644 index 0000000..567636f --- /dev/null +++ b/cloud/common/common-pojo/pom.xml @@ -0,0 +1,41 @@ + + + + cloud-parent + com.ganlucode.demo + 0.0.1-SNAPSHOT + ../cloud-parent/pom.xml + + 4.0.0 + pom + + ../../domain/a/service-a-pojo + ../../domain/b/service-b-pojo + + + common-pojo + + + + javax.persistence + persistence-api + 1.0 + + + org.hibernate.validator + hibernate-validator + + + + + com.baomidou + mybatis-plus-annotation + ${mybatis-plus.version} + + + + + + \ No newline at end of file diff --git a/cloud/common/common-service/pom.xml b/cloud/common/common-service/pom.xml new file mode 100644 index 0000000..a446e0c --- /dev/null +++ b/cloud/common/common-service/pom.xml @@ -0,0 +1,30 @@ + + + + cloud-parent + com.ganlucode.demo + 0.0.1-SNAPSHOT + ../cloud-parent/pom.xml + + 4.0.0 + pom + + ../../domain/a/service-a-service + ../../domain/b/service-b-service + + + common-service + + + + + + org.springframework.cloud + spring-cloud-starter-netflix-hystrix + + + + + \ No newline at end of file diff --git a/cloud/common/common-web/pom.xml b/cloud/common/common-web/pom.xml new file mode 100644 index 0000000..f55f9dd --- /dev/null +++ b/cloud/common/common-web/pom.xml @@ -0,0 +1,63 @@ + + + + cloud-parent + com.ganlucode.demo + 0.0.1-SNAPSHOT + ../cloud-parent/pom.xml + + 4.0.0 + pom + + ../../domain/a/service-a-web + ../../domain/b/service-b-web + + + common-web + + + + + + org.springframework.cloud + spring-cloud-starter-sleuth + + + + org.springframework.cloud + spring-cloud-starter-zipkin + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.boot + spring-boot-starter-aop + + + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-client + + + + org.springframework.cloud + spring-cloud-starter-config + + + org.springframework.boot + spring-boot-configuration-processor + true + + + + + \ No newline at end of file diff --git a/cloud/domain/a/service-a-api/pom.xml b/cloud/domain/a/service-a-api/pom.xml new file mode 100644 index 0000000..b5706af --- /dev/null +++ b/cloud/domain/a/service-a-api/pom.xml @@ -0,0 +1,25 @@ + + + + common-api + com.ganlucode.demo + 0.0.1-SNAPSHOT + ../../../common/common-api/pom.xml + + 4.0.0 + + service-a-api + + + + + service-a-pojo + com.ganlucode.demo + ${project.version} + + + + + \ No newline at end of file diff --git a/cloud/domain/a/service-a-dao/pom.xml b/cloud/domain/a/service-a-dao/pom.xml new file mode 100644 index 0000000..12bcd49 --- /dev/null +++ b/cloud/domain/a/service-a-dao/pom.xml @@ -0,0 +1,25 @@ + + + + common-dao + com.ganlucode.demo + 0.0.1-SNAPSHOT + ../../../common/common-dao/pom.xml + + 4.0.0 + + service-a-dao + + + + + service-a-pojo + com.ganlucode.demo + ${project.version} + + + + + \ No newline at end of file diff --git a/cloud/domain/a/service-a-pojo/pom.xml b/cloud/domain/a/service-a-pojo/pom.xml new file mode 100644 index 0000000..f207744 --- /dev/null +++ b/cloud/domain/a/service-a-pojo/pom.xml @@ -0,0 +1,16 @@ + + + + common-pojo + com.ganlucode.demo + 0.0.1-SNAPSHOT + ../../../common/common-pojo/pom.xml + + 4.0.0 + + service-a-pojo + + + \ No newline at end of file diff --git a/cloud/domain/a/service-a-service/pom.xml b/cloud/domain/a/service-a-service/pom.xml new file mode 100644 index 0000000..1707f6d --- /dev/null +++ b/cloud/domain/a/service-a-service/pom.xml @@ -0,0 +1,29 @@ + + + + common-service + com.ganlucode.demo + 0.0.1-SNAPSHOT + ../../../common/common-service/pom.xml + + 4.0.0 + + service-a-service + + + + + service-a-dao + com.ganlucode.demo + ${project.version} + + + service-a-api + com.ganlucode.demo + ${project.version} + + + + \ No newline at end of file diff --git a/cloud/domain/a/service-a-web/pom.xml b/cloud/domain/a/service-a-web/pom.xml new file mode 100644 index 0000000..c151e38 --- /dev/null +++ b/cloud/domain/a/service-a-web/pom.xml @@ -0,0 +1,26 @@ + + + + common-web + com.ganlucode.demo + 0.0.1-SNAPSHOT + ../../../common/common-web/pom.xml + + 4.0.0 + + service-a-web + + + + + + + + + + + + + \ No newline at end of file diff --git a/cloud/domain/a/service-a-web/src/main/java/com/ganlucode/springcloud/ServiceAApplication.java b/cloud/domain/a/service-a-web/src/main/java/com/ganlucode/springcloud/ServiceAApplication.java new file mode 100644 index 0000000..e9e7b81 --- /dev/null +++ b/cloud/domain/a/service-a-web/src/main/java/com/ganlucode/springcloud/ServiceAApplication.java @@ -0,0 +1,29 @@ +package com.ganlucode.springcloud; + +import org.springframework.boot.WebApplicationType; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +import org.springframework.context.annotation.Bean; +import org.springframework.web.client.RestTemplate; + +/** + * @author GanLu + * @date 2020-08-11 19:42 + */ +@SpringBootApplication +@EnableDiscoveryClient +public class ServiceAApplication { + + @Bean + public RestTemplate register() { + return new RestTemplate(); + } + + public static void main(String[] args) { + new SpringApplicationBuilder(ServiceAApplication.class) + .web(WebApplicationType.SERVLET) + .run(args); + } + +} diff --git a/cloud/domain/a/service-a-web/src/main/java/com/ganlucode/springcloud/controller/EurekaController.java b/cloud/domain/a/service-a-web/src/main/java/com/ganlucode/springcloud/controller/EurekaController.java new file mode 100644 index 0000000..38dc9ea --- /dev/null +++ b/cloud/domain/a/service-a-web/src/main/java/com/ganlucode/springcloud/controller/EurekaController.java @@ -0,0 +1,79 @@ +package com.ganlucode.springcloud.controller; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.cloud.client.ServiceInstance; +import org.springframework.cloud.client.discovery.DiscoveryClient; +import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; +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; + +import java.util.List; + +/** + * @author GanLu + * @date 2020-08-11 19:46 + */ +@Slf4j +@RestController +@RequestMapping("/eureka") +public class EurekaController { + + @Autowired + private LoadBalancerClient client; + + @Autowired + private RestTemplate restTemplate; + + @Autowired + private DiscoveryClient discoveryClient; + + @Value("${spring.application.name}") + private String applicationName; + + public static final String SERVICE_NAME_B = "SERVICE-B"; + + @GetMapping("/sayHi") + public String sayHi() { + + + return "Hello, this is "+applicationName; + } + + @GetMapping("/b/hello") + public String hello() { + ServiceInstance instance = client.choose(SERVICE_NAME_B); + + if (instance == null) { + return "No available instances"; + } + + String target = String.format("http://%s:%s/eureka/sayHi", + instance.getHost(), + instance.getPort()); + log.info("url is {}", target); + + return restTemplate.getForObject(target, String.class); + } + + @GetMapping("/b/hello2") + public String hello2() { + List serviceInstanceList = discoveryClient.getInstances(SERVICE_NAME_B); + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < serviceInstanceList.size(); i++) { + ServiceInstance serviceInstance = serviceInstanceList.get(i); + String target = String.format("http://%s:%s/eureka/sayHi", + serviceInstance.getHost(), + serviceInstance.getPort()); + log.info("url is {}", target); + sb.append(restTemplate.getForObject(target, String.class)); + sb.append("\n"); + } + return sb.toString(); + } + + +} diff --git a/cloud/domain/a/service-a-web/src/main/resources/application.yml b/cloud/domain/a/service-a-web/src/main/resources/application.yml new file mode 100644 index 0000000..f333ceb --- /dev/null +++ b/cloud/domain/a/service-a-web/src/main/resources/application.yml @@ -0,0 +1,23 @@ +############################################################ +# +# Eureka注册中心 +# +############################################################ +eureka: + client: + serviceUrl: + defaultZone: http://localhost:20000/eureka/ + +spring: + application: + name: service-a + +server: + port: 20011 + + + + + + + diff --git a/cloud/domain/b/service-b-api/pom.xml b/cloud/domain/b/service-b-api/pom.xml new file mode 100644 index 0000000..2763ded --- /dev/null +++ b/cloud/domain/b/service-b-api/pom.xml @@ -0,0 +1,25 @@ + + + + common-api + com.ganlucode.demo + 0.0.1-SNAPSHOT + ../../../common/common-api/pom.xml + + 4.0.0 + + service-b-api + + + + + service-b-pojo + com.ganlucode.demo + ${project.version} + + + + + \ No newline at end of file diff --git a/cloud/domain/b/service-b-dao/pom.xml b/cloud/domain/b/service-b-dao/pom.xml new file mode 100644 index 0000000..ee4cc0a --- /dev/null +++ b/cloud/domain/b/service-b-dao/pom.xml @@ -0,0 +1,16 @@ + + + + common-dao + com.ganlucode.demo + 0.0.1-SNAPSHOT + ../../../common/common-dao/pom.xml + + 4.0.0 + + service-b-dao + + + \ No newline at end of file diff --git a/cloud/domain/b/service-b-pojo/pom.xml b/cloud/domain/b/service-b-pojo/pom.xml new file mode 100644 index 0000000..7a1dfbc --- /dev/null +++ b/cloud/domain/b/service-b-pojo/pom.xml @@ -0,0 +1,16 @@ + + + + common-pojo + com.ganlucode.demo + 0.0.1-SNAPSHOT + ../../../common/common-pojo/pom.xml + + 4.0.0 + + service-b-pojo + + + \ No newline at end of file diff --git a/cloud/domain/b/service-b-service/pom.xml b/cloud/domain/b/service-b-service/pom.xml new file mode 100644 index 0000000..3f88655 --- /dev/null +++ b/cloud/domain/b/service-b-service/pom.xml @@ -0,0 +1,30 @@ + + + + common-service + com.ganlucode.demo + 0.0.1-SNAPSHOT + ../../../common/common-service/pom.xml + + 4.0.0 + + service-b-service + + + + + service-b-dao + com.ganlucode.demo + ${project.version} + + + service-b-api + com.ganlucode.demo + ${project.version} + + + + + \ No newline at end of file diff --git a/cloud/domain/b/service-b-web/pom.xml b/cloud/domain/b/service-b-web/pom.xml new file mode 100644 index 0000000..80e7b7c --- /dev/null +++ b/cloud/domain/b/service-b-web/pom.xml @@ -0,0 +1,25 @@ + + + + common-web + com.ganlucode.demo + 0.0.1-SNAPSHOT + ../../../common/common-web/pom.xml + + 4.0.0 + + service-b-web + + + + + + + + + + + + \ No newline at end of file diff --git a/cloud/domain/b/service-b-web/src/main/java/com/ganlucode/springcloud/ServiceBApplication.java b/cloud/domain/b/service-b-web/src/main/java/com/ganlucode/springcloud/ServiceBApplication.java new file mode 100644 index 0000000..b4bb3b2 --- /dev/null +++ b/cloud/domain/b/service-b-web/src/main/java/com/ganlucode/springcloud/ServiceBApplication.java @@ -0,0 +1,29 @@ +package com.ganlucode.springcloud; + +import org.springframework.boot.WebApplicationType; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +import org.springframework.context.annotation.Bean; +import org.springframework.web.client.RestTemplate; + +/** + * @author GanLu + * @date 2020-08-11 19:42 + */ +@SpringBootApplication +@EnableDiscoveryClient +public class ServiceBApplication { + + @Bean + public RestTemplate register() { + return new RestTemplate(); + } + + public static void main(String[] args) { + new SpringApplicationBuilder(ServiceBApplication.class) + .web(WebApplicationType.SERVLET) + .run(args); + } + +} diff --git a/cloud/domain/b/service-b-web/src/main/java/com/ganlucode/springcloud/controller/EurekaController.java b/cloud/domain/b/service-b-web/src/main/java/com/ganlucode/springcloud/controller/EurekaController.java new file mode 100644 index 0000000..78be479 --- /dev/null +++ b/cloud/domain/b/service-b-web/src/main/java/com/ganlucode/springcloud/controller/EurekaController.java @@ -0,0 +1,79 @@ +package com.ganlucode.springcloud.controller; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.cloud.client.ServiceInstance; +import org.springframework.cloud.client.discovery.DiscoveryClient; +import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; +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; + +import java.util.List; + +/** + * @author GanLu + * @date 2020-08-11 19:46 + */ +@Slf4j +@RestController +@RequestMapping("/eureka") +public class EurekaController { + + @Autowired + private LoadBalancerClient client; + + @Autowired + private RestTemplate restTemplate; + + @Autowired + private DiscoveryClient discoveryClient; + + @Value("${spring.application.name}") + private String applicationName; + + public static final String SERVICE_NAME_A = "SERVICE-A"; + + @GetMapping("/sayHi") + public String sayHi() { + + + return "Hello, this is "+applicationName; + } + + @GetMapping("/a/hello") + public String hello() { + ServiceInstance instance = client.choose(SERVICE_NAME_A); + + if (instance == null) { + return "No available instances"; + } + + String target = String.format("http://%s:%s/eureka/sayHi", + instance.getHost(), + instance.getPort()); + log.info("url is {}", target); + + return restTemplate.getForObject(target, String.class); + } + + @GetMapping("/a/hello2") + public String hello2() { + List serviceInstanceList = discoveryClient.getInstances(SERVICE_NAME_A); + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < serviceInstanceList.size(); i++) { + ServiceInstance serviceInstance = serviceInstanceList.get(i); + String target = String.format("http://%s:%s/eureka/sayHi", + serviceInstance.getHost(), + serviceInstance.getPort()); + log.info("url is {}", target); + sb.append(restTemplate.getForObject(target, String.class)); + sb.append("\n"); + } + return sb.toString(); + } + + +} diff --git a/cloud/domain/b/service-b-web/src/main/resources/application.yml b/cloud/domain/b/service-b-web/src/main/resources/application.yml new file mode 100644 index 0000000..23954e6 --- /dev/null +++ b/cloud/domain/b/service-b-web/src/main/resources/application.yml @@ -0,0 +1,23 @@ +############################################################ +# +# Eureka注册中心 +# +############################################################ +eureka: + client: + serviceUrl: + defaultZone: http://localhost:20000/eureka/ + +spring: + application: + name: service-b + +server: + port: 20012 + + + + + + + diff --git a/cloud/platform/eureka-server/pom.xml b/cloud/platform/eureka-server/pom.xml new file mode 100644 index 0000000..a84de42 --- /dev/null +++ b/cloud/platform/eureka-server/pom.xml @@ -0,0 +1,22 @@ + + + + cloud-parent + com.ganlucode.demo + 0.0.1-SNAPSHOT + ../../common/cloud-parent/pom.xml + + 4.0.0 + + eureka-server + + + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-server + + + + \ No newline at end of file diff --git a/cloud/platform/eureka-server/src/main/java/com/ganlucode/springcloud/EurekaServerApplication.java b/cloud/platform/eureka-server/src/main/java/com/ganlucode/springcloud/EurekaServerApplication.java new file mode 100644 index 0000000..0dd8199 --- /dev/null +++ b/cloud/platform/eureka-server/src/main/java/com/ganlucode/springcloud/EurekaServerApplication.java @@ -0,0 +1,23 @@ +package com.ganlucode.springcloud; + +import org.springframework.boot.WebApplicationType; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; +import org.springframework.context.annotation.Bean; +import org.springframework.web.client.RestTemplate; + +/** + * @author GanLu + * @date 2020-04-07 23:44 + */ +@SpringBootApplication +@EnableEurekaServer +public class EurekaServerApplication { + + public static void main(String[] args) { + new SpringApplicationBuilder(EurekaServerApplication.class) + .web(WebApplicationType.SERVLET) + .run(args); + } +} diff --git a/cloud/platform/eureka-server/src/main/resources/application.yml b/cloud/platform/eureka-server/src/main/resources/application.yml new file mode 100644 index 0000000..70e33b2 --- /dev/null +++ b/cloud/platform/eureka-server/src/main/resources/application.yml @@ -0,0 +1,13 @@ +spring: + application: + name: eureka-server + +server: + port: 20000 + +eureka: + instance: + hostname: localhost + client: + register-with-eureka: false + fetch-registry: false \ No newline at end of file diff --git a/pom.xml b/pom.xml index 455052f..1aa4460 100644 --- a/pom.xml +++ b/pom.xml @@ -9,7 +9,8 @@ pom 0.0.1-SNAPSHOT - cloud/spring-cloud-demo + cloud/platform/eureka-server + cloud/common/cloud-parent org.springframework.boot -- Gitee From 9df3e875f9de569405e91e2dc5d8e794288c894f Mon Sep 17 00:00:00 2001 From: ganluCode Date: Tue, 11 Aug 2020 20:27:38 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E5=88=9B=E5=BB=BAEureka,demo=20=E7=94=A8Re?= =?UTF-8?q?stTemplate=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/ganlucode/springcloud/controller/EurekaController.java | 1 - 1 file changed, 1 deletion(-) diff --git a/cloud/domain/b/service-b-web/src/main/java/com/ganlucode/springcloud/controller/EurekaController.java b/cloud/domain/b/service-b-web/src/main/java/com/ganlucode/springcloud/controller/EurekaController.java index 78be479..a361036 100644 --- a/cloud/domain/b/service-b-web/src/main/java/com/ganlucode/springcloud/controller/EurekaController.java +++ b/cloud/domain/b/service-b-web/src/main/java/com/ganlucode/springcloud/controller/EurekaController.java @@ -39,7 +39,6 @@ public class EurekaController { @GetMapping("/sayHi") public String sayHi() { - return "Hello, this is "+applicationName; } -- Gitee