spring cloud config开发使用

背景

    随着程序项目越来越多,越来越复杂:功能开关、参数配置、第三方服务地址、内部调用、白名单、黑名单等。

    配置修改后实时生效,灰度发布,分环境、分集群管理配置、版本控制、回滚机制。

    在这样的大环境下,传统的通过配置文件、数据库等方式已经越来越无法满足开发人员对配置管理的需求。

1、config server配置

引入config server的依赖

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-config-server</artifactId>
</dependency>

application.yml配置

spring:
  application:
    name: configserver
  cloud:
    config:
      server:
        git:
          uri: https://xxx #配置git仓库地址
          username: xxx #用户名
          password: xxx #密码
server:
  port: 8081

启动类

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

2、config client配置

引入依赖

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-config-client</artifactId>
</dependency>

bootstrap.yml配置

server:
  port: 8080
spring:
  application:
    name: cc
  profiles:
    active: api
  cloud:
    config:
      uri: http://localhost:8081 #配置中心
      label: cc_dev #版本、分支
      profile: api
management:
  security:
    enabled: false

    bootstrap.yml读取配置中心的cc_dev分支中的cc.yml、cc-api.yml。

    Config Server提供的HTTP接口来获取数据:     

curl    http://localhost:8081/{application}/{profile}/{label}

3、git文件

    master(生产分支):cc.yml、cc-api.yml。。

    cc_dev(开发分支):cc.yml、cc-api.yml。

    cc_test(测试分支):cc.yml、cc-api.yml。

    cc_uat(预生产分支):cc.yml、cc-api.yml。

4、持续集成,与Jenkins结合

    Jenkins进行自动化时,对应的git分支和服务器的环境保持一致。

    

环境 dev(开发) test(测试) uat(预生产) prod(生产)
git分支 dev test uat master
服务器

dev1_ip

dev2_ip

dev3_ip

test1_ip

test2_ip

test3_ip

uat_ip prod_ip

    开发、测试环境对应多套环境,如dev1,dev2,dev3,test1,test2,test3。预生产、生产有且仅有一套环境。

    预生产是生产环境的配置的克隆或者小一号环境。经过预生产就可以合并到生产环境中去了。

    注意:

     如果遇到紧急突发需求v2,而uat上已经有了v1。v1对应uat分支,v2对应uat_temp分支。

  • 这时可以将Jenkins中的uat分支修改为uat_temp,先对紧急需求进行测试。
  • uat测试完成后,再将Jenkins的uat_temp分支切回uat分支。

5、多项目共用配置文件

    cc和dd项目共用配置文件。

    cc的配置文件:

server:
  port: 8080
spring:
  application:
    name: cc
  profiles:
    active: api
  cloud:
    config:
      uri: http://localhost:8081 #配置中心
      label: dev #版本、分支
      profile: api
management:
  security:
    enabled: false

    dd的配置文件

server:
  port: 8080
spring:
  application:
    name: dd
  profiles:
    active: api
  cloud:
    config:
      uri: http://localhost:8081 #配置中心
      label: dev #版本、分支
      profile: api
management:
  security:
    enabled: false

    配置中心文件

    其中application.yml、application-env.yml是公共配置。cc开始的为cc项目中的配置。dd开始的为dd项目中的配置。

6、Security 安全

  6.1 config server端

    引入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

    application.yml配置

security:
  basic:
    enabled: true
  user:
    name: admin
    password: admin

6.2 config client

    application.yml配置

spring:
  cloud:
    config:
      uri: http://admin:admin@localhost:8888 #配置中心
      label: prod   #版本、分支
      profile: env

猜你喜欢

转载自blog.csdn.net/xixingzhe2/article/details/84071124