ITPub博客

首页 > IT职业 > IT职场 > 一周学会Spring Cloud(Day4 分布式配置中心及其高可用化

一周学会Spring Cloud(Day4 分布式配置中心及其高可用化

IT职场 作者:wzzhujun 时间:2021-09-09 09:52:58 0 删除 编辑

        Spring Cloud Config是使用配置服务来保存各个服务的配置文件,在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。

一.创建工程

        创建名为config-server的spring boot工程,引入依赖


    org.springframework.cloud
    spring-cloud-config-server
    org.springframework.cloud
    spring-cloud-starter-eureka
    1.1.3.RELEASE
    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-server

图片描述

        在程序的入口Application类加上@EnableConfigServer注解开启配置服务器的功能

图片描述

@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

    public static void main(String[] args) {

        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

需要在程序的配置文件application.yml文件配置如下:

spring:
  cloud:
    config:
      server:
        label: master
      git:
        uri: https://github.com/
        searchPaths: 
#          username: your username
#          password: your password
  application:
    name: config-server
server:
  port: 8888

图片描述

  • spring.cloud.config.server.git.uri:配置git仓库地址

  • spring.cloud.config.server.git.searchPaths:配置仓库路径

  • spring.cloud.config.label:配置仓库的分支

  • spring.cloud.config.server.git.username:访问git仓库的用户名

  • spring.cloud.config.server.git.password:访问git仓库的用户密码

        如果Git仓库为公开仓库,可以不填写用户名和密码,如果是私有仓库需要填写,本例子是公开仓库。远程仓库https://github.com/ 中有个文件config-client-dev.properties文件中有一个属性:

foo = foo version 2

二.启动测试

    启动程序:访问http://localhost:8888/config-client/dev

图片描述


  config-client
  
    dev
  
  
  23a7e79483fcf58c9df3e9276d7e8a984ae5a481
  
  
    
      
        https://github.com/chinamobilehyb/imooc/SpringCloudConfig/config-client-dev.properties
      
      
        foo version 2
        hello spring io
      
      
    

    证明配置服务中心可以从远程程序获取配置信息。

    http请求地址和资源文件映射如下:

    • /{application}/{profile}[/{label}]

    • /{application}-{profile}.yml

    • /{label}/{application}-{profile}.yml

    • /{application}-{profile}.properties

    • /{label}/{application}-{profile}.properties

三.构建config-client工程

        创建名为config-client的工程,引入依赖:


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

图片描述

配置文件bootstrap.properties(此处配置文件一定是这个格式,否则会测试失败)

spring.application.name=config-client
spring.cloud.config.label=master
spring.cloud.config.profile=dev
spring.cloud.config.uri= http://localhost:8888/
server.port=8881
  
#spring.cloud.config.label 指明远程仓库的分支
#spring.cloud.config.profile
#dev开发环境配置文件
#test测试环境
#pro正式环境
#spring.cloud.config.uri= http://localhost:8888/ 指明配置服务中心的网址


图片描述

程序的入口类,写一个API接口“/hi”,返回从配置中心读取的foo变量的值

@SpringBootApplication
@RestController
public class ConfigClientApplication {

    public static void main(String[] args) {

        SpringApplication.run(ConfigClientApplication.class, args);
    }

    @Value("${foo}")
    String foo;

    @RequestMapping(value = "/hi")
    public String hi() {
        return foo;
    }
}

    打开网址访问:http://localhost:8881/hi,网页显示:

foo version 2

      这就说明,config-client从config-server获取了foo的属性,而config-server是从git仓库读取的。

四.高可用化

      我们已经学会了一个服务如何从配置中心读取文件,配置中心如何从远程git读取配置文件,当服务实例很多时,都从配置中心读取文件,这时可以考虑将配置中心做成一个微服务,将其集群化,从而达到高可用。

五.创建eureka_config工程

        引入Eureka的起步依赖spring-cloud-starter-eureka-server

图片描述


    org.springframework.cloud
    spring-cloud-starter-eureka-server
    1.1.3.RELEASE

        在配置文件application.yml上,指定服务端口为8889,加上作为服务注册中心的基本配置

图片描述

server:
  port: 8889

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

        入口类加上注解@@EnableEurekaServer

图片描述

@EnableEurekaServer
@SpringBootApplication
public class EurekaConfigApplication {

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

六.改造config-server

    加上EurekaClient的起步依赖spring-cloud-starter-eureka。配置文件application.yml,指定服务注册地址为http://localhost:8889/eureka/

spring:
  cloud:
    config:
      server:
        label: master
        git:
          uri: https://github.com/chinamobilehyb/imooc
          searchPaths: SpringCloudConfig
#          username: your username
#          password: your password
  application:
    name: config-server
server:
  port: 8888
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8889/eureka/

图片描述

    最后需要在程序的启动类Application加上@EnableEureka的注解

图片描述

七.改造config-client

    加上起步依赖spring-cloud-starter-eureka,配置文件bootstrap.properties,加上服务注册地址为http://localhost:8889/eureka/

图片描述

spring.application.name=config-client
spring.cloud.config.label=master
spring.cloud.config.profile=dev
#spring.cloud.config.uri= http://localhost:8888/

eureka.client.serviceUrl.defaultZone=http://localhost:8889/eureka/
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=config-server
server.port=8881

#spring.cloud.config.discovery.enabled 是从配置中心读取文件。
#spring.cloud.config.discovery.serviceId 配置中心的servieId,即服务名。

依次启动eureka-servr,config-server,config-client 
访问网址:http://localhost:8889/

图片描述

访问http://localhost:8881/hi,浏览器显示:

foo version 2

图片描述

以上便是建立分布式配置中心及其高可用化的具体流程,按部就班应该是不会出错的~感谢大家的阅读~


上一篇:一周学会Spring Cloud(Day3 断路器 Hystrix和路由网关 zuul)

下一篇:一周学会Spring Cloud(Day5 消息总线和链路追踪)

图片描述

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/3370/viewspace-2814007/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-14

  • 博文量
    83
  • 访问量
    733