Spring Cloud Config高可用分布式配置中心

  • 简介

    Spring Cloud Config provides server and client-side support for externalized configuration in a distributed system. With the Config Server you have a central place to manage external properties for applications across all environments. The concepts on both client and server map identically to the Spring Environment and PropertySource abstractions, so they fit very well with Spring applications, but can be used with any application running in any language. As an application moves through the deployment pipeline from dev to test and into production you can manage the configuration between those environments and be certain that applications have everything they need to run when they migrate. The default implementation of the server storage backend uses git so it easily supports labelled versions of configuration environments, as well as being accessible to a wide range of tooling for managing the content. It is easy to add alternative implementations and plug them in with Spring configuration.

    Spring Cloud Config为分布式系统提供了服务&客户端用于支持外部配置。您可以在所有环境中管理应用程序的外部属性。客户端和服务器上的概念映射与Spring EnvironmentPropertySource抽象相同,因此它们与Spring应用程序非常契合,但可以与任何以任何语言运行的应用程序一起使用。随着应用程序通过从开发人员到测试和生产的部署流程,您可以管理这些环境之间的配置,并确定应用程序具有迁移时需要运行的一切。服务器存储后端的默认实现使用git,因此它轻松支持标签版本的配置环境,以及可以访问用于管理内容的各种工具。很容易添加替代实现,并使用Spring配置将其插入。

  • Config Server

maven依赖

 

                <dependency><!-- 用于监控-->
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency><!-- 高可用配置中心-->
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</artifactId>
		</dependency>
		<dependency><!-- eureka 客户端,用于注册-->
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
                <dependency><!-- svn依赖,使用svn作为外部配置服务器-->
			<groupId>org.tmatesoft.svnkit</groupId>
			<artifactId>svnkit</artifactId>
		</dependency>
		<dependency><!-- 安全管理,客户端访问需使用账号及密码-->
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-security</artifactId>
		</dependency>

  • 配置
application.yml
spring: 
  application:
    name: config_server
  cloud: 
    config:
    #使用默认值,配置值无效
      overrideNone: true
      enabled: true
      server: 
        svn:
          uri: svn://120.24.76.47/springcloud
          username: tzhuang
          password: 1314
        default-label: sysconfig
        overrides:
          myname: "{cipher}AQAw3rTMdBbtgvE2CKB09BtEPyGHthYXCEBwqyiAGMWkYwWTqXn3xKu0bTKXYYcd+VyI6w57XAecTyfL0BJPf+eOwk+AvVDE1ax4fhjrTmCHm3Qavj7uDF7wF8ciCDWLka+RPZyg16vXUqsRbMzSL3tbLuFu+J6k0essJCtDtYX0VnZveCfl/Sn+bh2M4A4BYcVNE49RBEhhU4JKb2BCth90ragHArcoT6MAfqhGMXodgW4VW2d7g+ETGTiUNVkpnv/zKxa+/H3lzWQ9LqTSPTc4gq1Gag/lDvlaZ8rubhAEsZyxEI7YnSihDy7rcEoAcOcrjsRjK/outSIAuubqgT6irVuBeodhCHwHXTBf3RwyrFklBWyHNubGxKC8yo3dbbA="
        #到客户端前不解密
#        encrypt:
#          enabled: false
  #svn需加入下面配置
  profiles:
    active: subversion
#logging: 
#  levels:  
#    org.springframework.boot.env.PropertySourcesLoader: TRACE
#    org.springframework.cloud.config.server: DEBUG

server: 
  port: 2221

eureka:
  instance: 
#    instance-id: 192.168.1.1:${server.port}
#   使用ip(默认使用内网)
    #会覆盖hostname
    instance-id: xxx.xx.xx.xx:${server.port}
    prefer-ip-address: true
    ip-address: xx.xx.xx.xx
    #解决注册中心调用内网服务器名称(或内网ip)的问题
    hostname: xx.xx.xx.xx
  client: 
    serviceUrl: 
     defaultZone: http://xxxx/eureka/,http://xxxx/eureka/
   
    
    
  
management: 
  security:
    enabled: false
#jdk提供的非对称加密,若加在这儿,只能通过url加密、解密,无法解密远程配置文件#encrypt:# key-store:# location: classpath:/config_server_03.jks# password: 123# alias: tzhkey# secret: 13232#jks文件生成#keytool -genkeypair -alias tzhkey -keyalg RSA -dname "CN=Web Server,OU=Unit,O=Organization,L=City,S=State,C=US" -keypass 123213-keystore config_server_03.jks -storepass 12312#开放监控端口management: security: enabled: falsesecurity: basic: enabled: true user: name: username password: 123214
bootstrap.yml
#使用非对称加密,下载JCE到JDK,生成jks文件。
encrypt: #Flag to say that a process should fail if there is an encryption or decryption error true fail-on-error: false key-store: location: classpath:/config_server_03.jks password: 1234 alias: tzhkey secret: 1234545

使用http://localhost:1111/encrypt -d 1314 对1314加密

在配置文件中使用{cipher}xxxx(加密字符串),访问配置文件,加密属性自动解密


  • Config Client

Spring Cloud Config provides server and client-side support for externalized configuration in a distributed system. With the Config Server you have a central place to manage external properties for applications across all environments. The concepts on both client and server map identically to the Spring Environment and PropertySource abstractions, so they fit very well with Spring applications, but can be used with any application running in any language. As an application moves through the deployment pipeline from dev to test and into production you can manage the configuration between those environments and be certain that applications have everything they need to run when they migrate. The default implementation of the server storage backend uses git so it easily supports labelled versions of configuration environments, as well as being accessible to a wide range of tooling for managing the content. It is easy to add alternative implementations and plug them in with Spring configuration.

Spring Cloud Config为分布式系统提供了服务&客户端用于支持外部配置。您可以在所有环境中管理应用程序的外部属性。客户端和服务器上的概念映射与Spring EnvironmentPropertySource抽象相同,因此它们与Spring应用程序非常契合,但可以与任何以任何语言运行的应用程序一起使用。随着应用程序通过从开发人员到测试和生产的部署流程,您可以管理这些环境之间的配置,并确定应用程序具有迁移时需要运行的一切。服务器存储后端的默认实现使用git,因此它轻松支持标签版本的配置环境,以及可以访问用于管理内容的各种工具。很容易添加替代实现,并使用Spring配置将其插入。

使用消息总线刷新配置

pom依赖

                <dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-bus-amqp</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>

配置

server:
  port: 3334

spring: 
  cloud:
    config:
      discovery:
        enabled: true
        service-id: config_server
      username: username
      password: 1314
      name: userinfo
      profile: dev
  rabbitmq:
    host: xxxxx
    port: 39424
#    host: 192.168.1.200
#    port: 5672
    username: username
    password: 1314
   
    
    
  
management: 
  security:
    enabled: false
    
eureka:
  instance:
    prefer-ip-address: true
    hostname: xxx.xxx.xx.xx
    instance-id: xxx.xxx.xxx.xx:${server.port}
    ip-address: xx.x.xx.xx
  client: 
    service-url:
      defaultZone: http://xxx/eureka/,http://xxx/eureka/

使用post请求http://localhost:3334/bus/refresh刷新本地缓存配置

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值