已有框架的缺点:目前每个微服务配置参数都放在每个项目的application.yml或者application.properties,如果要切换环境要设置多个Profile 比如eureka server 集群模拟,启动两个eureka sever要指定两个—spring.profiles.active=?
Config:在统一的地方动态配置环境参数(热更新)
架构图如下:
tcloud-commons-configrepo
添加如下将
子文件夹命名规则是:service-微服务名 开发环境配置文件命名规则是:微服务名-dev.yml 注意:不要有任何中文注释! tcloud-user-provider-dev.yml内容是: server: port: 8000 spring: datasource: url: jdbc:mysql://10.122.7.114:3306/svw_account?useUnicode=true username: root password: root driverClassName: com.mysql.jdbc.Driver max-active: 20 max-idle: 8 min-idle: 8 initial-size: 10
mybatis: configuration: mapUnderscoreToCamelCase:true
eureka: client: serviceUrl: defaultZone: http://localhost:8100/eureka/ instance: prefer-ip-address:true |
文件application-dev.yml内容是: profile: dev-1.0 |
文件application.yml内容是: profile: default-1.0 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0modelVersion>
<groupId>com.svw.tbox.tcloud.commonsgroupId> <artifactId>tcloud-commons-configserverartifactId> <version>0.0.1-SNAPSHOTversion> <name>tcloud-commons-configservername> <url>http://maven.apache.orgurl>
<parent> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-parentartifactId> <version>1.4.3.RELEASEversion> parent>
<properties> <project.build.sourceEncoding>UTF-8project.build.sourceEncoding> properties>
<dependencies> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-config-serverartifactId> dependency> dependencies>
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-dependenciesartifactId> <version>Camden.SR4version> <type>pomtype> <scope>importscope> dependency> dependencies> dependencyManagement> project> |
package com.svw.tbox.tcloud.commons.configserver;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication @EnableConfigServer publicclass ConfigServerApplication { publicstaticvoid main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } } |
server: port: 8300 spring: application: name: tcloud-commons-configserver cloud: config: server: git: clone-on-start:true #启动快速识别错误的配资源 uri: http://10.122.7.81/TBoxGroup/POC/tcloud-commons-configrepo.git #uri: 对应gitlab地址 search-paths: conf-{application} # 扫描tcloud-commons-configrepo/ username: huruifeng # Git仓库的账号 password: hurf10hurf10 ##快速定位问题 #logging: # level: # org.springframework.cloud: DEBUG # org.springframework.boot: DEBUG |
效果:
启动tcloud-commons-configserver
访问http://localhost:8300/application/dev如下:
访问http://localhost:8300/application/default如下:
访问http://localhost:8300/tcloud-user-provider/dev出现如下
======================================================
修改tcloud-user-provider 和tcloud-user-consumer
<dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-webartifactId> dependency> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-configartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-actuatorartifactId> dependency> |
spring: application: name: tcloud-user-provider # 注册应用名{application} cloud: bus: trace: enabled:true config: failFast:true profile: local # 对应config server所获取的配置文件中的{profile} label: develop discovery: enabled:true serviceId: tcloud-commons-config-server |
tcloud-user-provider内容如下:
@RestController publicclass UserController {
/** * 版本号 */ @Value("${profile}") private String profile;
/** * 数据库连接地址 */ @Value("${spring.Datasource.url}") private String url;
@GetMapping("/getValue") public String getConfigValue(){ return"版本号是:"+profile+" 数据库连接地址是:"+url; } …… |
注意:bootstrap.yml 是父目录文件优先(详细微服务中的bootstrap.yml参数不能覆盖config-repo中的bootstrap.yml参数,和application.*文件正好相反)
=》启动tcloud-commons-configserver
=》启动tcloud-base-eurekaserver
=》启动tcloud-gateway-zuulserver
=》启动tcloud-user-provider
=》访问http://localhost:8000/getValue
或者http://localhost:8200/tcloud-user-provider/getValue获得如下结果:
现在有个问题,每次更新tcloud-commons-configrepo/下的文件内容参数,对应的微服务又要重启获取变更后的内容
=》使用bus 设置自动更新(修改文件=>请求一个url=>自动更新到多有微服务)
架构:
将config server 改造成eureka server发现的服务
将config server 注册到对应的 zk 和 kafka上
<dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-config-serverartifactId> dependency> <dependency> dependency> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-bus-kafkaartifactId> dependency> |
## 注册到eureka eureka: client: serviceUrl: defaultZone: http://localhost:8100/eureka/ instance: prefer-ip-address:true #注册IP,而不是默认hostname |
spring: application: name: tcloud-commons-config-server cloud: stream: default-binder: kafka kafka: binder: zk-nodes: 10.122.7.120:2181 # kafka zk节点 brokers: 10.122.7.120:9092 # kafka server节点 #关闭健康检查需要用户名密码的访问的/bus/refresh(可以直接访问bus/refresh) management: security: enabled:false |
@SpringBootApplication @EnableConfigServer @EnableDiscoveryClient publicclass ConfigServerApplication { publicstaticvoid main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } } |
<dependency> <groupId>org.springframework.retrygroupId> <artifactId>spring-retryartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-aopartifactId> dependency> <dependency> dependency> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-eurekaartifactId> dependency> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-feignartifactId> dependency> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-configartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-actuatorartifactId> dependency> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-bus-kafkaartifactId> dependency> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-hystrixartifactId> dependency> |
spring: cloud: stream: default-binder: kafka kafka: binder: zk-nodes: 10.122.7.120:2181 brokers: 10.122.7.120:9092 |
@RefreshScope @RestController @Api( publicclass UserController { @Value("${profile}") private String profile; publicvoid setProfile(String profile) { this.profile = profile; } public String getProfile() { returnprofile; } @RequestMapping("/profile") public String profile() { returnthis.profile; } …… |
@RefreshScope @RestController publicclass TboxController { …… @Value("${profile}") private String profile; publicvoid setProfile(String profile) { this.profile = profile; } public String getProfile() { returnprofile; } @RequestMapping("/profile") public String profile() { returnthis.profile; } …… |
依次启动
Consumer 控制台出现
Mapped"{[/bus/refresh],methods=[POST]}" … 说明可以自动刷新
[root@T-kafka ~]# kafka-topics.sh --list --zookeeper localhost:2181 __consumer_offsets atopic springCloudBus test |
访问两个微服务客户端地址 |
修改 config server中的参数 : profile: dev-2.0
Post请求 http://localhost:8200/tcloud-commons-config-server/bus/refresh ,此处建议使用postman模拟post请求: |
再次访问: 这两个地址 |