diff --git a/labx-22/labx-22-scn-eureka-demo01-provider/src/main/resources/application.yaml b/labx-22/labx-22-scn-eureka-demo01-provider/src/main/resources/application.yaml index 30dd465f..2bfb44e6 100644 --- a/labx-22/labx-22-scn-eureka-demo01-provider/src/main/resources/application.yaml +++ b/labx-22/labx-22-scn-eureka-demo01-provider/src/main/resources/application.yaml @@ -7,7 +7,7 @@ server: eureka: client: - register-with-eureka: true - fetch-registry: true + register-with-eureka: true # 注册到 Eureka-Server,默认为 true + fetch-registry: true # 从 Eureka-Server 获取注册表,默认为 true service-url: - defaultZone: http://127.0.0.1:8761/eureka/ + defaultZone: http://127.0.0.1:8761/eureka/ # Eureka-Server 地址 diff --git a/labx-22/labx-22-scn-eureka-demo02-consumer/pom.xml b/labx-22/labx-22-scn-eureka-demo02-consumer/pom.xml new file mode 100644 index 00000000..51fd2369 --- /dev/null +++ b/labx-22/labx-22-scn-eureka-demo02-consumer/pom.xml @@ -0,0 +1,56 @@ + + + + labx-22 + cn.iocoder.springboot.labs + 1.0-SNAPSHOT + + 4.0.0 + + labx-22-scn-eureka-demo02-consumer + + + 2.2.4.RELEASE + Hoxton.SR1 + + + + + + + org.springframework.boot + spring-boot-starter-parent + ${spring.boot.version} + pom + import + + + org.springframework.cloud + spring-cloud-dependencies + ${spring.cloud.version} + pom + import + + + + + + + + org.springframework.boot + spring-boot-starter-web + + + + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-client + + + + diff --git a/labx-22/labx-22-scn-eureka-demo02-consumer/src/main/java/cn/iocoder/springcloudalibaba/labx22/consumerdemo/DemoConsumerApplication.java b/labx-22/labx-22-scn-eureka-demo02-consumer/src/main/java/cn/iocoder/springcloudalibaba/labx22/consumerdemo/DemoConsumerApplication.java new file mode 100644 index 00000000..93f81a06 --- /dev/null +++ b/labx-22/labx-22-scn-eureka-demo02-consumer/src/main/java/cn/iocoder/springcloudalibaba/labx22/consumerdemo/DemoConsumerApplication.java @@ -0,0 +1,69 @@ +package cn.iocoder.springcloudalibaba.labx22.consumerdemo; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.ServiceInstance; +import org.springframework.cloud.client.discovery.DiscoveryClient; +import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.RestTemplate; + +import java.util.List; + +@SpringBootApplication +// @EnableDiscoveryClient +public class DemoConsumerApplication { + + public static void main(String[] args) { + SpringApplication.run(DemoConsumerApplication.class, args); + } + + @Configuration + public class RestTemplateConfiguration { + + @Bean + public RestTemplate restTemplate() { + return new RestTemplate(); + } + + } + + @RestController + static class TestController { + + @Autowired + private DiscoveryClient discoveryClient; + @Autowired + private RestTemplate restTemplate; + @Autowired + private LoadBalancerClient loadBalancerClient; + + @GetMapping("/hello") + public String hello(String name) { + // 获得服务 `demo-provider` 的一个实例 + ServiceInstance instance; + if (true) { + // 获取服务 `demo-provider` 对应的实例列表 + List instances = discoveryClient.getInstances("demo-provider"); + // 选择第一个 + instance = instances.size() > 0 ? instances.get(0) : null; + } else { + instance = loadBalancerClient.choose("demo-provider"); + } + // 发起调用 + if (instance == null) { + throw new IllegalStateException("获取不到实例"); + } + String targetUrl = instance.getUri() + "/echo?name=" + name; + String response = restTemplate.getForObject(targetUrl, String.class); + // 返回结果 + return "consumer:" + response; + } + + } + +} diff --git a/labx-22/labx-22-scn-eureka-demo02-consumer/src/main/resources/application.yaml b/labx-22/labx-22-scn-eureka-demo02-consumer/src/main/resources/application.yaml new file mode 100644 index 00000000..28732752 --- /dev/null +++ b/labx-22/labx-22-scn-eureka-demo02-consumer/src/main/resources/application.yaml @@ -0,0 +1,12 @@ +spring: + application: + name: demo-consumer # Spring 应用名 +server: + port: 28080 # 服务器端口。默认为 8080 + +eureka: + client: + register-with-eureka: true # 注册到 Eureka-Server,默认为 true + fetch-registry: true # 从 Eureka-Server 获取注册表,默认为 true + service-url: + defaultZone: http://eureka-node01:18761/eureka/, http://eureka-node02:28761/eureka/ diff --git a/labx-22/labx-22-scn-eureka-demo02-provider/pom.xml b/labx-22/labx-22-scn-eureka-demo02-provider/pom.xml new file mode 100644 index 00000000..689ba14f --- /dev/null +++ b/labx-22/labx-22-scn-eureka-demo02-provider/pom.xml @@ -0,0 +1,56 @@ + + + + labx-22 + cn.iocoder.springboot.labs + 1.0-SNAPSHOT + + 4.0.0 + + labx-22-scn-eureka-demo02-provider + + + 2.2.4.RELEASE + Hoxton.SR1 + + + + + + + org.springframework.boot + spring-boot-starter-parent + ${spring.boot.version} + pom + import + + + org.springframework.cloud + spring-cloud-dependencies + ${spring.cloud.version} + pom + import + + + + + + + + org.springframework.boot + spring-boot-starter-web + + + + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-client + + + + diff --git a/labx-22/labx-22-scn-eureka-demo02-provider/src/main/java/cn/iocoder/springcloudalibaba/labx22/providerdemo/DemoProviderApplication.java b/labx-22/labx-22-scn-eureka-demo02-provider/src/main/java/cn/iocoder/springcloudalibaba/labx22/providerdemo/DemoProviderApplication.java new file mode 100644 index 00000000..aed29686 --- /dev/null +++ b/labx-22/labx-22-scn-eureka-demo02-provider/src/main/java/cn/iocoder/springcloudalibaba/labx22/providerdemo/DemoProviderApplication.java @@ -0,0 +1,27 @@ +package cn.iocoder.springcloudalibaba.labx22.providerdemo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@SpringBootApplication +@EnableDiscoveryClient +public class DemoProviderApplication { + + public static void main(String[] args) { + SpringApplication.run(DemoProviderApplication.class, args); + } + + @RestController + static class TestController { + + @GetMapping("/echo") + public String echo(String name) { + return "provider:" + name; + } + + } + +} diff --git a/labx-22/labx-22-scn-eureka-demo02-provider/src/main/resources/application.yaml b/labx-22/labx-22-scn-eureka-demo02-provider/src/main/resources/application.yaml new file mode 100644 index 00000000..5e5592e0 --- /dev/null +++ b/labx-22/labx-22-scn-eureka-demo02-provider/src/main/resources/application.yaml @@ -0,0 +1,13 @@ +spring: + application: + name: demo-provider # Spring 应用名 + +server: + port: 18080 # 服务器端口。默认为 8080 + +eureka: + client: + register-with-eureka: true # 注册到 Eureka-Server,默认为 true + fetch-registry: true # 从 Eureka-Server 获取注册表,默认为 true + service-url: + defaultZone: http://eureka-node01:18761/eureka/, http://eureka-node02:28761/eureka/ diff --git a/labx-22/labx-22-scn-eureka-server-cluster/src/main/resources/application-node02.yaml b/labx-22/labx-22-scn-eureka-server-cluster/src/main/resources/application-node02.yaml index 8dfd4c2d..1c44ec48 100644 --- a/labx-22/labx-22-scn-eureka-server-cluster/src/main/resources/application-node02.yaml +++ b/labx-22/labx-22-scn-eureka-server-cluster/src/main/resources/application-node02.yaml @@ -12,4 +12,4 @@ eureka: register-with-eureka: true fetch-registry: true service-url: - defaultZone: http://eureka-node01:28761/eureka/ + defaultZone: http://eureka-node01:18761/eureka/ diff --git a/labx-22/pom.xml b/labx-22/pom.xml index 359488d8..9b45c9c2 100644 --- a/labx-22/pom.xml +++ b/labx-22/pom.xml @@ -15,7 +15,11 @@ labx-22-scn-eureka-server-standalone labx-22-scn-eureka-demo01-provider labx-22-scn-eureka-demo01-consumer + labx-22-scn-eureka-server-cluster + labx-22-scn-eureka-demo02-provider + labx-22-scn-eureka-demo02-consumer +