spring cloud config开发使用

背景

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

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

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

一、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);
	}
}

二、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接口来获取数据:     bootstrap

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

三、git文件

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

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

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

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

四、持续集成,与Jenkins结合

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

    

环境 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分支。

五、多项目共用配置文件

    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项目中的配置。

六、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
相关文章
相关标签/搜索