Spring Cloud Config + Spring Cloud Bus + RabbitMq实现微服务高可用配置中心

        在一个微服务架构中会有很多微服务,每个微服务都有自己的配置文件,这会随着微服务的数量增多而变的难以管理。所以spring cloud 给我们提供了一个很强大的配置管理中心,spring cloud config,他可以将我们的配置文件集中于一处进行集中管理。并且可以通过spring cloud bus + rabbitMQ 进行广播,同步到所有节点,并且直接生效。

 

注意几点问题:

1,可以单独使用spring cloud config进行配置管理,但是无法实现广播推送到多节点,那么就感觉意义不大了;想要实现广播推送,那么应该还是需要使用spring cloud bus+rabbitMQ的。

2,rabbitMQ安装回头单独写一个吧,这里说一下我遇到的一个使用的问题,默认账户密码是guest/guest,并且这个账户的权限不足外部登录访问。需要操作设置,不然会启动项目时会出现socketException。 无法登录。

3,我用的spring boot版本较高,和spring boot1的很多依赖名称都改变了(比如feign->openfeign等等),部分其他博主的教程不太可行。这里使用时你们可以注意一下版本号。

 

建设配置文件的git仓库

学习阶段,我一般使用的是github,接下来我写一下我建立的git配置文件仓库。

登录github,点击右上角“+”号,new repository

Spring Cloud Config + Spring Cloud Bus + RabbitMq实现微服务高可用配置中心_第1张图片

因为这个仓库我已经存在,所以爆红,换个名称即可。最后点击create repository。新建仓库。然后进入仓库

Spring Cloud Config + Spring Cloud Bus + RabbitMq实现微服务高可用配置中心_第2张图片

进入新建文件页面。

Spring Cloud Config + Spring Cloud Bus + RabbitMq实现微服务高可用配置中心_第3张图片

这里注意的是如果你想将文件创建在一个文件夹下,再标了1的位置先写上你的文件夹名,再用/隔开即可。类似再IDEA下新建一个类的操作。在2处可以编写你的配置文件内容。最后commit。

这里我已经新建好了一个yml,以此为例。

Spring Cloud Config + Spring Cloud Bus + RabbitMq实现微服务高可用配置中心_第4张图片

 

搭建spring cloud config server端

我们使用sts自带的插件spring starter project新建一个spring boot 项目

Spring Cloud Config + Spring Cloud Bus + RabbitMq实现微服务高可用配置中心_第5张图片

然后选择依赖,这里我直接贴上我的POM,上面有注释。



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.1.2.RELEASE
		 
	
	com.travelsky.config
	travelsky-config
	0.0.1-SNAPSHOT
	travelsky-config
	Demo project for Spring Boot

	
		1.8
		Greenwich.RC2
	

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

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

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	

	
		
			
				org.springframework.cloud
				spring-cloud-dependencies
				${spring-cloud.version}
				pom
				import
			
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	

	
		
			spring-milestones
			Spring Milestones
			https://repo.spring.io/milestone
		
	


然后我们要写一下config的配置文件

server:
  port: 8084
spring:
  application:
    name: travelsky-config-server
  cloud:
    config:
      server:
        git:   
          uri:      # 配置git仓库的地址
          search-paths: #git仓库地址下的相对地址,可以配置多个,用,分割。
          username: # git仓库的账号
          password: # git仓库的密码
          
  rabbitmq:   #rabbitMQ配置
    host: localhost 
    port: 5672
    username: admin
    password: 123456
eureka:
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:8081/eureka/    ## 注册中心eurka地址

#关闭安全验证,和之前的校验写法不一样了,这里要注意。
management: 
    endpoints: 
        web: 
            exposure: 
                include: "*"
      

接下来最后一步,在启动类加上对spring cloud config的支持注解,和eureka客户端发现注解。

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class TravelskyConfigApplication {

	public static void main(String[] args) {
		SpringApplication.run(TravelskyConfigApplication.class, args);
	}

}

这里我们的项目已经搭建完成了。

 

spring cloud config clinet 

这里我们将之前做的spring cloud + eureka 搭建的生产者拿来改造一下。

为了完整性我就把全部的pom贴出来;里面有注释。



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.1.1.RELEASE
		 
	
	com.travelsky.producer.test
	travelsky-producer-test
	0.0.1-SNAPSHOT
	travelsky-producer-test
	Demo project for Spring Boot

	
		1.8
		Greenwich.RC2
	

	

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

		
		
			org.springframework.boot
			spring-boot-starter-actuator
		

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

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

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	

	
		
			
				org.springframework.cloud
				spring-cloud-dependencies
				${spring-cloud.version}
				pom
				import
			
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	

	
		
			spring-milestones
			Spring Milestones
			https://repo.spring.io/milestone
		
	


然后我们新建一个bootstrap.yml配置文件

server: 
  port: 8050   #服务端口号
spring: 
  application: 
    name: travelsky-producer-test  #服务名称
  rabbitmq: 
    host: localhost
    port: 5672
    username: admin
    password: 123456
  cloud: 
    bus: 
      trace: 
        enabled: true
    config: 
          #配置文件名称application前缀
      name: travelsky-producer-test
            #配置文件分支名
      profile: dev
#            配置到了eureka管理,不直接使用URI了。
#            uri: http://localhost:8086/
      label: master
      discovery:
                #开始config服务发现支持
        enabled: true
                #指向sever端name
        service-id: travelsky-config-server


eureka: 
  client: 
    serviceUrl:  #注册中心的注册地址
      defaultZone: http://127.0.0.1:8081/eureka/ 
  instance: 
    lease-renewal-interval-in-seconds: 1  #每间隔1s,向服务端发送一次心跳,证明自己依然”存活“
    lease-expiration-duration-in-seconds: 2 #告诉服务端,如果我2s之内没有给你发心跳,就代表我“死”了,将我踢出掉。
#关闭安全验证
management: 
    endpoints: 
        web: 
            exposure: 
                include: "*"

这里有一个关于bootstrap.yml和applicatio.yml的关系。就是bootstrap是在application之前被运行的。他是运来加载一些不会变得配置,比如application.name;而当使用spring cloud config时,一般application被用来做一些应用级的配置,可以进行热加载。

这时候我们系统中就可以不适用application.yml了。

我们写一个controller测试一下

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * 生产者暴露接口服务
 * @ClassName: com.travelsky.ProducerTestController
 * @author: wm
 * @date: 2019年1月12日 上午11:14:45
 * @version : 1.0
 */
@RestController
//刷新地址http://localhost:8082/actuator/refresh/
@RefreshScope//自动更新配置文件
public class ProducerTestController {

	@Value("${travelsky.hello}")
	private String travelskyHello;
	
	@GetMapping("/getHello")
	public String getHello() {
		return this.travelskyHello;
	}
	

}

使用value注解从配置文件中取出travelsky.hello的值进行打印。

 

测试

这时候我们按顺序启动以下服务:1,eureka注册中心。2,config配置中心。3,travelsky-producer-test 8050。4,travelsky-producer-test 8051。

然后访问http://localhost:8050/getHello进行测试

此时已经证明我们已经通过config配置中心管理了我们的配置文件。

 

单一节点刷新配置文件

在生产过程中配置文件很可能需要改变,这时候我们就需要使用到spring cloud config 的refresh功能了。他可以让我们通过重新请求配置文件,得到最新的配置而无需重启项目。做到更改即生效。

我们 先来修改一下配置文件,再最后加11111111111

Spring Cloud Config + Spring Cloud Bus + RabbitMq实现微服务高可用配置中心_第6张图片

这时候我们再请求http://localhost:8050/getHello结果并没有改变。

Spring Cloud Config + Spring Cloud Bus + RabbitMq实现微服务高可用配置中心_第7张图片

接下来我们发送一个post请求

http://localhost:8050/actuator/refresh

再次请求http://localhost:8050/getHello,会发现结果已经是最新的了。

这就大大的方便了我们的使用,无需重启系统。

但这时候我们请求http://localhost:8051/getHello结果却没有改变。多节点情况该如何处理呢?

 

多节点刷新

 

真正的生产环境,肯定是要高可用性的,所以一般服务我们都不可能只部署一个节点。那么当配置文件更新时需要多节点同步更新。所以我们需用使用spring cloud bus 进行广播推送。其实很简单。

1,通过client端进行同步刷新

我们将上面的post请求变更一下,http://localhost:8050/actuator/bus-refresh。

当请求结束后,我们请求8051,就会发现8050和8051被同步更新了。

2,通过server端进行广播推送

我们先将配置文件改成33333333

Spring Cloud Config + Spring Cloud Bus + RabbitMq实现微服务高可用配置中心_第8张图片

这时候我们请求config端的POST请求:http://localhost:8084/actuator/bus-refresh

同样的,8050和8051节点都得到了更新。

Spring Cloud Config + Spring Cloud Bus + RabbitMq实现微服务高可用配置中心_第9张图片

 

spring cloud config其实还是有他的缺点的,比如没有可视化界面,这一点好像携程的开源框架apollo做的还不错。回头单独再开一个了解一下apollo。

你可能感兴趣的:(spring,java,分布式,spring,cloud)