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

创建文件
 master分支
# config-dev.yml config: info: "config info for dev(master)" # config-test.yml config: info: "config info for test(master)" # config-prod.yml config: info: "config info for prod(master)"
 dev分支
# config-dev.yml config: info: "config info for dev(dev)" # config-test.yml config: info: "config info for test(dev)" # config-prod.yml config: 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
@EnableConfigServer
public class SpringcloudConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringcloudConfigServerApplication.class, args);
    }
}
 application.yml配置文件
server:
  port: 8888
spring:
  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: true
eureka:
  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: 9999
spring:
  application:
    name: config-client
  cloud:
    config:
      # 配置中心地址
      uri: http://localhost:8888
      # 分支名称
      label: master
      # 配置文件名称
      name: config
      # 配置后缀名称
      profile: dev
eureka:
  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
@RefreshScope
public 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: 8888
spring:
  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: 9999
spring:
  application:
    name: config-client
  cloud:
    config:
      # 配置中心地址
      uri: http://localhost:8888
      # 分支名称
      label: master
      # 配置文件名称
      name: config
      # 配置后缀名称
      profile: dev
      # 配置中心用户名
      username: xxx
      # 配置中心密码
      password: xxx
六、集群搭建示例
添加配置
bootstrap.yml
server:
  port: 9999
spring:
  application:
    name: config-client
  cloud:
    config:
      # 分支名称
      label: master
      # 配置文件名称
      name: config
      # 配置后缀名称
      profile: dev
      # 集群绑定
      discovery:
        enabled: true
        service-id: config-server
eureka:
  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环境的配置
	声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
		
评论(0)