本文共 8593 字,大约阅读时间需要 28 分钟。
在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在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获取配置文件的内容,请求到后再启动容器。使用Spring Cloud Config的架构:
创建一个model工程作为配置中心,即config-server
。
org.springframework.cloud spring-cloud-config-server
在启动类中添加@EnableConfigServer
注解开启配置服务器的功能。
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.config.server.EnableConfigServer;@SpringBootApplication// 开启配置服务@EnableConfigServerpublic class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); }}
配置文件appication.yml
server: port: 8095spring: profiles: native application: name: config-server cloud: config: server: native: search-locations: classpath:/shared
在工程的 Resources 目录下建一个 shared
文件夹,用于存放本地配置文件。在shared 目录下,新建一个 config-client-dev.yml
文件,用作 config-client 工程的 dev (开发环境〉的配置文件。在 config-client-dev.yml 配置文件中,指定程序的端口号为 9999 ,并定义一个变量 foo,内容如下:
server: port: 9999foo: foo version 1
启动测试
访问:http://127.0.0.1:8095/config-client-dev.yml
创建一个model工程作为配置中心,即config-client
。
org.springframework.cloud spring-cloud-starter-config
在启动类中添加@EnableConfigServer
注解开启配置服务器的功能。
创建配置文件bootstrap.yml
,注意这里用的是 bootstrap.yml ,bootstrap 相对于 application 具有优先的执行顺序。
spring: application: name: config-client cloud: config: uri: http://127.0.0.1:8095/ fail-fast: true profile: dev name: config-client
spring.cloud.config.uri
:配置中心的地址spring.cloud.config.fail-fast
:如果没有读取成功,则执行快速失败{spring.cloud.config.name}
和变量{spring.cloud.config.profile}
,两者以“-”相连,构成了向 Config Server 读取的配置文件名,本案例为:config-client-dev.yml
文件。在Git服务器上准备配置文件。
配置文件的命名规则是:{application}-{profile}.properties/yml
jdbc.driverClassName=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=truejdbc.username=rootjdbc.password=123456
推送文件到git服务器,这里使用的是码云,当然也可以使用github或者使用svn。使用码云创建一个项目(私有项目需要账号密码)
创建一个model工程作为配置中心,即config-server
。
org.springframework.cloud spring-cloud-config-server
在程序的入口Application类加上@EnableConfigServer
注解开启配置服务器的功能。
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.config.server.EnableConfigServer;@SpringBootApplication// 开启配置服务@EnableConfigServerpublic class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); }}
编写配置文件application.yml:
server: port: 8085spring: application: name: config-server cloud: config: server: git: #配置git仓库地址 uri: https://gitee.com/yyangqqian/spring-cloud-config.git search-paths: - config #配置文件目录地址 #码云账号(公有项目不需要设置) username: #码云密码(公有项目不需要设置) password: #分支名称 label: master
配置说明:
启动测试
访问:http://127.0.0.1:8085/jdbc.url/dev
证明配置服务中心可以从远程程序获取配置信息。 /{application}/{profile}/[label]
/{application}-{profile}.yml /{label}/{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.properties
其中{label}是指分支,默认是master。
访问:http://127.0.0.1:8085/microservice-dev.yml
创建一个SpringBoot的model工程作为配置中心,即config-client
。其pom文件:
org.springframework.cloud spring-cloud-starter-config
新增配置文件bootstrap.yml
:
spring: application: name: config-client cloud: config: # 配置中心的地址 uri: http://127.0.0.1:8085/ # 对应配置服务中的{profile} profile: dev # 对应的分支 label: master# 必须和Git上的配置文件名相匹配,例如配置文件为:testConfig-dev.properties,这个时候对应的name=testConfig # 对应配置服务中的{application},不写默认是本应用名,即spring.application.name name: microserviceserver: port: 8086
变量{spring.cloud.config.name}
和变量{spring.cloud.config.profile}
,两者以“-”相连,构成了向配置中心读取的配置文件名,本案例为:microservice-dev.yml
文件。
编写对象通过@Value注解读取Config Server中的值。
import lombok.Getter;import lombok.Setter;import lombok.ToString;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;@Component@Setter@Getter@ToStringpublic class JdbcConfigBean { @Value("${jdbc.url}") private String url; @Value("${jdbc.username}") private String username; @Value("${jdbc.password}") private String password; @Value("${jdbc.driverClassName}") private String driverClassName;}
编写控制器
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class DemoController { @Autowired JdbcConfigBean jdbcConfigBean; @GetMapping("/demo") public String demo() { return jdbcConfigBean.toString(); }}
启动测试
将配置中心做成一个微服务注册到eureka,将其集群化,从而达到高可用。
导入EurekaClient的起步依赖。
org.springframework.cloud spring-cloud-starter-netflix-eureka-client
在application.yml
配置文件中增加注册到eureka的配置。
eureka: client: serviceUrl: defaultZone: http://localhost:8100/eureka/
最后在程序的启动类Application加上@EnableEurekaClient
开启eureka客户端注解。
导入EurekaClient的起步依赖。
org.springframework.cloud spring-cloud-starter-netflix-eureka-client
在bootstrap.yml
配置文件中增加注册到eureka的配置。
eureka: client: serviceUrl: defaultZone: http://localhost:8100/eureka/
修改bootstrap.yml
配置文件,将写死配置中心地址方式改成从eureka注册中心获取配置中心服务。
spring: application: name: config-client cloud: config: # 配置中心的地址# uri: http://127.0.0.1:8085/ # 对应配置服务中的{profile} profile: dev # 对应的分支 label: master # 必须和Git上的配置文件名相匹配,例如配置文件为:testConfig-dev.properties,这个时候对应的name=testConfig # 不写默认是本应用名,即spring.application.name name: microservice discovery: enabled: true #启用发现服务功能 service-id: config-server #指定配置中心的服务名
最后在程序的启动类Application加上@EnableEurekaClient
开启eureka客户端注解。
使用actuator
监控中心完成刷新功能。
config-client
项目上做修改。 导入依赖: org.springframework.boot spring-boot-starter-actuator
为动态更新配置内容的bean添加@RefreshScope
注解。
在application.yml文件中,添加配置如下:
#开启所有端点management: endpoints: web: exposure: include: "*"
重启测试
使用refrehs端点curl -X POST http://localhost:8086/actuator/refresh
刷新从Server中获取配置文件的属性值。
传统方式将zuul路由规则配置在配置文件中,如果修改了路由规则,需要重启服务器。可以将配置文件存在配置中心,利用SpringCloud Config实现动态路由规则添加。
在git服务器上创建配置文件service-zuul-dev.yml
,注释掉网关工程yml文件中的zuul配置,把配置拷到service-zuul-dev.yml中。
zuul: # 定义服务转发规则 routes: # 这个名字任意取的,建议取有意义的 api-ribbon: # 配置请求URL的请求规则 path: /api-ribbon/** # 真正的微服务地址,path匹配的请求都转发到这里 serviceId: eureka-ribbon-client api-feign: path: /api-feign/** serviceId: eureka-feign-client
导入依赖
org.springframework.boot spring-boot-starter-actuator org.springframework.cloud spring-cloud-starter-config
注释掉网关工程中的配置,并开启actuator:
management: endpoints: web: exposure: include: "*"
在网关工程新建bootstrap.yml
,启用Spring Cloud Config
功能,将zuul注册到eureka:
spring: application: name: service-zuul cloud: config: # 对应配置服务中的{profile} profile: dev # 对应的分支 label: master # 必须和Git上的配置文件名相匹配,例如配置文件为:testConfig-dev.properties,这个时候对应的name=testConfig # 不写默认是本应用名,即spring.application.name name: service-zuul discovery: enabled: true #启用发现服务功能 service-id: config-server #指定配置中心的服务名eureka: client: serviceUrl: defaultZone: http://localhost:9100/eureka/
在启动类增加@RefreshScope
注解,支持手动刷新。
转载地址:http://vgdhb.baihongyu.com/