一、为什么要使用spring-cloud 配置中心(config)
- 在微服务的框架体系下,随着工程的扩大产生出越来越多的服务,每一个服务里面都可能需要维护大量配置文件
- 如何在生产环境每次改版都涉及修改配置文件这可能就是个灾难
- spring cloud配置中心(cofig-server)为我们集中统一管理配置文件
- 每当有配置文件更新时,子服务自动拉取最新的配置文件信息并实时更新到程序中
二、本项目使用的spring-cloud 版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
创建config 工程
- 在gihub 或者 gitee 上面创建一个分支,存放config 文件
- 创建spring-cloud-zookeeper-dev.yml 文件
hello: text: hello spring dev1
- 创建spring-cloud-zookeeper-uat.yml文件
hello: text: hello spring uat
- 将上面的文件存放到“spring-cloud-zookeeper”下
三、创建config-server 工程
- pom文件
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
- bootstrap.yml 文件
server:
port: 8888
spring:
application:
name: spring-cloud-config-server
cloud:
zookeeper:
connect-string: 192.168.199.214:2181
discovery:
enabled: true
config:
server:
git:
uri: you git project address #github仓库地址
username: you username # 用户名
password: you passwd # 密码
search-paths: spring-cloud-zookeeper #查询的目录
rabbitmq:
host: 192.168.199.214
port: 5672
username: guest
password: guest
#暴露/bus-refresh接口
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS
- 启动工程类
@EnableConfigServer
@SpringBootApplication
@EnableDiscoveryClient
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
四、Client 项目
- pom.xml文件
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</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-zookeeper-discovery</artifactId>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
- bootstrap.yml文件
server:
port: 8081
spring:
application:
name: spring-cloud-zookeeper-client
cloud:
zookeeper:
connect-string: 192.168.199.214:2181
config:
discovery:
enabled: true
service-id: spring-cloud-config-server
profile: dev
name: spring-cloud-zookeeper-client
bus:
trace:
enabled: true
rabbitmq:
host: 192.168.199.214
port: 5672
username: guest
password: guest
#暴露监控端点
management:
endpoints:
web:
exposure:
include: "*"
#feign客户端配置
feign:
hystrix:
#设置feign开启hystrix(服务保护)
enabled: true
#hystrix配置
hystrix:
command:
default:
execution:
isolation:
thread:
#feign整合hystrix 光设置Hystrix 超时没用的要配合ribbon超时
timeoutInMilliseconds: 3000
circuitBreaker:
#默认20 ,熔断的阈值,如何user服务报错满足3次,熔断器就会打开,就算order之后请求正确的数据也不行。
requestVolumeThreshold: 3
#默认5S , 等5S之后熔断器会处于半开状态,然后下一次请求的正确和错误讲决定熔断器是否真的关闭和是否继续打开
sleepWindowInMilliseconds: 8000
-
在controller 中注入 hello.text
@RestController @RefreshScope public class HelloController { @Value("${hello.text}") private String helloTest; @GetMapping("/hello") public String hello() { return helloTest; } }
-
启动工程类
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
@EnableFeignClients
public class ZookeeperClientApplication {
public static void main(String[] args) {
SpringApplication.run(ZookeeperClientApplication.class, args);
}
}
- 当修改config 项目的配置文件提交后,访问client 项目:curl -X POST http://localhost:8081/actuator/bus-refresh
- 再访问:curl -X GET http://localhost:8081/hello 就可以看到最新的配置了
Docker 安装rabbitMQ
- 脚本
docker pull rabbitmq:management
docker run -d --name rabbitmq -p 5671:5671 -p 5672:5672 -p 4369:4369 -p 25672:25672 -p 15671:15671 -p 15672:15672 rabbitmq:management
- 访问rabbitMQ web 管理界面
容器启动之后就可以访问web管理界面了,访问 http://宿主机IP:15672。
系统提供了默认账号。 用户名:guest 密码: guest
五、遇到的问题
- 配置文件更新不刷新
- 在client 项目下使用 /actuator/bus-refresh post 请求
- 配置文件问题
- bootstrap.yml 这里一定要记得配置
#暴露监控端点 management: endpoints: web: exposure: include: "*"
- pom.xml
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency>
- bootstrap.yml 这里一定要记得配置
六、参考文章
七、关于我
- 关注不迷路,点赞走一波~ 转载请标注~
- 公众号