10.1 概述
分布式系统面临的–配置文件问题
**
微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务,由于每个服务都需要必要的配置信息才能运行,所以一套集中式的,动态的配置管理设施是必不可少的。spring cloud提供了configServer来解决这个问题,我们每一个微服务自己带着一个application.yml,那上百个的配置文件修改起来,令人头疼!
什么是SpringCloud config分布式配置中心?
**
spring cloud config 为微服务架构中的微服务提供集中化的外部支持,配置服务器为各个不同微服务应用的所有环节提供了一个中心化的外部配置。
spring cloud config 分为服务端和客户端两部分。
服务端也称为 分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密,解密信息等访问接口。
客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息。配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理。并且可用通过git客户端工具来方便的管理和访问配置内容。
spring cloud config 分布式配置中心能干嘛?
**
- 集中式管理配置文件
- 不同环境,不同配置,动态化的配置更新,分环境部署,比如 /dev /test /prod /beta /release
- 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息
- 当配置发生变动时,服务不需要重启,即可感知到配置的变化,并应用新的配置
- 将配置信息以REST接口的形式暴露
spring cloud config 分布式配置中心与GitHub整合
**
由于spring cloud config 默认使用git来存储配置文件 (也有其他方式,比如自持SVN 和本地文件),但是最推荐的还是git ,而且使用的是 http / https 访问的形式。
10.2 入门案例
10.2.1 服务端
**
编写application.yml提交到github上或者码云上面(注意:—-和空格的输入,否则之后访问补到)
spring:
profiles:
active: dev
---
spring:
profiles: dev
application:
name: springcloud-config-dev
---
spring:
profiles: test
application:
name: springcloud-config-test
新建springcloud-config-server-3344模块导入pom.xml依赖
<dependencies>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--config-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>2.2.5.RELEASE</version>
</dependency>
<!--eureka-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
</dependencies>
resource下创建application.yml配置文件,Spring Cloud Config服务器从git存储库(必须提供)为远程客户端提供配置:
server:
port: 3344
spring:
application:
name: springcloud-config-server
# 连接github远程仓库
cloud:
config:
server:
git:
# 注意是https的而不是ssh
uri: https://github.com/lzh66666/spring-cloud-kuang.git
# 通过 config-server可以连接到git,访问其中的资源以及配置~
default-label: main
# 不加这个配置会报Cannot execute request on any known server 这个错:连接Eureka服务端地址不对
# 或者直接注释掉eureka依赖 这里暂时用不到eureka
eureka:
client:
register-with-eureka: false
fetch-registry: false
- application.properties配置文件
erver.port=3344
spring.application.name=springcloud-config-server
#连接远程仓库
spring.cloud.config.server.git.uri=https://gitee.com/Program_Monkey/spring-cloud-config.git
# 通过config-server 可以连接到git 访问其中的资源以及配置
注意:default-label属性,默认是master提交,我改成main提交之后页面死活出不来
可以输入git status
查看自己的分支
访问:http://localhost:3344/application-dev.yml页面
访问:http://localhost:3344/application-test.yml页面
HTTP服务具有以下格式的资源:(main是我的分支,默认为master)
/{application}/{profile}[/{label}] // http://localhost:3344/application/test/main /{application}-{profile}.yml // http://localhost:3344/application-test.yml /{label}/{application}-{profile}.yml // http://localhost:3344/main/application-test.yml /{application}-{profile}.properties /{label}/{application}-{profile}.properties
10.2.2 客户端
**
将本地git仓库springcloud-config文件夹下新建的config-client.yml提交到github或码云仓库:(千万别加注释,否则路径找不到)
spring:
profiles:
active: dev
---
server:
port: 8201
spring:
profiles: dev
application:
name: springcloud-provider-dept
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
---
server:
port: 8202
spring:
profiles: test
application:
name: springcloud-provider-dept
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
新建一个springcloud-config-client-3355模块,并导入依赖
<dependencies>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--config-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<!--actuator-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
resources下创建application.yml和bootstrap.yml配置文件
- bootstrap.yml是系统级别的配置
# 系统级别的配置
spring:
cloud:
config:
name: config-client # 需要从git上读取的资源名称,不要后缀
profile: dev
label: main
uri: http://localhost:3344
- application.yml是用户级别的配置
# 用户级别的配置
spring:
application:
name: springcloud-config-client
- 创建controller包下的ConfigClientController.java用于测试
@RestController
public class ConfigClientController {
@Value("${spring.application.name}")
private String applicationName; //获取微服务名称
@Value("${eureka.client.service-url.defaultZone}")
private String eurekaServer; //获取Eureka服务
@Value("${server.port}")
private String port; //获取服务端的端口号
@RequestMapping("/config")
public String getConfig(){
return "applicationName:"+applicationName +
"eurekaServer:"+eurekaServer +
"port:"+port;
}
}
- 主启动类
@SpringBootApplication
public class ConfigClient_3355 {
public static void main(String[] args) {
SpringApplication.run(ConfigClient_3355.class,args);
}
}
- 测试:
启动服务端Config_server_3344 再启动客户端ConfigClient
访问:http://localhost:8201/config/
10.3 小案例
本地新建config-dept.yml和config-eureka.yml并提交到码云仓库
**
- config-dept.yml
spring:
profiles:
active: dev
---
server:
port: 8081
mybatis:
type-aliases-package: nuc.ss.springcloud.pojo
config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:mybatis/mapper/*.xml
spring:
profiles: dev
application:
name: springcloud-config-dept
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://localhost:3306/db01?characterEncoding=utf-8&useUnicode=true
username: root
password: admin
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
instance:
instance-id: springcloud-provider-dept-8081
info:
app.name: lzh-springcloud
company.name: com.lzh
---
server:
port: 8081
mybatis:
type-aliases-package: nuc.ss.springcloud.pojo
config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:mybatis/mapper/*.xml
spring:
profiles: test
application:
name: springcloud-config-dept
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://localhost:3306/db02?characterEncoding=utf-8&useUnicode=true
username: root
password: admin
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
instance:
instance-id: springcloud-provider-dept-8081
info:
app.name: lzh-springcloud
company.name: com.lzh
- config-eureka.yml
spring:
profiles:
active: dev
---
server:
port: 7001
spring:
profiles: dev
application:
name: springcloud-config-eureka
eureka:
instance:
hostname: eureka7001.com
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
---
server:
port: 7001
spring:
profiles: test
application:
name: springcloud-config-eureka
eureka:
instance:
hostname: eureka7001.com
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
- 上传成功
新建springcloud-config-eureka-7001模块,并将原来的springcloud-eureka-7001模块下的内容拷贝的该模块。
- 清空该模块的application.yml配置
spring:
application:
name: springcloud-config-eureka-7001
- 并新建bootstrap.yml连接远程配置
spring:
cloud:
config:
name: config-eureka # 仓库中的配置文件名称
label: main
profile: dev
uri: http://localhost:3344
- 在pom.xml中添加spring cloud config依赖
**
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
- 主启动类
**
@SpringBootApplication
@EnableEurekaServer
public class EurekaServer_7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaServer_7001.class,args);
}
}
测试
**
- 启动 Config_Server_3344,并访问 http://localhost:3344/master/config-eureka-dev.yml 测试
- 启动ConfigEurekaServer_7001,访问 http://localhost:7001/ 测试
- 显示上图则成功
新建springcloud-config-dept-8081模块并拷贝springcloud-provider-dept-8081的内容
同理导入spring cloud config依赖、清空application.yml 、新建bootstrap.yml配置文件并配置
spring:
cloud:
config:
name: config-dept
label: main
profile: dev
uri: http://localhost:3344
- 主启动类
//启动类
@SpringBootApplication
@EnableEurekaClient //在服务启动后自动注册到Eureka中
@EnableDiscoveryClient //服务发现~
@EnableCircuitBreaker//添加对熔断的支持
public class DeptProvider_8081 {
public static void main(String[] args) {
SpringApplication.run(DeptProvider_8081.class,args);
}
//增加一个 Servlet
@Bean
public ServletRegistrationBean hystrixMetricsStreamServlet(){
ServletRegistrationBean registrationBean = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
//访问该页面就是监控页面
registrationBean.addUrlMappings("/actuator/hystrix.stream");
return registrationBean;
}
}
只需更改github远程即可实现部署