Spring Cloud Config-配置中心

Spring Cloud Config简介

  • Spring Cloud为服务外部化配置提供了服务端和客户端。
  • config-server用于集中式配置服务器,它可以横向扩展,默认使用Git配置内容,后面我将会使用gitblit来管理配置文件。
  • config-client操作config-server中属性,再通过config-server去远程配置中心中中拉去配置。

config-server

  1. 搭建Git仓库,这里采用的是虚拟机搭建gitblit服务。
    1、cd /home/ubuntu/data/ & mkdir gitblit & cd gitblit/
    2、wget http://dl.bintray.com/gitblit/releases/gitblit-1.6.2.tar.gz
    3、tar -zxvf gitblit-1.6.2.tar.gz
    4、vim data/gitblit.properties,其中有几个配置非常重要:
        server.httpPort=9300 设置端口;
        server.httpsPort 设置https协议端口,这个不需要刻意注释掉;
        server.httpBindInterface = 192.168.1.100 服务端的ip地址;
    5、修改gitblit 启动脚本,可以根据自己选用linux操作系统,来修改对应脚本,这里采用的是ubuntu,vim service-ubuntu.sh
        GITBLIT_PATH= /home/ubuntu/data/gitblit/
        GITBLIT_BASE_FLODER= /home/ubuntu/data/gitblit/data/
        GITBLIT_USER= "root"
    6、将gitblit启动脚本复制到/etc/init.d/,开机自启。cp service-ubuntu.sh /etc/init.d/gitblit
    7、此时可以通过192.168.1.100:9300 访问到虚拟机上的git仓库
    8、创建版本库config-center,并在IDEA创建config-center项目,添加dev,prod目录,用于存放开发、生产环境配置文件

     

  2. 使用Spring Initializr初始化Spring Cloud 项目,pom依赖
    
    	org.springframework.cloud
    	spring-cloud-config-server
    

     

  3. application.properties
    server.port=7010
    spring.application.name=config-server
    server.servlet.context-path=/config
    #访问git仓库的用户名
    spring.cloud.config.server.git.username=admin
    #访问git仓库的用户密码
    spring.cloud.config.server.git.password=admin
    # git管理配置
    spring.cloud.config.server.git.uri=http://[email protected]:9300/r/config-center.git
    spring.cloud.config.server.git.searchPaths=dev,prod
    #配置仓库的分支
    spring.cloud.config.label=master
    
    #eureka服务器地址
    eureka.client.service-url.defaultZone=http://eureka:[email protected]:8761/eureka
    
    #指定注册到eureka server实例ID
    eureka.instance.instance-id=${eureka.instance.ip-address}:${server.port}
    #表示将自己的ip注册到eureka server上面
    eureka.instance.prefer-ip-address=true
    #指定eureka client IP地址
    eureka.instance.ip-address=127.0.0.1
  4. ConfigServerApplication
    @SpringBootApplication
    @EnableConfigServer
    public class ConfigServerApplication {
    	public static void main(String[] args) {
    		SpringApplication.run(ConfigServerApplication.class, args);
    	}
    }

config-client

pom


	org.springframework.cloud
	spring-cloud-starter-config


	org.springframework.cloud
	spring-cloud-starter-netflix-eureka-client

application.yml

spring:
  profiles:
    active: dev
  application:
    name: config-client
  cloud:
    config:
#      config-server高可用配置
#      discovery: 
#        enabled: true
#        service-id: config-server
      label: master
      profile: ${spring.profiles.active}
      name: ${spring.application.name}
      uri: http://127.0.0.1:7010/config/

git仓库中dev目录下config-client-dev.properties

server.servlet.context-path=/config-client

config-server高可用

可以将config-server作为eureka客户端,并注册多个实例,config-client启动时,通过service-id随机从某一个config-server中拉取远程仓库配置,即使某一个config-server挂掉,也可从其它config-server中拉取配置。

pom依赖中添加


	org.springframework.cloud
	spring-cloud-starter-netflix-eureka-client

ConfigServerApplication中

@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient //添加eureka 客户端注解
public class ConfigServerApplication {
	public static void main(String[] args) {
		SpringApplication.run(ConfigServerApplication.class, args);
	}
}

config-client端bootstrap.yml中

spring:
  profiles:
    active: dev
  application:
    name: config-client
  cloud:
    config:
#     config-server高可用配置
      discovery: 
        enabled: true
        service-id: config-server
      label: master
      profile: ${spring.profiles.active}
      name: ${spring.application.name}
#     uri: http://127.0.0.1:7010/config/

Spring Cloud bus

  • 方式一:可通过客户端刷新配置,但是这种方式不推荐
  • 方式二:直接通过config server端去刷新所有config client端配置

pom


    org.springframework.cloud
    spring-cloud-starter-bus-amqp


    org.springframework.cloud
    spring-cloud-config-monitor

config-server properties配置

#rabbitmq配置
spring.rabbitmq.host=192.168.1.100
spring.rabbitmq.port= 5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

#spring cloud bus 配置
spring.cloud.bus.refresh.enabled=true
#暴露bus-refresh节点,通过此节点刷新配置
management.endpoints.web.exposure.include=*

config-client pom引入


    org.springframework.cloud
    spring-cloud-starter-bus-amqp

config-client properties配置

#rabbitmq配置
spring.rabbitmq.host=192.168.1.100
spring.rabbitmq.port= 5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

可用postman工具发送post请求, http://127.0.0.1:7010/actuator/bus-refresh,config客户端信息先从eureka server注销掉,然后通过配置中心重新加载注册。

DiscoveryClient_CONFIG-CLIENT/127.0.0.1:8081: registering service...
Received remote refresh request. Keys refreshed []
Attempting to connect to: [192.168.1.100:5672]
Created new connection: rabbitConnectionFactory.publisher#15b0c66a:0/SimpleConnection@4009d18d [delegate=amqp://[email protected]:5672/, localPort= 50620]
Auto-declaring a non-durable, auto-delete, or exclusive Queue (springCloudBus.anonymous.qGousCjESOGlUTRdrEgmUA) durable:false, auto-delete:true, exclusive:true. It will be redeclared if the broker stops and is restarted while the connection factory is alive, but all messages will be lost.
DiscoveryClient_CONFIG-CLIENT/127.0.0.1:8081 - registration status: 204

 

你可能感兴趣的:(Spring Cloud Config-配置中心)