微信公众号:bugstack虫洞栈 沉淀、分享、成长,专一于原创专题案例,以最易学习编程的方式分享知识,让本身和他人都能有所收获。目前已完成的专题有;Netty4.x实战专题案例、用Java实现JVM、基于JavaAgent的全链路监控、手写RPC框架、架构设计专题案例[Ing]等。html
在微服务架构中,为了更方便的向微服务实例广播消息,咱们一般会构建一个消息中心,让全部的服务实例都链接上来,而该消息中心所发布的消息都会被微服务实例监听和消费,咱们把这种机制叫作消息总线(SpringCloud Bus)java
当咱们的微服务达到是几个到百个以上,在更新配置时,不太可能一个个刷新或者重启,这样既不能保证效率也容易致使遗漏形成事故。所以咱们须要SpringCloud Bus 提供总线服务,在咱们push代码到Git的时候,经过Webhooks(http://localhost:port/actuator/bus-refresh/)执行刷新,消息总线会通知各个实例更新配置,以达到自动更新全服务配置。git
itstack-demo-springcloud-07
├── itstack-demo-springcloud-config-client
│ └── src
│ └── main
│ ├── java
│ │ └── org.itstack.demo
│ │ ├── web
│ │ │ └── ConfigClientController.java
│ │ └── ConfigClientApplication.java
│ └── resources
│ ├── application.yml
│ └── bootstrap.yml
├── itstack-demo-springcloud-config-server
│ └── src
│ └── main
│ ├── java
│ │ └── org.itstack.demo
│ │ └── ConfigServerApplication.java
│ └── resources
│ └── application.yml
└── itstack-demo-springcloud-eureka-server
└── src
└── main
├── java
│ └── org.itstack.demo
│ └── EurekaServerApplication.java
└── resources
└── application.yml
复制代码
完整代码欢迎关注公众号:bugstack虫洞栈 回复“SpringCloud专题”进行下载github
web/ConfigClientController.java & 添加注解@RefreshScope自动刷新配置web
/** * 微信公众号:bugstack虫洞栈 | 沉淀、分享、成长,专一于原创专题案例 * 论坛:http://bugstack.cn * Create by 付政委 on @2019 */
@RestController
@RefreshScope
public class ConfigClientController {
@Value("${info.profile:error}")
private String profile;
@GetMapping("/config")
public Mono<String> config() {
return Mono.justOrEmpty(profile);
}
}
复制代码
ConfigClientApplication.java & 普通配置便可spring
/** * 微信公众号:bugstack虫洞栈 | 沉淀、分享、成长,专一于原创专题案例 * 论坛:http://bugstack.cn * Create by 付政委 on @2019 */
@SpringBootApplication
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}
复制代码
application.yml & 须要配置endpoints,这样才能够暴漏刷新服务编程
spring:
application:
name: itstack-demo-springcloud-config-client
cloud:
bus:
trace:
enabled: true
enabled: true
server:
port: 9001
# 若是不使用消息总线,则开启以下配置 /actuator/refresh 这个 Endpoint 暴露出来
#management:
# endpoints:
# web:
# exposure:
# include: refresh
复制代码
bootstrap.yml & 配置中心服务配置,http://localhost:7397 添加配置服务bootstrap
spring:
cloud:
config:
name: config-client # 对应 {application} 部分,例如;config-client-dev = 只取最后一个符号'-'以前的
profile: dev # 对应 {profile} 部分
label: master # 对应 {label} 部分,即 Git 的分支。若是配置中心使用的是本地存储,则该参数无用
discovery:
enabled: true # 开启 config 服务发现支持
service-id: itstack-demo-springcloud-config-server # 配置服务name
#配置文件会被转换成 Web,访问规则以下;
#/{application}/{profile}[/{label}]
#/{application}-{profile}.yml
#/{label}/{application}-{profile}.yml
#/{application}-{profile}.properties
#/{label}/{application}-{profile}.properties
eureka:
client:
service-url:
defaultZone: http://localhost:7397/eureka/
复制代码
ConfigServerApplication.java & 添加注解@EnableConfigServer设置成配置服务中心浏览器
/** * 微信公众号:bugstack虫洞栈 | 沉淀、分享、成长,专一于原创专题案例 * 论坛:http://bugstack.cn * Create by 付政委 on @2019 */
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
复制代码
application.yml & 配置信息,消息总线刷新微信
server:
port: 8080
spring:
application:
name: itstack-demo-springcloud-config-server
cloud:
config:
server:
git:
uri: https://github.com/fuzhengwei/itstack-demo-config # 换成本身的配置Git仓库的地址,若是没有能够新建工程地址,也能够克隆个人;https://github.com/fuzhengwei/itstack-demo-config
search-paths: config-repo # Git仓库地址下的底层配置文件名称,若是配置多个用逗号','分割。
# 若是配置中心须要访问权限,则开启配置
# spring.cloud.config.server.git.username:Github帐户
# spring.cloud.config.server.git.password:Github密码
eureka:
client:
service-url:
defaultZone: http://localhost:7397/eureka/
management:
endpoints:
web:
exposure:
include: bus-refresh
复制代码
EurekaServerApplication.java & 添加注解@EnableEurekaServer启动服务发现
/** * 微信公众号:bugstack虫洞栈 | 沉淀、分享、成长,专一于原创专题案例 * 论坛:http://bugstack.cn * Create by 付政委 on @2019 */
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run( EurekaServerApplication.class, args );
}
}
复制代码
application.yml & 配置信息
server:
port: 7397
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
spring:
application:
name: itstack-demo-springcloud-eureka-server
复制代码
准备好本身Github的配置仓库,也能够克隆个人Git;github.com/fuzhengwei/… {有一组配置配置文件}
配置Webhooks,在https://github.com/换你本身的fuzhengwei/换你本身的itstack-demo-netty/settings/hooks/new
分别启动服务
访问配置服务,端口7397;http://localhost:8080/config-client/dev
{
"name": "config-client",
"profiles": [
"dev"
],
"label": null,
"version": "ea0b1a1017595d542aa01b8b2bda68f9620dd81a",
"state": null,
"propertySources": [
{
"name": "https://github.com/fuzhengwei/itstack-demo-config/config-repo/config-client-dev.yml",
"source": {
"info.profile": "dev bus"
}
}
]
}
复制代码
info:
profile: dev bus
复制代码
访问使用配置的客户端
dev bus
复制代码
dev
复制代码