spring cloud config开发使用

Spring Cloud 配置中心实战
本文介绍如何使用 Spring Cloud Config 实现集中化的配置管理,包括配置服务器与客户端的搭建、多环境配置的支持、与 Git 的集成及持续集成流程。同时探讨了多个项目共享配置文件的实践,并介绍了安全性配置。

背景

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

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

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

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分支devtestuatmaster
服务器

dev1_ip

dev2_ip

dev3_ip

test1_ip

test2_ip

test3_ip

uat_ipprod_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

    配置中心文件

62018_06_088190b.jpg

    其中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

    

转载于:https://my.oschina.net/u/182501/blog/1551282

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值