SpringCloud微服务之Config知识总结

目录一、什么是Spring Cloud Config?二、搭建GIT环境三、服务端示例四、客户端示例五、安全认证示例六、集群搭建示例

一、什么是Spring Cloud Config? Spring Cloud Config 可以为微服务架构中的应用提供集中化的外部配置支持,它分为服务端和客户端两个部分。 Spring Cloud Config 服务端被称为分布式配置中心,它是个独立的应用,可以从配置仓库获取配置信息并提供给客户端使用。 Spring Cloud Config 客户端可以通过配置中心来获取配置信息,在启动时加载配置。 Spring Cloud Config 的配置中心默认采用Git来存储配置信息,所以天然就支持配置信息的版本管理,并且可以使用Git客户端来方便地管理和访问配置信息。

二、搭建GIT环境

创建仓库

创建文件

master分支

# config-dev.ymlconfig:  info: "config info for dev(master)"# config-test.ymlconfig:  info: "config info for test(master)"# config-prod.ymlconfig:  info: "config info for prod(master)"

dev分支

# config-dev.ymlconfig:  info: "config info for dev(dev)"# config-test.ymlconfig:  info: "config info for test(dev)"# config-prod.ymlconfig:  info: "config info for prod(dev)"

三、服务端示例

添加依赖

<dependency>    <groupId>org.springframework.cloud</groupId>    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>    <version>2.2.0.RELEASE</version></dependency><dependency>    <groupId>org.springframework.cloud</groupId>    <artifactId>spring-cloud-config-server</artifactId>    <version>2.2.0.RELEASE</version></dependency>

添加配置

启动类

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

application.yml配置文件

server:  port: 8888spring:  application:    name: config-server  cloud:    config:      server:        # 配置存储配置信息的Git仓库        git:          uri: https://gitee.com/prochick/spring-cloud-config.git          # Git用户名          username: xxx          # Git密码          password: xxx          # 指定是否开启启动时直接从git获取配置          clone-on-start: trueeureka:  instance:    prefer-ip-address: true    instance-id: config-server-8888  client:    fetch-registry: false    register-with-eureka: true    service-url:      defaultZone: http://localhost:8010/eureka/

访问说明

# 获取配置信息/{label}/{application}-{profile}# 获取配置文件信息/{label}/{application}-{profile}.yml

application

代表应用名称,默认为配置文件中的spring.application.name,如果配置了spring.cloud.config.name,则为该名称

label

代表分支名称,对应配置文件中的spring.cloud.config.label

profile

代表环境名称,对应配置文件中的spring.cloud.config.profile

测试使用

# 访问http://localhost:8888/master/config-dev来获取master分支上dev环境的配置信息# 访问http://localhost:8888/master/config-dev.yml来获取master分支上dev环境的配置文件信息# 访问http://localhost:8888/master/config-test.yml来获取master分支上test环境的配置文件信息# 访问http://localhost:8888/dev/config-dev.yml来获取dev分支上dev环境的配置文件信息

四、客户端示例

添加依赖

<dependency>   <groupId>org.springframework.boot</groupId>   <artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-web</artifactId></dependency><dependency>    <groupId>org.springframework.cloud</groupId>    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>    <version>2.2.0.RELEASE</version></dependency><dependency>    <groupId>org.springframework.cloud</groupId>    <artifactId>spring-cloud-starter-config</artifactId>    <version>2.2.0.RELEASE</version></dependency>

添加配置

配置文件

bootstrap.yml

server:  port: 9999spring:  application:    name: config-client  cloud:    config:      # 配置中心地址      uri: http://localhost:8888      # 分支名称      label: master      # 配置文件名称      name: config      # 配置后缀名称      profile: deveureka:  instance:    prefer-ip-address: true    instance-id: config-client-9999    client:      fetch-registry: false      register-with-eureka: true      service-url:        defaultZone: http://localhost:8010/eureka/ # 暴露刷新监控 management:  endpoints:    web:      exposure:        include: 'refresh'

控制器类

@RestController@RefreshScopepublic class ConfigController {    @Value("${config.info}")    private String configInfo;    @GetMapping("/configInfo")    public String getConfigInfo() {        return configInfo;    }}

测试使用

# 访问http://localhost:9999/configInfo 可以获取到dev分支下dev环境的配置

五、安全认证示例

添加依赖

<dependency>    <groupId>org.springframework.cloud</groupId>    <artifactId>spring-cloud-config-server</artifactId>    <version>2.2.0.RELEASE</version></dependency><dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-security</artifactId></dependency>

添加配置

服务端配置

server:  port: 8888spring:  application:    name: config-server  # 配置存储配置信息的Git仓库  cloud:    config:      server:        git:          # 访问地址          uri: https://gitee.com/prochick/spring-cloud-config.git          # Git用户名          username: xxx          # Git密码          password: xxx          # 指定是否开启启动时直接从git获取配置          clone-on-start: true  # 配置用户名和密码  security:     user:      name: xxx      password: xxx

客户端配置

server:  port: 9999spring:  application:    name: config-client  cloud:    config:      # 配置中心地址      uri: http://localhost:8888      # 分支名称      label: master      # 配置文件名称      name: config      # 配置后缀名称      profile: dev      # 配置中心用户名      username: xxx      # 配置中心密码      password: xxx

六、集群搭建示例

添加配置

bootstrap.yml

server:  port: 9999spring:  application:    name: config-client  cloud:    config:      # 分支名称      label: master      # 配置文件名称      name: config      # 配置后缀名称      profile: dev      # 集群绑定      discovery:        enabled: true        service-id: config-servereureka:  instance:    prefer-ip-address: true    instance-id: config-client-9999    client:      fetch-registry: true      register-with-eureka: true      service-url:        defaultZone: http://localhost:8010/eureka/

测试访问

# 访问eureka-server

# 访问http://localhost:9999/configInfo 可以获取到dev分支下dev环境的配置

到此这篇关于SpringCloud微服务之Config知识总结的文章就介绍到这了,更多相关SpringCloud Config内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

人的一生是奋斗的一生,人们为了取得成功都在不断地努力着,

SpringCloud微服务之Config知识总结

相关文章:

你感兴趣的文章:

标签云: