目录
1.3.配置客户端,我们还是用tensquare_base为例
1.集中配置组件 SpringCloudConfig
1.1.SpringCloudConfig 简介
在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需
要分布式配置中心组件。在
Spring Cloud
中,有分布式配置中心组件
spring cloud config
,它
支持配置服务放在配置服务的内存中(即本地),也支持放在远程
Git
仓库中。在
spring cloud
config
组件中,分两个角色,一是
config server
,二是
config client
。
Config Server
是一个可横向扩展、集中式的配置服务器,它用于集中管理应用程序各个环境
下的配置,默认使用
Git
存储配置文件内容,也可以使用
SVN
存储,或者是本地文件存储。
Config Client
是
Config Server
的客户端,用于操作存储在
Config Server
中的配置内容。
微服务在启动时会请求
Config Server
获取配置文件的内容,请求到后再启动容器。
详细内容看在线文档:
https://springcloud.cc/spring-cloud-config.html
1.2.配置服务端
1.2.1. 将配置文件提交到码云
使用
GitHub
时,国内的用户经常遇到的问题是访问速度太慢,有时候还会出现无法连接的情况。如果我们希望体验 Git
飞一般的速度,可以使用国内的
Git
托管服务——码云 (gitee.com
)。
和
GitHub
相比,码云也提供免费的
Git
仓库。此外,还集成了代码质量检测、项目演示等 功能。对于团队协作开发,码云还提供了项目管理、代码托管、文档管理的服务。
(
1
)浏览器打开
gitee.com
,注册用户 ,注册后登陆码云管理控制台。
(
2
)创建项目
tensquare-config (
点击右上角的加号 ,下拉菜单选择创建项目
)
(
3
)
上传配置文件,将
tensquare_base
工程的
application.yml
改名为
base-test.yml
后上传
文件命名规则:
{application}-{profile}.yml
或
{application}-{profile}.properties application 为应用名称
profile
指的开发环境(用于区分开发环境,测试环境、 生产环境等)
(
4
)复制
git
地址
,
备用
1.2.2. 配置中心微服务
(
1
)创建工程模块 配置中心微服务
tensquare_config ,pom.xml
引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
(2)
创建启动类
ConfigServerApplication
package com.tensquare.config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
/**
* 配置中心微服务
*/
@SpringBootApplication
@EnableConfigServer
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class,args);
}
}
(3)编写配置文件 application.yml
server:
port: 12000
spring:
cloud:
config:
server:
git:
uri: https://gitee.com/xxxxxx此处用你复制的地址
(4)启动项目,浏览器测试:http://localhost:12000/base-
test
.yml 可以看到配置内容
1.3.配置客户端,我们还是用tensquare_base为例
(
1
)在
tensquare_base
工程添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
(2)添加 bootstrap.yml ,删除 application.yml
spring:
cloud:
config:
uri: http://127.0.0.1:12000 # 配置中心服务端地址
name: base # 配置文件前面部分
profile: test # 配置文件后面部分
label: master # 从仓库哪个分支获取
(3)测试: 启动工程 三个微服务tensquare_eureka,tensquare_config,tensquare_base,看tensquare_base是否可以正常运行。
http://localhost:9001/label
2.SpringCloudBus
2.1.SpringCloudBus 简介
如果我们更新码云中的配置文件,那客户端工程是否可以及时接受新的配置信息 呢?我们现在来做有一个测试,修改一下码云中的配置文件中 mysql
的端口 ,然后测试 http://localhost:9001/label 数据依然可以查询出来,证明修改服务器中的配置并没有更新立刻到工程,只有重新启动程序才会读取配置。 那我们如果想在不重启微服务的情况下更新 配置如何来实现呢?
我们使用
SpringCloudBus
来实现配置的自动更新。
2.2配置服务端
(1)
修改
tensquare_config
工程的
pom.xml
,引用依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-bus</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>
(2)
修改
application.yml
,添加配置
rabbitmq:
host: 192.168.141.141
management: #暴露触发消息总线的地址
endpoints:
web:
exposure:
include: bus-refresh
2.3 配置客户端
我们还是以基础模块为例,加入消息总线。
(
1
)修改
tensquare_base
工程 ,引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-bus</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
(
2
)在码云的配置文件中配置
rabbitMQ 的地址,
增加自定义配置sms,整体配置文件如下。
server:
port: 9001
spring:
application:
name: tensquare-base #基础微服务
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.141.141:3306/tensquare_base?characterEncoding=utf-8&useSSL=false
username: root
password: 123456
jpa:
database: mysql
show-sql: true
generate-ddl: true
rabbitmq:
host: 192.168.141.141
eureka:
client:
register-with-eureka: true # 是否把该服务注册到eureka服务
fetch-registry: true # 是否需要从eureka获取信息
service-url: # eureka注册地址
defaultZone: http://127.0.0.1:6868/eureka
instance: # 在多个相同微服务的时候,需要eureka自动给每个微服务分配ip地址
prefer-ip-address: true
sms:
ip: 127.0.0.1
(3)
在 tensquare_base中LabelController新增方法
@Value("${sms.ip}")
private String ip;
/**
* 读取自定义配置
*/
@RequestMapping(value = "/showIp",method = RequestMethod.GET)
public Result showIp(){
return new Result(true,StatusCode.OK,"查询成功",ip);
}
(4)启动 tensquare_eureka 、tensquare_config 和 tensquare_base 看是否正常运行
(5)
修改码云上的配置文件中的自定义配置
,IP
改为 bugger
。
(6
)
postman
测试
Url: http://127.0.0.1:12000/actuator/bus-refresh Method: post
(7
)再次观察输出的数据是否改变了
以下流程为:
改完(5)之后,先get请求
http://localhost:9001/label/showIp,获取到如下图
之后执行(6),post请求
http://127.0.0.1:12000/actuator/bus-refresh,请求成功后内容为空。
观察结果(7)如下图