SpringCloud Config客户端的配置
创建Spring Cloud Config客户端,通过服务端从git获取配置文件信息,首先创建一个往git上面创建一个microservicecloud-config-client.yml配置文件
填写
spring: profiles: active: - dev --- server: port: 8201 spring: profiles: dev application: name: microservicec loud-config-client eureka: client: serviceUrl: defaultZone: http://eureka7001:7001/eureka/ --- server: port: 8202 spring: profiles: test application: name: microservicec loud-config-client eureka: client: serviceUrl: defaultZone: http://eureka7001:7001/eureka/
然后在当前目录下 右键打开Git Bash Here命令行面板
输入命令:
git status git add . git commit -m "test file" #提交添加注释 git push origin master # 将本地master分支推送到origin主机
提交到github
创建一个microservicecloud-config-client-3355微服务,添加依赖
<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.atguigu.springcloud</groupId> <artifactId>microservicecloud</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>microservicecloud-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文件
spring: cloud: config: name: microservicecloud-config-client #需要从github上读取的资源名称,注意没有yml后缀名 profile: test #本次访问的配置项 label: master uri: http://config-3344.com:3344 #本微服务启动后先去找3344号服务,通过SpringCloudConfig获取GitHub的服务地址
配置bootstrap.yml的原因
https://blog.csdn.net/dulabing/article/details/80183662
https://www.cnblogs.com/fengli9998/p/8489084.html
配置application.yml
spring: application: name: microservicecloud-config-client
获取配置文件信息类
package com.atguigu.springcloud.rest; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ConfigClientRest { @Value("${spring.application.name}") private String applicationName; @Value("${eureka.client.serviceUrl.defaultZone}") private String eurekaServers; @Value("${server.port}") private String port; @RequestMapping("/config") public String getConfig() { String str = "applicationName: " + applicationName + "\t eurekaServers:" + eurekaServers + "\t port: " + port; System.out.println("******str: " + str); return "applicationName: " + applicationName + "\t eurekaServers:" + eurekaServers + "\t port: " + port; } }
启动类
package com.atguigu.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ConfigClient_3355_StartSpringCloudApp { public static void main(String[] args) { SpringApplication.run(ConfigClient_3355_StartSpringCloudApp.class, args); } }
分别启动
microservicecloud-eureka-7001
microservicecloud-config-3344
microservicecloud-config-client-3355
由于客户端的bootstrap.yml配置的是test所有访问的是git上面的8202端口
http://client-config.com:8202/config
当我们把bootstrap配置文件中的 profile更改为 dev,再启动访问的便是git上的8201端口
config服务端配置ok且测试通过,我们可以和config+GitHub进行配置修改并获取内容
SpringCloud Config客户端配置实战
做一个eureka服务+一个访问的微服务,将两个微服务的配置统由github获得实现统一配置分布式管理,完成多环境的变更。
eureka的git配置文件microservicecloud-config-eureka-client可以根据dev或者test切换不同的配置
spring: profiles: active: - dev --- server: port: 7001 spring: profiles: dev application: name: microservicecloud-config-eureka-client eureka: instance: hostname: eureka7001 #eureka服务端的实例名称 prefer-ip-address: true #访问路径可以显示IP地址 client: register-with-eureka: false #false表示不向注册中心注册自己。 fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务 service-url: defaultZone: http://eureka7001:7001/eureka/ --- server: port: 7001 spring: profiles: test application: name: microservicecloud-config-eureka-client eureka: instance: hostname: eureka7001 #eureka服务端的实例名称 prefer-ip-address: true #访问路径可以显示IP地址 client: register-with-eureka: false #false表示不向注册中心注册自己。 fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务 service-url: defaultZone: http://eureka7001:7001/eureka/
提供接口的微服务的git配置文件可以根据dev或者test切换不同配置,连接不同数据库
spring: profiles: active: - dev --- server: port: 8001 spring: profiles: dev application: name: microservicecloud-config-dept-client datasource: type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型 driver-class-name: org.gjt.mm.mysql.Driver # mysql驱动包 url: jdbc:mysql://localhost:3306/cloudDB01 # 数据库名称 username: root password: 671354 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.smxy.lq.entities # 所有Entity别名类所在包 mapper-locations: - classpath:mybatis/mapper/**/*.xml # mapper映射文件 eureka: client: #客户端注册进eureka服务列表内 service-url: defaultZone: http://eureka7001:7001/eureka/ instance: instance-id: microservicecloud-dept8001 #服务名称 prefer-ip-address: true #访问路径可以显示IP地址 info: app.name: microservicecloud company.name: www.baidu.com --- server: port: 8001 spring: profiles: test application: name: microservicecloud-config-dept-client datasource: type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型 driver-class-name: org.gjt.mm.mysql.Driver # mysql驱动包 url: jdbc:mysql://localhost:3306/cloudDB02 # 数据库名称 username: root password: 671354 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.smxy.lq.entities # 所有Entity别名类所在包 mapper-locations: - classpath:mybatis/mapper/**/*.xml # mapper映射文件 eureka: client: #客户端注册进eureka服务列表内 service-url: defaultZone: http://eureka7001:7001/eureka/ instance: instance-id: microservicecloud-dept8001 #服务名称 prefer-ip-address: true #访问路径可以显示IP地址 info: app.name: microservicecloud company.name: www.baidu.com
新建microservicecloud-config-eureka-client-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.atguigu.springcloud</groupId> <artifactId>microservicecloud</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>microservicecloud-config-eureka-client-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,指向github的配置文件
spring: cloud: config: name: microservicecloud-config-eureka-client #需要从github上读取的资源名称,注意没有yml后缀名 profile: dev #本次访问的配置项 label: master uri: http://config-3344.com:3344 #本微服务启动后先去找3344号服务,通过SpringCloudConfig获取GitHub的服务地址
application.yml
spring: application: name: microservicecloud-config-eureka-client
其他的与之前的7001没什么区别
新建microservicecloud-config-dept-client-8001微服务添加依赖
<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.atguigu.springcloud</groupId> <artifactId>microservicecloud</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>microservicecloud-config-dept-client-8001</artifactId> <dependencies> <!-- SpringCloudConfig配置 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <!-- 引入自己定义的api通用包,可以使用Dept部门Entity --> <dependency> <groupId>com.atguigu.springcloud</groupId> <artifactId>microservicecloud-api</artifactId> <version>${project.version}</version> </dependency> <!-- actuator监控信息完善 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- 将微服务provider侧注册进eureka --> <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>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>
添加bootstrap.yml配置,name填写github的配置文件
spring: cloud: config: name: microservicecloud-config-dept-client #需要从github上读取的资源名称,注意没有yml后缀名 #profile配置是什么就取什么配置dev or test profile: dev #profile: test label: master uri: http://config-3344.com:3344 #SpringCloudConfig获取的服务地址
其它的配置与之前的microservicecloud-provider-dept-8001一致,依次启动
microservicecloud-config-3344、
microservicecloud-config-eureka-client-7001
microservicecloud-config-dept-client-8001的微服务,
更改bootstrap.yml中的profile的属性就可以实现动态更改策略
springcloud完整代码:
https://github.com/yjp245/microservicecloud