什么是springCloud Config
- 为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不容微服务应用的所有环境提供了一个中心化的外部配置。
- 分为服务端和客户端。
config服务端与Git通讯
- 码云(gitee)上先搭建仓库和文本内容
- 搭建方式:https://mp.csdn.net/postedit/100901379
- 搭建地址:https://gitee.com/Xiaokeworksveryhard/springCloud
- 搭建内容:
spring: profiles: active: - dev #注意有空格 --- spring: profiles: dev #开发环境 application: name: springCloud_config --- spring: profiles: test #测试环境 application: name: xiaoke #请保存为UTF-8格式
- 搭建服务端cloud_config配置项目
- pom引入
<!--springCloudConfig--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</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>
- application.yml引入,idea中的uri一定要引入https格式,git@格式会出现Reject HostKey,暂时没解决方案。
server: port: 3344 spring: application: name: springCloud_config cloud: config: server: git: uri: https://gitee.com/Xiaokeworksveryhard/springCloud.git # git上的名字 # search-paths: application # username: 账号 # password: 密码 # label: master # hosts配置了www.myconfig.com 127.0.0.1
- 启动项引入@EnableConfigServer
- 访问下面地址可以直接访问到 码云gitee上
- 访问方式:http://项目ip:项目端口/gitee上文件
- http://www.myconfig.com:3344/application-dev.yml
- http://www.myconfig.com:3344/application-test.yml
- 结论:服务端配置项已经与gitee互通,客户通过该配置项直接访问gitee上文件。
config客户端通过config服务端获得github上的配置
- 搭建客户端cloud_config_client
- pom
<!-- 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-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency>
- 建bootstrap.yml
- 优先级大于application.yml,两个文件互补。
- 有相同内容application会覆盖bootstrap
注意:必须指定gitee上地址(来自其他项目),环境、分支、文件名。spring: cloud: config: name: spring-cloud-config-client #需要从码云上读取资源名称,注意没有yml后缀名 profile: dev label: master uri: http://www.myconfig.com:3344 #本微服务启动后先去找3344号服务,通过SpringCloudConfig获取GitHub的服务地址
- 建application.yml(先空着)
-
与bootstrap.yml互补,如果有的话会覆盖
-
- controller
注意:@Value("${xxx}") 找不到启动不了项目package com.config.client.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 ConfigClientController { @Value("${spring.application.name}") private String applicationName; @Value("${eureka.client.service-url.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; } }
- git上创建spring-cloud-config-client.yml
- 内容
spring: profiles: active: - dev --- spring: profiles: dev #开发环境 application: name: spring-cloud-config-dev server: port: 2010 eureka: client: service-url: defaultZone: http://www.one.com:7001/eureka/ --- spring: profiles: test #测试环境 application: name: spring-cloud-config-test server: port: 2020 eureka: client: service-url: defaultZone: http://www.one.com:7001/eureka/ #请保存为UTF-8格式
- 内容
-
结论
- 访问:http://www.myconfigclient.com:2010/config
- 结果:applicationName: spring-cloud-config-dev eurekaServers:http://www.one.com:7001/eureka/ port: 2010
- 绿色结果来自gitee上spring-cloud-config-client文件
- 流程:启动 > bootstrap.yml > application.yml(覆盖) > http://www.myconfig.com:3344 > gitee > 通过name:spring-cloud-config-client & master > 找到 > dev > spring.application.name、eureka.client.service-url.defaultZone、server.port > http://www.myconfigclient.com:server.port/config > 打印出结果
- 访问:http://www.myconfigclient.com:2010/config
config配置演示与策略切换
- 做一个eureka服务 + 一个dept访问你的微服务, 将两个微服务的配置统一由github 获得实现统一配置分布式管理,完成多环境的变更。
- git配置项目继续使用http://www.myconfig.com:3344,该项目与gitee已经连接上
- 搭建Eureka服务 + git配置项目
- 创建项目eureka_service_config_client,按照eureka注册中心那样搭建,在这基础上加上springcloudConfig
- POM
<!--eureka-server服务端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <!-- 修改后立即生效,热部署 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> <!-- SpringCloud Config客户端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
- 启动项
@EnableEurekaServer public class EurekaServiceConfigClientApp { public static void main(String[] args) { SpringApplication.run(EurekaServiceConfigClientApp.class, args); } }
- bootstrap.yml
spring: cloud: config: name: microservicecloud-config-eureka-client #需要从码云上读取资源名称,注意没有yml后缀名 microservicecloud-config-eureka-client profile: dev label: master uri: http://www.myconfig.com:3344 #本微服务启动后先去找3344号服务,通过SpringCloudConfig获取GitHub的服务地址
- application.yml没什么用
spring: application: name: microservicecloud-config-eureka-client
- git文件:microservicecloud-config-eureka-clien.yml
spring: profiles: active: - dev --- server: port: 7001 #注册中心占用7001端口,冒号后面要有空格 spring: profiles: dev application: name: microservicecloud-config-eureka-client eureka: instance: hostname: www.euraka7001.com client: register-with-eureka: false #当前的eureka-server自己不注册进服务列表中 fetch-registry: false #不通过eureka获取注册信息 service-url: defaultZone: http://www.euraka7001.com:7001/eureka/ --- server: port: 7001 #注册中心占用7001端口,冒号后面要有空格 spring: profiles: test application: name: microservicecloud-config-eureka-client eureka: instance: hostname: www.euraka7001.com client: register-with-eureka: false #当前的eureka-server自己不注册进服务列表中 fetch-registry: false #不通过eureka获取注册信息 service-url: defaultZone: http://www.euraka7001.com:7001/eureka/
- 测试:先自动config服务项目 cloud_config、 在启动eureka_service_config_client
- 搭建微服务,服务提供者config
- 创建项目cloud_perovider_config_client
- pom
<dependency> <groupId>com.springcloud</groupId> <artifactId>cloud_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>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jetty --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> <version>2.1.6.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web --> <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.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> <!-- 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> <!-- eureka 点击服务名称跳转页面的内容监控 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> <version>1.5.9.RELEASE</version> </dependency>
- 复制增删改查、启动项、mybatis
- 增加bootstrap.yml
spring: cloud: config: name: microservicecloud-config-dept-client #需要从码云上读取资源名称,注意没有yml后缀名microservicecloud-config-dept-client.yml profile: test label: master uri: http://www.myconfig.com:3344 #本微服务启动后先去找3344号服务,通过SpringCloudConfig获取GitHub的服务地址
- 增加application.yml
# 与bootstrap.yml互补,如果有的话会覆盖 #spring: ## cloud: ## config: ## name: spring-cloud-config-client # hosts文件 www.myconfigclient.com 127.0.0.1 spring: application: name: microservicecloud-config-dept-client
-
git上:microservicecloud-config-dept-client.yml文件
spring: profiles: active: - dev --- server: port: 8081 mybatis: config-location: classpath:mybatis/mybatis.cfg.xml #mybatis配置文件所在路径 type-aliases-package: com.hongdou.entity #所有Entity别名类所在的包 mapper-locations: - classpath:mybatis/mapper/**/*.xml #mapper 映射的文件 spring: profiles: dev application: name: microservicecloud-config-dept-client datasource: driver-class-name: org.gjt.mm.mysql.Driver #mysql驱动包 url: jdbc:mysql://192.168.3.75:3306/xiaoke01 #s数据库名称 type: com.alibaba.druid.pool.DruidDataSource #当前数据源操作类型 username: root password: 123456 dbcp2: min-idle: 5 #数据库连接池的最小维持连接数 initial-size: 5 #初始化连接数 max-total: 5 #最大连接数 max-wait-millis: 200 #等待连接获取的最大超时时间 eureka: client: service-url: defaultZone: http://www.euraka7001.com:7001/eureka/ instance: instance-id: dept-8001.com #在eureka上显示自己设置的服务名称 prefer-ip-address: true # 鼠标移动在eureka上显示自己的ip info: app.name: cloud_procider_服务提供者 company.name: www.dev.com build.artifactId: $project.artifactId$ build.version: $project.version$ --- server: port: 8081 mybatis: config-location: classpath:mybatis/mybatis.cfg.xml #mybatis配置文件所在路径 type-aliases-package: com.hongdou.entity #所有Entity别名类所在的包 mapper-locations: - classpath:mybatis/mapper/**/*.xml #mapper 映射的文件 spring: profiles: test application: name: microservicecloud-config-dept-client datasource: driver-class-name: org.gjt.mm.mysql.Driver #mysql驱动包 url: jdbc:mysql://192.168.3.75:3306/xiaoke02 #s数据库名称 type: com.alibaba.druid.pool.DruidDataSource #当前数据源操作类型 username: root password: 123456 dbcp2: min-idle: 5 #数据库连接池的最小维持连接数 initial-size: 5 #初始化连接数 max-total: 5 #最大连接数 max-wait-millis: 200 #等待连接获取的最大超时时间 eureka: client: service-url: defaultZone: http://www.euraka7001.com:7001/eureka/ instance: instance-id: dept-8001.com #在eureka上显示自己设置的服务名称 prefer-ip-address: true # 鼠标移动在eureka上显示自己的ip info: app.name: cloud_procider_服务提供者 company.name: www.test.com build.artifactId: $project.artifactId$ build.version: $project.version$
- 测试:启动cloud_config、eureka_service_config_client、cloud_perovider_config_client
git地址:[email protected]:Xiaokeworksveryhard/spring_cloud.git