Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SpringCloud基础教程(五)-配置中心热生效和高可用

 我的博客: 兰陵笑笑生,欢迎浏览博客!

 上一章 SpringCloud基础教程(四)-配置中心入门当中,我们在对Eureka的有了基本的基础认识之上,深入的了解Eureka高可用集群和其他的生产环境中用到的一些配置。本章将开始了解分布式环境下的配置中心。

前言

 在实际的项目运行中,我们会根据实际需求修改配置内容,那么有没有一种方式,能够在不启动服务组件的情况向让配置文件动态的生效呢,Spring Cloud Conifg中提供了一种方式了。当然我们还需要考虑一旦配置服务宕机的话,那么配置客户端是无法获取到配置信息的,所以针对配置服务,我们也希望能提供高可用的服务。

一、配置热生效

 首先我们在客户端(每个开发的微服务)的控制器上添加 @RefreshScope 注解,并在客户端(项目的pom.xml中引入actuator依赖(actuator中包含了/actuator/refresh的api):

  @RefreshScope
@RestController
public class ValueController {

    @Value("${k1}")
     String value;

    @GetMapping("/get")
    public String getValue(){
        return value;
    }
}复制代码

亲自测试,如果将@RefreshScope注解添加在启动类上,value是不管用的,只有添加在需要热加载的Bean上才生效。

      
            org.springframework.boot
            spring-boot-starter-actuator
    复制代码

 修改bootstrap.xml 添加如下配置,表示可以通过手动的刷新改变获取配置信息

  management:
  endpoints:
    enabled-by-default: true
    web:
      exposure:
        include: refresh复制代码

 在配置完成之后,我们通过以下的步骤测试热生效:

步骤一:启动Config Server和Config Client项目访问。

步骤二:访问http://localhost:9003/get 返回的结果是 “master-test-v1 ”;

步骤三:修改配置文件ConfigServer-test.properties 内容修改成”k1=master-test-v2“;提交到git仓库

步骤四:访问http://localhost:9003/get 返回的结果依旧是 “master-test-v1 ”;

步骤五:通过POST访问http://localhost:g003/actuator/refresh 访问得到响应【“k1”】, 注意这里的版本是2.x系列的springCloud,和1.X系列的接口访问不一样):

第六步:再次去访问http://localhost:9003/get 返回的结果变成了是 “master-test-v2 ”;

 通过以上的配置,我们成功的测试了手动的请求可以刷新并动态的加载最新的配置,当然这样还是存在一个缺点,就是需要手动刷新。当然SpringCloud中介绍了使用Bus来通知Spring Cloud Config,后期的文章还需继续生如介绍。

二、高可用

 在上文中,我们的配置的客户端都是通过制定配置Config Server的实例地址方式获取配置信息的,一旦Config Server遇到故障,获取配置信息就会出现故障,SpringCloud同样支持通过高可用的方式解决这样的问题。接下来我们结合Eureka组件,来搭建高可用的配置中心。

2.1 Config Server配置

 在Config Server的项目中添加Eureka的依赖:

         
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        复制代码

 并在ConfigServerApplicaition.Java启动类添加注解@EnableEurekaClient 开启Eureka注册:

  import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigServerApplicaition {
    
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplicaition.class, args);
    }
}
复制代码

 修改Config Server的applicaiton.xml配置:修改端口号和实例Id,启动2个Config Server实例:

  server:
  port: 6001
#服务提供方
spring:
  application:
  #服务的名称
    name: server-config-cluster
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/lnxxs/springCloudConfig.git
          password:
          username:
          #git非植
      label: master
      uri: http://localhost:6001/
      enabled: true
      #http安全
  security:
    user:
      name: user
      password: pwd
      #注册的实实例
eureka:
  instance:
    instance-id: config-server1
  client:
    service-url:
    #连接eureka的url
      defaultZone:  http://eureka7001.com:7001/eureka/复制代码

 在Eureka的监控中心,我们看到了名称为server-config-cluster的实例有2个,分别是config-server1和

config-server2:

2.2、客户端配置

 客户端需要从Eureka获取Config Server的地址,首先在客户端的项目pom.xml中添加Eureka的依赖:

          
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        复制代码

 启动类添加@EnableDiscoveryClient注解,启动开发服务发现的功能,发现Config Server配置服务信息:

  import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableAutoConfiguration
@EnableDiscoveryClient
public class ConfigClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class, args);
    }
}复制代码

 同时在bootstrap.xml洗澡呢Eureka注册中心的地址配置,同时将spring.cloud.config.uri去掉,打开spring.cloud.config.discover.enabled=ture ,并添加spring.cloud.config.service-id为配置服务的spring.application.name= server-config-cluster,具体配置如下:

  spring:
  application:
    name: server-client
  cloud:
    config:
        label: master
        profile: test
        #修改uri为 discovery.service-id
        #uri: http://localhost:6001/
        name: ConfigServer
        fail-fast: true
        retry:
          initial-interval: 1000
          max-attempts: 6
          max-interval: 2000
          multiplier: 1.1
        username: user
        password: pwd
        discovery:
          service-id: server-config-cluster
          enabled: true
management:
  endpoints:
    enabled-by-default: true
    web:
      exposure:
        include: refresh
eureka:
  instance:
    instance-id: config-client
  client:
    service-url:
    #连接eureka的url
      defaultZone:  http://eureka7001.com:7001/eureka/复制代码

 通过以上的配置,就可以实现配置中心的高可用。

三、总结

 本章是对配置中心做了深入的介绍,包括配置的热加载和配置服务的高可用,这些都是微服务架构所需要的。Spring Cloud Config功能特别的丰富,不止于此,同时还支持更换git仓库为SVN或者其他的仓库等,有感兴趣的同学可以自行研究。

.以就是本期的分享,你还可以关注公众号: 程序员笑笑生,关注更多精彩内容!

SpringCloud基础教程(一)-微服务与SpringCloud

SpringCloud基础教程(二)-服务发现 Eureka

SpringCloud基础教程(三)-Eureka进阶

SpringCloud 基础教程(四)-配置中心入门

SpringCloud基础教程(五)-配置中心热生效和高可用

SpringCloud 基础教程(六)-负载均衡Ribbon

更多精彩内容,请期待...

本文由博客一文多发平台 OpenWrite 发布!



This post first appeared on IT瘾 | IT社区推荐资讯, please read the originial post: here

Share the post

SpringCloud基础教程(五)-配置中心热生效和高可用

×

Subscribe to It瘾 | It社区推荐资讯

Get updates delivered right to your inbox!

Thank you for your subscription

×