SpringCloud与微服务Ⅹ --- SpringCloud Config分布式配置中心
分布式系统面临的问题 --- 配置问题
SpringCloud Config是什么
SpringCloud Config分为服务端和客户端两部分。
服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口。
1.集中管理配置文件
3.运行期间动态调整配置,不再需要每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息。
4.当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置
5.将配置信息以REST接口的形式暴露
由于SpringCloud Config默认使用GIT来存储配置文件(也支持SVN和本地文件),但最推荐的还是git,而且使用http/https访问的形式。
2.在本地拉取该项目,使用 git clone git@github.com:SillyBoy007/microservice-config.git
3.在该项目里新建一个application.yml
4.编写yml文件,并保存为UTF-8格式
spring: profiles: active: - dev --- spring: profiles: dev #开发环境 application: name: microservice-config-wang-dev --- spring: profiles: test #测试环境 application: name: microservice-config-wang-test
5.上传yml文件到github
查看github的Code发现上面已经有了该文件。那么我们该如何读取github上的文件配置信息呢?
创建一个模块microservice-config-3344。
pom文件
<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.0</modelVersion> <parent> <groupId>com.wang.springcloud</groupId> <artifactId>microservice</artifactId> <version>1.0-SNAPSHOT</version> </parent> <artifactId>microservice-config-3344</artifactId> <dependencies> <!-- springCloud Config --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <!-- 避免Config的Git插件报错:org/eclipse/jgit/api/TransportConfigCallback --> <dependency> <groupId>org.eclipse.jgit</groupId> <artifactId>org.eclipse.jgit</artifactId> <version>4.10.0.201712302008-r</version> </dependency> <!-- 图形化监控 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- 熔断 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <!-- 热部署插件 --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> </project>
server:
port: 3344
spring:
application:
name: microservice-config
cloud:
config:
server:
git:
uri: git@github.com:SillyBoy007/microservice-config.git #git仓库地址
search-paths: microservice-config-learn #git项目的目录
@SpringBootApplication @EnableConfigServer public class SpringCloudConfig3344 { public static void main(String[] args) { SpringApplication.run(SpringCloudConfig3344.class,args); } }
127.0.0.1 config3344.com
启动microservice-config-3344项目,分别访问下列的地址
http://config3344.com:3344/application-dev.yml
http://config3344.com:3344/application-test.yml
http://config3344.com:3344/application-dasdd.yml
配置读取规则
1./{application}-{profile}.yml
http://config3344.com:3344/application-dev.yml
2./{application}/{profile}[/{label}]
http://config3344.com:3344/applicaiton/test/master
3./label/{application}-{profile}.yml
http://config3344.com:3344/master/application-test.yml
spring: profiles: active: - dev --- server: port: 8201 spring: profiles: dev application: name: microservice-config-client eureka: client: service-url: defaultZone: http://eureka-dev.com:7001/eureka/ --- server: port: 8202 spring: profiles: test application: name: microservice-config-client eureka: client: service-url: defaultZone: http://eureka-test.com:7001/eureka/
2.将上述的文件上传到github上。
pom文件:
<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.0</modelVersion> <parent> <groupId>com.wang.springcloud</groupId> <artifactId>microservice</artifactId> <version>1.0-SNAPSHOT</version> </parent> <artifactId>microservice-config-client-3355</artifactId> <dependencies> <!-- SpringCloud Config客户端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> </project>
新建bootstrap.yml
application.yml是用户级的资源配置项
bootstrap.yml是系统级的,优先级更高
SpringCloud会创建一个‘BootStrap Context’,作为Spring应用的Application Context
spring:
cloud:
config:
name: microservice-config-client #需要从github上读取的资源名称,没有yml后缀名
profile: dev
label: master
uri: http://config3344.com:3344 #本微服务启动后先去找3344号服务,通过SpringCloud Config获取GitHub的服务地址
spring:
application:
name: microservice-config-client
127.0.0.1 client-config.com
主启动类
package com.wang.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ConfigClient3355 { public static void main(String[] args) { SpringApplication.run(ConfigClient3355.class,args); } }
package com.wang.springcloud.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ConfigClientRestController { @Value("${spring.application.name}") private String applicationName; @Value("${eureka.client.service-url.defaultZone}") private String eurekaServers; @Value("${server.port}") private String port; @RequestMapping public String getConfig(){ return "applicationName:"+applicationName+"\t eurekaServers:"+eurekaServers+"\t port:"+port; } }
1.先启动microservice-config-3344项目,再启动microservice-config-client-3355项目。
2.访问http://client-config.com:8201/,http://client-config.com:8201/config
3.修改bootstrap.yml,将环境profile切换为test
4.访问http://client-config.com:8202/ , http://client-config.com:8202/config
六.SpringCloud Config 实际应用
修改microservice-config项目
spring: profiles: active: - dev --- server: port: 7001 spring: profiles: dev application: name: microservice-config-eureka-client eureka: instance: hostname: eureka7001.com #eureka实例的主机名 client: register-with-eureka: false #不把自己注册到eureka上 fetch-registry: false #不从eureka上来获取服务的注册信息 service-url: defaultZone: http://eureka7001.com:7001/eureka/ --- server: port: 7001 spring: profiles: test application: name: microservice-config-eureka-client eureka: instance: hostname: eureka7001.com #eureka实例的主机名 client: register-with-eureka: false #不把自己注册到eureka上 fetch-registry: false #不从eureka上来获取服务的注册信息 service-url: defaultZone: http://eureka7001.com:7001/eureka/
spring: profiles: active: - dev --- server: port: 8001 spring: profiles: dev application: name: microservice-config-dept-client datasource: type: com.alibaba.druid.pool.DruidDataSource #数据源类型 driver-class-name: org.gjt.mm.mysql.Driver #数据库驱动 url: jdbc:mysql://localhost:3306/cloudDB01 #数据库url username: root password: 123456 dbcp2: min-idle: 5 #数据库连接池的最小维持连接数 initial-size: 5 #初始化连接数 max-total: 5 #最大连接数 max-wait-millis: 200 #等待连接获取的最大超时时间 mybatis: config-location: classpath:mybatis/mybatis.cfg.xml #mybatis配置文件所在路径 type-aliases-package: com.wang.springcloud.entities #所有entity别名类所在包 mapper-locations: classpath:mybatis/mapper/**/*.xml #mapper映射文件 eureka: client: service-url: defaultZone: http://eureka7001.com:7001/eureka --- server: port: 8002 spring: profiles: test application: name: microservice-config-dept-client datasource: type: com.alibaba.druid.pool.DruidDataSource #数据源类型 driver-class-name: org.gjt.mm.mysql.Driver #数据库驱动 url: jdbc:mysql://localhost:3306/cloudDB02 #数据库url username: root password: 123456 dbcp2: min-idle: 5 #数据库连接池的最小维持连接数 initial-size: 5 #初始化连接数 max-total: 5 #最大连接数 max-wait-millis: 200 #等待连接获取的最大超时时间 mybatis: config-location: classpath:mybatis/mybatis.cfg.xml #mybatis配置文件所在路径 type-aliases-package: com.wang.springcloud.entities #所有entity别名类所在包 mapper-locations: classpath:mybatis/mapper/**/*.xml #mapper映射文件 eureka: client: service-url: defaultZone: http://eureka7001.com:7001/eureka
3.上传上述两个新文件到github
4.新建microservice-config-eureka-server-7001项目
<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.0</modelVersion> <parent> <groupId>com.wang.springcloud</groupId> <artifactId>microservice</artifactId> <version>1.0-SNAPSHOT</version> </parent> <artifactId>microservice-config-eureka-server-7001</artifactId> <dependencies> <!-- SpringCloudConfig配置 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <!-- 热部署插件 --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> </project>
bootstrap.yml
spring:
cloud:
config:
name: microservice-config-eureka-server
profile: dev
label: master
uri: http://config3344.com:3344 #本微服务启动后先去找3344号服务,通过SpringCloud Config获取GitHub的服务地址
spring:
application:
name: microservice-config-eureka-server
主启动类
package com.wang.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class ConfigEurekaServer7001 { public static void main(String[] args) { SpringApplication.run(ConfigEurekaServer7001.class,args); } }
4.新建microservice-config-dept-client-8001项目
pom文件
<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.0</modelVersion> <parent> <groupId>com.wang.springcloud</groupId> <artifactId>microservice</artifactId> <version>1.0-SNAPSHOT</version> </parent> <artifactId>microservice-config-dept-client-8001</artifactId> <dependencies> <!-- SpringCloudConfig配置 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>com.wang.springcloud</groupId> <artifactId>microservice-api</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> </project>
spring: cloud: config: name: microservice-config-dept-client #需要从github上读取的资源名称,注意没有yml后缀名 #profile配置是什么就取什么配置dev or test profile: dev #配置环境 #profile: test label: master uri: http://config3344.com:3344 #SpringCloudConfig获取的服务地址
application.yml
spring:
application:
name: microservice-config-dept-client
4.拷贝microservice-provider-dept-8001项目的mybatis资源文件以及mapper、service业务代码
5.启动测试