docs/Spring全家桶/SpringCloud/SpringCloudConsul.md
Spring Cloud Consul Ϊ SpringBoot Ӧṩ Consul֧֣ConsulȿΪעʹãҲΪʹãĽ÷ϸܡ
ConsulHashiCorp˾ƳĿԴṩϵͳеķġߵȹܡЩеÿһԸҪʹãҲһʹԹȫλķ֮Consulṩһķ
Spring Cloud Consul ԣ
ǴӹConsulַhttps://www.consul.io/downloads.html
ɺֻһexeļ˫У
Բ鿴汾ţ
consul --version
鿴汾Ϣ£
Consul v1.6.1 Protocol 2 spoken by default, understands 2 to 3 (agent will automatically use protocol >2 when speaking to compatible agents)
ʹÿģʽ
consul agent -dev
ͨµַԷConsulҳhttp://localhost:8500
ͨuser-serviceribbon-serviceʾ·ע뷢ֵĹܣҪǽӦԭEurekaעָ֧ΪConsulע֧֡
consul-user-serviceģconsul-ribbon-serviceģ飻
ԭEurekaעֵᷢΪConsulģSpringBoot Actuator
<dependency> <groupId>org.springframework.cloud</groupId> spring-cloud-starter-consul-discovery </dependency> <dependency> <groupId>org.springframework.boot</groupId> spring-boot-starter-actuator </dependency>ļapplication.ymlEurekaעᷢøΪConsulģ
server: port: 8206 spring: application: name: consul-user-service cloud: consul: #Consulעᷢ host: localhost port: 8500 discovery: service-name: ${spring.application.name}
consul-user-serviceһconsul-ribbon-serviceConsulҳϿԿϢ
consul-user-serviceconsul-ribbon-serviceĬϻȥĽӿڣǵconsul-ribbon-serviceĽӿʾ¸ؾܡ
εýӿڣhttp://localhost:8308/user/1 Էconsul-user-serviceĿ̨ӡϢ
2019-10-20 10:39:32.580 INFO 12428 --- [io-8206-exec-10] c.macro.cloud.controller.UserController : idȡûϢûΪmacro
ͨconsul-config-clientģ飬ConsulϢʾùĹܡ
pom.xml
<dependency> <groupId>org.springframework.cloud</groupId> spring-cloud-starter-consul-config </dependency> <dependency> <groupId>org.springframework.cloud</groupId> spring-cloud-starter-consul-discovery </dependency>ļapplication.ymlõdevã
spring: profiles: active: dev
ļbootstrap.ymlҪǶConsulùܽã
server: port: 9101 spring: application: name: consul-config-client cloud: consul: host: localhost port: 8500 discovery: serviceName: consul-config-client config: enabled: true #ǷĹ format: yaml #ֵĸʽ prefix: config #Ŀ¼ profile-separator: ':' #õķָ data-key: data #key֣ConsulK/V洢ô洢ڶӦKV
ConfigClientControllerConsulлȡϢ
/**
Created by macro on 2019/9/11. */ @RestController @RefreshScope public class ConfigClientController {
@Value("${config.info}") private String configInfo;
@GetMapping("/configInfo") public String getConfigInfo() { return configInfo; } }
consulô洢keyΪ:
config/consul-config-client:dev/data
consulô洢valueΪ
config: info: "config info for dev"
洢Ϣͼ£
consul-config-clientýӿڲ鿴Ϣhttp://localhost:9101/configInfo
config info for dev
ֻҪConsulеϢٴεò鿴õĽӿڣͻᷢѾˢ¡ʹSpring Cloud ConfigʱҪýӿڣͨSpring Cloud BusˢáConsulʹԴControl Bus ʵһ¼ݻƣӶʵ˶̬ˢ¹ܡ
springcloud-learning
consul-config-client -- ʾconsulΪĵconsulͻ
consul-user-service -- עᵽconsulṩUserCRUDӿڵķ
consul-service -- עᵽconsulribbonòԷ
https://github.com/macrozheng/springcloud-learning
https://lijunyi.xyz/docs/SpringCloud/SpringCloud.html#_2-2-x-%E5%88%86%E6%94%AF https://mp.weixin.qq.com/s/2jeovmj77O9Ux96v3A0NtA https://juejin.cn/post/6931922457741770760 https://github.com/D2C-Cai/herring http://c.biancheng.net/springcloud https://github.com/macrozheng/springcloud-learning