SpringCloud配置中心(RabbitMQ消息总线终极版)

一、服务端:
svn版本
同样先示例server端的代码,基本步骤一样。
1、添加依赖

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
	<groupId>org.tmatesoft.svnkit</groupId>
	<artifactId>svnkit</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

需要多引入svnkitr包
2、配置文件

application.yml
 server:
  port: 8008

spring: 
  application:
    name: t-config-server
  profiles:
    active: subversion
   #消息中间件
  rabbitmq:
    host: localhost
    port: 5672
    username: admin
    password: admin 
  cloud:
    config:
      server:
        svn:
          uri: https://tsc:8443/svn/t-config-server/test
          username: tangsc
          password: tangsc
        default-label: trunk
management:
  security:
     enabled: false 
bootstrap.yml  注册到Eureka
eureka:
  instance:
    non-secure-port: ${server.port:8008}
  client:
    service-url:
      defaultZone: http://peer1:8761/eureka/,http://peer2:8762/eureka/

和git版本稍有区别,需要显示声明subversion.
在svn上新建neo-config-dev.properties文件,内容:neo.hello=hello im dev
3、启动类
启动类没有变化,添加@EnableConfigServer激活对配置中心的支持

@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

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

4、测试
服务端测试
访问:http://localhost:8008/neo-config-dev.properties,返回:neo.hello: hello im dev,说明服务端可以正常读取到svn代码库中的配置信息。修改配置文件neo-config-dev.properties中配置信息为:neo.hello=hello im dev update,再次在浏览器访问http://localhost:8008/neo-config-dev.properties,返回:neo.hello: hello im dev update。说明server端会自动读取最新提交的内容

二、客户端
主要展示如何在业务项目中去获取server端的配置信息
1、添加依赖

<!-- 配置中心客户端 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>
<!-- 监控更新 -->
	<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>

引入spring-boot-starter-web包方便web测试
2、配置文件
需要配置两个配置文件,application.properties和bootstrap.properties

application.yml如下:

server:
  port: 8877

spring: 
  application:
    name: t-common
   #消息中间件
  rabbitmq:
    host: localhost
    port: 5672
    username: admin
    password: admin
#关闭安全验证
management:
  security:
     enabled: false 
#监控 
feign.hystrix.enabled: true 

bootstrap.yml如下:

#配置中心
spring:
  cloud:
    bus:
      trace:
        enabled: true #开启消息跟踪
    config:
      name: neo-config
      profile: dev
      label: trunk
      discovery:
        enabled: true
        service-id: t-config-server
     
#注册中心        
eureka:
  instance:
    non-secure-port: ${server.port:8877}
  client:
    service-url:
      defaultZone: http://peer1:8761/eureka/,http://peer2:8762/eureka/

spring.application.name:对应{application}部分
spring.cloud.config.profile:对应{profile}部分
spring.cloud.config.label:对应git的分支。如果配置中心使用的是本地存储,则该参数无用
spring.cloud.config.uri:配置中心的具体地址
spring.cloud.config.discovery.service-id:指定配置中心的service-id,便于扩展为高可用配置集群。
特别注意:上面这些与spring-cloud相关的属性必须配置在bootstrap.properties中,config部分内容才能被正确加载。因为config的相关配置会先于application.properties,而bootstrap.properties的加载也是先于application.properties。
3、启动类
启动类添加@EnableConfigServer,激活对配置中心的支持

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

启动类只需要@SpringBootApplication注解就可以
4、web测试
使用@Value注解来获取server端参数的值

@RestController
class HelloController {
    @Value("${neo.hello}")
    private String hello;

    @RequestMapping("/hello")
    public String from() {
        return this.hello;
    }
}

启动项目后访问:http://localhost:8002/hello,返回:hello im dev update说明已经正确的从server端获取到了参数。到此一个完整的服务端提供配置服务,客户端获取配置参数的例子就完成了。

自动更新测试:
修改svn上的配置文件内容,然后命令行执行:
curl -X POST http://localhost:8008/bus/refresh 模拟一个post请求访问config-server,通知丫自动更新,然后再分别访问客户端hello查看自动更新效果。
局部刷新
某些场景下(例如灰度发布),我们可能只想刷新部分微服务的配置,此时可通过/bus/refresh端点的destination参数来定位要刷新的应用程序。
例如:/bus/refresh?destination=customers:8000,这样消息总线上的微服务实例就会根据destination参数的值来判断是否需要要刷新。其中,customers:8000指的是各个微服务的ApplicationContext ID。
destination参数也可以用来定位特定的微服务。例如:/bus/refresh?destination=customers:**,这样就可以触发customers微服务所有实例的配置刷新。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值