Skip to content

Commit ebf6be8

Browse files
author
YunaiV
committed
增加 Spring Cloud Zookeeper 入门
1 parent f1cccfa commit ebf6be8

File tree

8 files changed

+280
-1
lines changed

8 files changed

+280
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>labx-25</artifactId>
7+
<groupId>cn.iocoder.springboot.labs</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>labx-25-sc-zookeeper-discovery-demo01-consumer</artifactId>
13+
14+
<properties>
15+
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
16+
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
17+
</properties>
18+
19+
<!--
20+
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
21+
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
22+
-->
23+
<dependencyManagement>
24+
<dependencies>
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-starter-parent</artifactId>
28+
<version>${spring.boot.version}</version>
29+
<type>pom</type>
30+
<scope>import</scope>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.cloud</groupId>
34+
<artifactId>spring-cloud-dependencies</artifactId>
35+
<version>${spring.cloud.version}</version>
36+
<type>pom</type>
37+
<scope>import</scope>
38+
</dependency>
39+
</dependencies>
40+
</dependencyManagement>
41+
42+
<dependencies>
43+
<!-- 引入 SpringMVC 相关依赖,并实现对其的自动配置 -->
44+
<dependency>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-starter-web</artifactId>
47+
</dependency>
48+
49+
<!-- 引入 Spring Cloud Common 通用依赖 -->
50+
<dependency>
51+
<groupId>org.springframework.cloud</groupId>
52+
<artifactId>spring-cloud-commons</artifactId>
53+
</dependency>
54+
55+
<!-- 引入 Spring Cloud Netflix Ribbon 相关依赖,使用 Ribbon 实现负载均衡,并实现对其的自动配置 -->
56+
<dependency>
57+
<groupId>org.springframework.cloud</groupId>
58+
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
59+
</dependency>
60+
61+
<!-- 引入 Spring Cloud Zookeeper Discovery 相关依赖,将 Zookeeper 作为注册中心,并实现对其的自动配置 -->
62+
<dependency>
63+
<groupId>org.apache.curator</groupId>
64+
<artifactId>curator-x-discovery</artifactId> <!-- Zookeeper 客户端 -->
65+
</dependency>
66+
<dependency>
67+
<groupId>org.springframework.cloud</groupId>
68+
<artifactId>spring-cloud-zookeeper-discovery</artifactId>
69+
</dependency>
70+
</dependencies>
71+
72+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package cn.iocoder.springcloud.labx25.zookeeperdemo.consumer;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.cloud.client.ServiceInstance;
7+
import org.springframework.cloud.client.discovery.DiscoveryClient;
8+
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
9+
import org.springframework.context.annotation.Bean;
10+
import org.springframework.context.annotation.Configuration;
11+
import org.springframework.web.bind.annotation.GetMapping;
12+
import org.springframework.web.bind.annotation.RestController;
13+
import org.springframework.web.client.RestTemplate;
14+
15+
import java.util.List;
16+
17+
@SpringBootApplication
18+
// @EnableDiscoveryClient
19+
public class DemoConsumerApplication {
20+
21+
public static void main(String[] args) {
22+
SpringApplication.run(DemoConsumerApplication.class, args);
23+
}
24+
25+
@Configuration
26+
public class RestTemplateConfiguration {
27+
28+
@Bean
29+
public RestTemplate restTemplate() {
30+
return new RestTemplate();
31+
}
32+
33+
}
34+
35+
@RestController
36+
static class TestController {
37+
38+
@Autowired
39+
private DiscoveryClient discoveryClient;
40+
@Autowired
41+
private RestTemplate restTemplate;
42+
@Autowired
43+
private LoadBalancerClient loadBalancerClient;
44+
45+
@GetMapping("/hello")
46+
public String hello(String name) {
47+
// 获得服务 `demo-provider` 的一个实例
48+
ServiceInstance instance;
49+
if (true) {
50+
// 获取服务 `demo-provider` 对应的实例列表
51+
List<ServiceInstance> instances = discoveryClient.getInstances("demo-provider");
52+
// 选择第一个
53+
instance = instances.size() > 0 ? instances.get(0) : null;
54+
} else {
55+
instance = loadBalancerClient.choose("demo-provider");
56+
}
57+
// 发起调用
58+
if (instance == null) {
59+
throw new IllegalStateException("获取不到实例");
60+
}
61+
String targetUrl = instance.getUri() + "/echo?name=" + name;
62+
String response = restTemplate.getForObject(targetUrl, String.class);
63+
// 返回结果
64+
return "consumer:" + response;
65+
}
66+
67+
}
68+
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
spring:
2+
application:
3+
name: demo-consumer # Spring 应用名
4+
cloud:
5+
zookeeper:
6+
connect-string: 127.0.0.1:2181
7+
# Zookeeper 作为注册中心的配置项,对应 ZookeeperDiscoveryProperties 配置类
8+
discovery:
9+
root: /services # Zookeeper 数据存储的根节点,默认为 /services
10+
11+
server:
12+
port: 28080 # 服务器端口。默认为 8080
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>labx-25</artifactId>
7+
<groupId>cn.iocoder.springboot.labs</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>labx-25-sc-zookeeper-discovery-demo01-provider</artifactId>
13+
14+
<properties>
15+
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
16+
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
17+
</properties>
18+
19+
<!--
20+
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
21+
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
22+
-->
23+
<dependencyManagement>
24+
<dependencies>
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-starter-parent</artifactId>
28+
<version>${spring.boot.version}</version>
29+
<type>pom</type>
30+
<scope>import</scope>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.cloud</groupId>
34+
<artifactId>spring-cloud-dependencies</artifactId>
35+
<version>${spring.cloud.version}</version>
36+
<type>pom</type>
37+
<scope>import</scope>
38+
</dependency>
39+
</dependencies>
40+
</dependencyManagement>
41+
42+
<dependencies>
43+
<!-- 引入 SpringMVC 相关依赖,并实现对其的自动配置 -->
44+
<dependency>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-starter-web</artifactId>
47+
</dependency>
48+
49+
<!-- 引入 Spring Cloud Common 通用依赖 -->
50+
<dependency>
51+
<groupId>org.springframework.cloud</groupId>
52+
<artifactId>spring-cloud-commons</artifactId>
53+
</dependency>
54+
55+
<!-- 引入 Spring Cloud Zookeeper Discovery 相关依赖,将 Zookeeper 作为注册中心,并实现对其的自动配置 -->
56+
<dependency>
57+
<groupId>org.apache.curator</groupId>
58+
<artifactId>curator-x-discovery</artifactId> <!-- Zookeeper 客户端 -->
59+
</dependency>
60+
<dependency>
61+
<groupId>org.springframework.cloud</groupId>
62+
<artifactId>spring-cloud-zookeeper-discovery</artifactId>
63+
</dependency>
64+
</dependencies>
65+
66+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package cn.iocoder.springcloud.labx25.zookeeperdemo.provider;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
@SpringBootApplication
10+
@EnableDiscoveryClient
11+
public class DemoProviderApplication {
12+
13+
public static void main(String[] args) {
14+
SpringApplication.run(DemoProviderApplication.class, args);
15+
}
16+
17+
@RestController
18+
static class TestController {
19+
20+
@GetMapping("/echo")
21+
public String echo(String name) {
22+
return "provider:" + name;
23+
}
24+
25+
}
26+
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
spring:
2+
application:
3+
name: demo-provider # Spring 应用名
4+
cloud:
5+
zookeeper:
6+
connect-string: 127.0.0.1:2181
7+
# Zookeeper 作为注册中心的配置项,对应 ZookeeperDiscoveryProperties 配置类
8+
discovery:
9+
root: /services # Zookeeper 数据存储的根节点,默认为 /services
10+
11+
server:
12+
port: 18080 # 服务器端口。默认为 8080

labx-25/pom.xml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>labs-parent</artifactId>
7+
<groupId>cn.iocoder.springboot.labs</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>labx-25</artifactId>
13+
<packaging>pom</packaging>
14+
15+
<modules>
16+
<module>labx-25-sc-zookeeper-discovery-demo01-provider</module>
17+
<module>labx-25-sc-zookeeper-discovery-demo01-consumer</module>
18+
</modules>
19+
20+
</project>

pom.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
<!-- <module>lab-58</module>-->
7272
<!-- <module>lab-59</module>-->
7373
<!-- <module>lab-60</module>-->
74+
<!-- <module>lab-61</module>-->
7475

7576
<!-- Spring Cloud 示例 -->
7677
<!-- <module>labx-01</module>-->
@@ -98,7 +99,7 @@
9899
<!-- <module>labx-23</module>-->
99100
<!-- <module>labx-24</module>-->
100101

101-
<module>lab-61</module>
102+
<module>labx-25</module>
102103
</modules>
103104

104105
</project>

0 commit comments

Comments
 (0)