博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springcloud(十二)-springcloud-config统一管理微服务配置
阅读量:4621 次
发布时间:2019-06-09

本文共 7863 字,大约阅读时间需要 26 分钟。

1.为什么要统一管理微服务配置

对于传统的单体应用,常使用配置文件管理所有配置。例如一个SpringBoot开发的单体应用,可将配置内容放在application.yml文件中。如果需要切换环境,可设置多个Profile,并在启动应用时指定spring.profiles.active={profile}.

然而,微服务架构中,微服务的配置管理一般有以下需求:

  1. 集中管理配置。一个使用微服务架构的应用系统可能会包含成百上千个微服务,因此集中管理配置是非常有必要的。
  2. 不同环境,不同配置。例如,数据源配置在不同的环境(开发、测试、预发布、生产等)中是不同的。
  3. 运行期间可动态调整。例如,可根据各个微服务的负载情况,动态调整数据源连接池大小或熔断阈值,并且在调整时不停止微服务。
  4. 配置修改后可自动更新。如配置内容发生变化,微服务能够自动更新配置。

综上所述,对于微服务架构而言,一个通用的配置管理机制是必不可少的,常见的做法是使用配置服务器管理配置。

2.Spring Cloud Config 简介

  spring cloud config为分布式系统外部化配置提供了服务器和客户端的支持,它包括Config Server和ConfigClient 两部分。由于Config Server和Config Client都实现了对Spring Environment和PropertySource抽象的映射,因此,Spring Cloud Config非常适合Spring应用程序,当然也可与任何其他语言编写的应用程序配合使用。

  Config Server是一个可横向扩展、集中式的配置服务器,它用于集中管理应用程序各个环境下的配置,默认使用Git存储配置文件(也可使用Subversion、本地文件系统或Vault存储配置),因此可以很方便地实现对配置的版本控制与内容审计。

  Config Client 是Config Server 的客户端,用于操作存储在Config Server中的配置属性。所有的微服务都指向Config Server。各个微服务在启动时,会请求Config Server 以获取所需要的配置属性,然后缓存这些属性以提高性能。

3.编写Config Server

1.在Git仓库https://gitee.com/fengyuduke/spring-cloud-config-repo中新建几个配置文件,

例如:

   

内容分别是:

profile=dev-1.0profile=production-1.0profile=texst-1.0profile=default-1.0

为了测试版本控制,为该Git创建config-label-v2.0分支,并为各个配置文件中的1.0改为2.0。

2.创建一个maven工程,Artifacted是microservice-config-server,并为项目添加一下依赖。

4.0.0
com.itmuch.cloud
microservice-config-server
0.0.1-SNAPSHOT
jar
microservice-config-server
http://maven.apache.org
org.springframework.boot
spring-boot-starter-parent
1.5.9.RELEASE
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-tomcat
org.springframework.boot
spring-boot-devtools
true
org.springframework.cloud
spring-cloud-config-server
org.springframework.cloud
spring-cloud-dependencies
Edgware.RELEASE
pom
import
org.springframework.boot
spring-boot-maven-plugin

3.编写启动类,在启动类上添加注解@EnableConfigServer,声明这是一个Config Server。

@SpringBootApplication@EnableConfigServerpublic class Application {    public static void main(String[] args) {        SpringApplication.run(Application.class, args);    }}

4.编写配置文件application.yml,并在其中添加一下内容。

server:  port: 8080spring:  application:    name: microservice-config-server  cloud:    config:      server:        git:          # 配置Git仓库的地址          uri: https://gitee.com/fengyuduke/spring-cloud-config-repo          # Git仓库的账号          username:           #Git仓库的密码          password:

这样,一个Config Server就完成了。

 

可以使用Config Server的端点获取配置文件的内容。端点与配置文件的映射规则如下:

/{application}/{profile}[/{label}]/{application}-{profile}.yml/{label}/{application}-{profile}.yml/{application}-{profile}.properties/{label}/{application}-{profile}.properties

以上端点都可映射到{application}-{profile}.properties这个配置文件,{application}表示微服务名称,{label}对应Git仓库的分支,默认是master 。

按照以上规则,对于本例,可使用以下URL访问到Git仓库master分支的microservice-foo-dev.properties,例如:

  • http://localhost:8080/microservice-foo/dev
  • http://localhost:8080/microservice-foo-dev.properties
  • http://localhost:8080/microservice-foo-dev.yml

测试一下:

访问http://localhost:8080/microservice-foo/dev

 

访问http://localhost:8080/microservice-foo-dev.properties,返回配置文件中的属性:

访问http://localhost:8080/config-label-v2.0/microservice-foo-dev.properties,获得如下结果:

说明获得了Git仓库config-label-v2.0分支中的配置信息。

至此,已成功构建了Config Server,并通过构造URL的方式,获取了Git仓库中的配置信息。

 

4.编写Config Client

1.创建一个maven工程,ArtifactId是microservice-config-client,并为项目添加以下依赖。

4.0.0
com.itmuch.cloud
microservice-config-client
0.0.1-SNAPSHOT
jar
microservice-config-client
http://maven.apache.org
org.springframework.boot
spring-boot-starter-parent
1.5.9.RELEASE
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-tomcat
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-devtools
true
org.springframework.cloud
spring-cloud-starter-config
org.springframework.cloud
spring-cloud-dependencies
Edgware.RELEASE
pom
import
org.springframework.boot
spring-boot-maven-plugin

2.创建一个基本的Springboot启动类。

3.编写配置文件application.yml,并在其中添加以下内容。

server:  port: 8081

4.创建配置文件bootstrap.yml,并在其中添加如下内容。

spring:  application:    # 对应config server所获取的配置文件的{application}    name: microservice-foo  cloud:    config:      uri: http://localhost:8080/      # profile对应config server所获取的配置文件中的{profile}      profile: dev      # 指定Git仓库的分支,对应config server所获取的配置文件的{label}      label: master

其中:

  • spring.application.name:对应Config Server 所获取的配置文件中的{application}。
  • spring.cloud.config.uri:指定Config Server 的地址,默认是http://localhost:8888.
  • spring.cloud.config.profile:profile对应Config Server所获取的配置文件中的{profile}。
  • spring.cloud.config.label:指定Git仓库的分支,对应Config Server所获取配置文件的{label}。

值得注意的是,以上属性必须配置在bootstrap.yml文件中,而不是application.yml。原因参考:https://www.cnblogs.com/fengyuduke/p/11024078.html

 5.编写Controller

@RestControllerpublic class ConfigClientController {    @Value("${profile}")    private String profile;        @GetMapping("/profile")    public String hello() {        return this.profile;    }}

在Controller中,通过注解@Value("${profile}"),绑定Git仓库配置文件中的profile属性。

测试

1.启动microservice-config-server。

2.启动microservice-config-client.

3.访问http://localhost:8081/profile,可获得如下结果。

 

说明Config Client能够正常通过Config Server获得Git仓库中对应环境的配置。

 

转载于:https://www.cnblogs.com/fengyuduke/p/11246332.html

你可能感兴趣的文章
vue入门
查看>>
JS线程Web worker
查看>>
Flex的动画效果与变换!(三)(完)
查看>>
mysql常见错误码
查看>>
Openresty 与 Tengine
查看>>
使用XV-11激光雷达做hector_slam
查看>>
布局技巧4:使用ViewStub
查看>>
学习记事
查看>>
java 子类重写父类的方法应注意的问题
查看>>
[LevelDB] LevelDB理论基础
查看>>
【codecombat】 试玩全攻略 第一关kithguard地牢
查看>>
【DP】 POJ 1191 棋盘分割 记忆化搜索
查看>>
自动化测试 Appium之Python运行环境搭建 Part2
查看>>
说说DBA职责和目标
查看>>
从头认识Spring-2.4 基于java的标准注解装配-@Inject-限定器@Named
查看>>
sql server 实现多表连接查询
查看>>
Python标准库:内置函数getattr(object, name[, default])
查看>>
转:android 自定义RadioButton样式
查看>>
HTTP请求过程
查看>>
织梦多域名解析到同一个空间导致打开链接不一致怎么办?
查看>>