Consul 概述
Consul 是一個(gè)可以提供服務(wù)發(fā)現(xiàn),健康檢查,多數(shù)據(jù)中心,key/Value 存儲(chǔ)的分布式服務(wù)框架,用于實(shí)現(xiàn)分布式系統(tǒng)的發(fā)現(xiàn)與配置。Cousul 使用 Go 語言實(shí)現(xiàn),因此天然具有可移植性,安裝包僅包含一個(gè)可執(zhí)行文件,直接啟動(dòng)即可運(yùn)行,方便部署
Consul 安裝與啟動(dòng)
以 windows 為例,在官網(wǎng)下載 Consul:https://www.consul.io/
下載之后解壓縮,進(jìn)入目錄運(yùn)行 consul.exe 即可:.\consul.exe agent -dev
Consul 啟動(dòng)完成后,在瀏覽器中訪問 http://ocalhost:8500/ 便可以看到 Consul 首頁
Consul 服務(wù)注冊(cè)與發(fā)現(xiàn)
創(chuàng)建 cousul-service 項(xiàng)目,引入依賴,其中 Spring Boot Actuator 是健康檢查需要依賴的包,本項(xiàng)目基于 SpringBoot 2.3.1,SpringCloud Hoxton.SR12
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<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>
</dependencies>
在 application.yml 配置文件中添加如下配置:
server:
port: 8080
spring:
application:
name: consul-service
cloud:
consul:
host: localhost
port: 8500
discovery:
instance-id: ${spring.application.name}:${server.port}
在啟動(dòng)類上添加注解 @EnableDiscoveryClient
@EnableDiscoveryClient
@SpringBootApplication
public class ConsulProducerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsulProducerApplication.class, args);
}
}
啟動(dòng)項(xiàng)目,查看 Consul Web 頁面,即可看到服務(wù)注冊(cè)成功
Consul 配置中心
參考上一節(jié)內(nèi)容創(chuàng)建 cousul-config 項(xiàng)目,引入依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
</dependencies>
在 bootstrap.yml 配置文件(注意必須使用 bootstrap)中添加如下配置:
server:
port: 8080
spring:
application:
name: consul-service
# profiles:
# active: dev # 指定環(huán)境,默認(rèn)加載 default 環(huán)境
cloud:
consul:
host: localhost
port: 8500
discovery:
instance-id: ${spring.application.name}:${server.port}
config:
enabled: true # false禁用Consul配置,默認(rèn)為true
format: yaml # 表示consul上面文件的格式,有四種:YAML、PROPERTIES、KEY-VALUE、FILES
prefix: config # 可以理解為配置文件所在的最外層目錄
default-context: consul-service # 設(shè)置應(yīng)用的文件夾名稱
data-key: consul-service-config # Consul的Key/Values中的Key,Value對(duì)應(yīng)整個(gè)配置文件
# 以上配置可以理解為:加載config/consul-service/文件夾下Key為consul-service-config的Value對(duì)應(yīng)的配置信息
# 配置環(huán)境分隔符,默認(rèn)值 "," 和 default-context 配置項(xiàng)搭配
# 例如應(yīng)用 consul-service 分別有環(huán)境 default、dev、test、prod
# 只需在 config 文件夾下創(chuàng)建 consul-service、consul-service-dev、consul-service-test、consul-service-prod 文件夾即可
# profile-separator: '-'
watch:
enabled: true # 是否開啟自動(dòng)刷新,默認(rèn)值true開啟
delay: 1000 # 刷新頻率,單位毫秒,默認(rèn)值1000
在啟動(dòng)類上添加注解 @EnableDiscoveryClient
@SpringBootApplication
@EnableDiscoveryClient
// 啟用配置屬性類,當(dāng)SpringBoot程序啟動(dòng)時(shí)會(huì)立即加載@EnableConfigurationProperties注解中指定的類對(duì)象
@EnableConfigurationProperties({MySqlComplexConfig.class})
public class ConsulConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConsulConfigApplication.class, args);
}
}
定義 MysqlConfig 配置類
@Component
@ConfigurationProperties(prefix = "mysql")
public class MysqlConfig {
private String host;
private String username;
private String password;
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
開發(fā) ConfigController
@RefreshScope // 用于重新刷新作用域?qū)崿F(xiàn)屬性值自動(dòng)刷新
@RestController
public class ConfigController {
@Autowired
private MysqlConfig mysqlConfig;
@GetMapping("getConfig")
public Map<String, String> getMysqlConfig() {
HashMap<String, String> map = new HashMap<>();
map.put("host", mysqlConfig.getHost());
map.put("username", mysqlConfig.getUsername());
map.put("password", mysqlConfig.getPassword());
return map;
}
}
在 Consul 管理界面添加配置信息,點(diǎn)擊左側(cè)菜單的 Key/Value,按照 bootstrap.yml 中的配置創(chuàng)建 config/consul-service 目錄,在 consul-service 目錄下創(chuàng)建 key:consul-service-config,在 value 添加配置信息
文章來源:http://www.zghlxwxcb.cn/news/detail-657416.html
請(qǐng)求 http://localhost:8080/getConfig,可以看到服務(wù)會(huì)從 Consul 中獲取配置,并返回文章來源地址http://www.zghlxwxcb.cn/news/detail-657416.html
到了這里,關(guān)于注冊(cè)中心/配置管理 —— SpringCloud Consul的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!