diff --git a/labx-25/labx-25-sc-zookeeper-discovery-demo01-consumer/pom.xml b/labx-25/labx-25-sc-zookeeper-discovery-demo01-consumer/pom.xml new file mode 100644 index 00000000..a1854f3b --- /dev/null +++ b/labx-25/labx-25-sc-zookeeper-discovery-demo01-consumer/pom.xml @@ -0,0 +1,72 @@ + + + + labx-25 + cn.iocoder.springboot.labs + 1.0-SNAPSHOT + + 4.0.0 + + labx-25-sc-zookeeper-discovery-demo01-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-commons + + + + + org.springframework.cloud + spring-cloud-starter-netflix-ribbon + + + + + org.apache.curator + curator-x-discovery + + + org.springframework.cloud + spring-cloud-zookeeper-discovery + + + + diff --git a/labx-25/labx-25-sc-zookeeper-discovery-demo01-consumer/src/main/java/cn/iocoder/springcloud/labx25/zookeeperdemo/consumer/DemoConsumerApplication.java b/labx-25/labx-25-sc-zookeeper-discovery-demo01-consumer/src/main/java/cn/iocoder/springcloud/labx25/zookeeperdemo/consumer/DemoConsumerApplication.java new file mode 100644 index 00000000..e4969562 --- /dev/null +++ b/labx-25/labx-25-sc-zookeeper-discovery-demo01-consumer/src/main/java/cn/iocoder/springcloud/labx25/zookeeperdemo/consumer/DemoConsumerApplication.java @@ -0,0 +1,69 @@ +package cn.iocoder.springcloud.labx25.zookeeperdemo.consumer; + +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-25/labx-25-sc-zookeeper-discovery-demo01-consumer/src/main/resources/application.yaml b/labx-25/labx-25-sc-zookeeper-discovery-demo01-consumer/src/main/resources/application.yaml new file mode 100644 index 00000000..34c38ba5 --- /dev/null +++ b/labx-25/labx-25-sc-zookeeper-discovery-demo01-consumer/src/main/resources/application.yaml @@ -0,0 +1,12 @@ +spring: + application: + name: demo-consumer # Spring 应用名 + cloud: + zookeeper: + connect-string: 127.0.0.1:2181 + # Zookeeper 作为注册中心的配置项,对应 ZookeeperDiscoveryProperties 配置类 + discovery: + root: /services # Zookeeper 数据存储的根节点,默认为 /services + +server: + port: 28080 # 服务器端口。默认为 8080 diff --git a/labx-25/labx-25-sc-zookeeper-discovery-demo01-provider/pom.xml b/labx-25/labx-25-sc-zookeeper-discovery-demo01-provider/pom.xml new file mode 100644 index 00000000..fdffce80 --- /dev/null +++ b/labx-25/labx-25-sc-zookeeper-discovery-demo01-provider/pom.xml @@ -0,0 +1,66 @@ + + + + labx-25 + cn.iocoder.springboot.labs + 1.0-SNAPSHOT + + 4.0.0 + + labx-25-sc-zookeeper-discovery-demo01-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-commons + + + + + org.apache.curator + curator-x-discovery + + + org.springframework.cloud + spring-cloud-zookeeper-discovery + + + + diff --git a/labx-25/labx-25-sc-zookeeper-discovery-demo01-provider/src/main/java/cn/iocoder/springcloud/labx25/zookeeperdemo/provider/DemoProviderApplication.java b/labx-25/labx-25-sc-zookeeper-discovery-demo01-provider/src/main/java/cn/iocoder/springcloud/labx25/zookeeperdemo/provider/DemoProviderApplication.java new file mode 100644 index 00000000..361dd1a6 --- /dev/null +++ b/labx-25/labx-25-sc-zookeeper-discovery-demo01-provider/src/main/java/cn/iocoder/springcloud/labx25/zookeeperdemo/provider/DemoProviderApplication.java @@ -0,0 +1,27 @@ +package cn.iocoder.springcloud.labx25.zookeeperdemo.provider; + +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-25/labx-25-sc-zookeeper-discovery-demo01-provider/src/main/resources/application.yaml b/labx-25/labx-25-sc-zookeeper-discovery-demo01-provider/src/main/resources/application.yaml new file mode 100644 index 00000000..d90b3159 --- /dev/null +++ b/labx-25/labx-25-sc-zookeeper-discovery-demo01-provider/src/main/resources/application.yaml @@ -0,0 +1,12 @@ +spring: + application: + name: demo-provider # Spring 应用名 + cloud: + zookeeper: + connect-string: 127.0.0.1:2181 + # Zookeeper 作为注册中心的配置项,对应 ZookeeperDiscoveryProperties 配置类 + discovery: + root: /services # Zookeeper 数据存储的根节点,默认为 /services + +server: + port: 18080 # 服务器端口。默认为 8080 diff --git a/labx-25/pom.xml b/labx-25/pom.xml new file mode 100644 index 00000000..85ec7fbf --- /dev/null +++ b/labx-25/pom.xml @@ -0,0 +1,20 @@ + + + + labs-parent + cn.iocoder.springboot.labs + 1.0-SNAPSHOT + + 4.0.0 + + labx-25 + pom + + + labx-25-sc-zookeeper-discovery-demo01-provider + labx-25-sc-zookeeper-discovery-demo01-consumer + + + diff --git a/pom.xml b/pom.xml index eb08f406..85baff08 100644 --- a/pom.xml +++ b/pom.xml @@ -71,6 +71,7 @@ + @@ -98,7 +99,7 @@ - lab-61 + labx-25