SpringBoot + Spring Cloud Consul 服務註冊和發現
- 2020 年 7 月 23 日
- 筆記
- springboot, Spring Cloud, 【框架】Spring Cloud
什麼是Consul
Consul 是 HashiCorp 公司推出的開源工具,用於實現分散式系統的服務發現與配置。與其它分散式服務註冊與發現的方案,Consul 的方案更「一站式」,內置了服務註冊與發現框架、分布一致性協議實現、健康檢查、Key/Value 存儲、多數據中心方案,不再需要依賴其它工具(比如 ZooKeeper 等)。使用起來也較為簡單。Consul 使用 Go 語言編寫,因此具有天然可移植性(支援Linux、windows和Mac OS X);安裝包僅包含一個可執行文件,方便部署,與 Docker 等輕量級容器可無縫配合。
Consul安裝
官網(consul.io)最新版本1.8.0 提供了MacOS,Windows, Linux, 如果你不知道怎麼安裝,官方還提供了影片。 
我這裡使用docker安裝,安裝過程總結起來為三句話:
docker search consul
docker pull consul
docker run --name consul -d -p 8600:8500 consul
沒問題的話,本機訪問//localhost:8600則可以打開consul自帶的管理系統,默認情況下沒有服務註冊進來
準備工作
- 新建父工程, 主要約定SpringCloud, SpringBoot版本號,我使用的是Hoxton.SR1, SpringBoot2.2
- 新建3個子Module,這裡兩個service,一個consumer, 兩個service我用8001和8002埠來區分,主要是想在服務消費時反應客戶端負載均衡。
項目結構如下圖
父工程pom.xml
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
服務提供者
服務註冊中心有了,那麼我們來開發兩個服務提供者,這裡新建了兩個Module,埠8001和8002。兩個Module程式碼相同,主要為了演示負載使用。
新建Module,添加spring-cloud-starter-consul-disconvery依賴
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
添加配置(application.yml)
server:
port: 8001
spring:
application:
name: consul-student-service
cloud:
consul:
port: 8600
host: 127.0.0.1
discovery:
service-name: ${spring.application.name}
修改啟動類,添加服務
這裡我直接寫了一個測試介面放在啟動類里。這裡我只貼了埠8001的程式碼,8002程式碼結構相同,只是埠不同。
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class ConsulStudentService8001 {
public static void main(String[] args) {
SpringApplication.run(ConsulStudentService8001.class,args);
}
@GetMapping("/student/version")
public String version(){
return "8001,202007222300";
}
}
OK,到這一步,啟動兩個服務,不出異常的情況下,可在註冊中心查看當前的服務實例。


Consul消費者
服務註冊中心有了,服務提供者也有了,我們再來開發一個服務消費者。
新建Module,同樣添加spring-cloud-starter-consul-disconvery依賴
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
添加配置(application.yml)
server:
port: 8080
spring:
application:
name: consul-student-consumer
cloud:
consul:
host: 127.0.0.1
port: 8600
discovery:
service-name: ${spring.application.name}
# 不需要註冊到consul中
register: false
修改啟動類,調用服務
開發RestTemplate配置類,調用REST介面時使用。
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
修改啟動類
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class ConsulStudentConsumer {
public static void main(String[] args) {
SpringApplication.run(ConsulStudentConsumer.class,args);
}
@Autowired
RestTemplate restTemplate;
@GetMapping("/consul/student/version")
public String version(){
//這裡使用服務實例名調用REST介面
return restTemplate.getForObject("//consul-student-service/student/version",String.class);
}
}
OK, 這一步完成之後,可以啟動消費者介面,刷新幾次,從返回結果上能看出來是輪訓調用服務提供者介面實例。



