zoukankan      html  css  js  c++  java
  • springcloud(八):配置中心服务化和高可用

    在前两篇的介绍中,客户端都是直接调用配置中心的server端来获取配置文件信息。这样就存在了一个问题,客户端和服务端的耦合性太高,如果server端要做集群,客户端只能通过原始的方式来路由,server端改变IP地址的时候,客户端也需要修改配置,不符合springcloud服务治理的理念。springcloud提供了这样的解决方案,我们只需要将server端当做一个服务注册到eureka中,client端去eureka中去获取配置中心server端的服务既可。

    这篇文章我们基于配置中心git版本的内容来改造

    server端改造

    1、添加依赖

     1 <dependencies>
     2     <dependency>
     3         <groupId>org.springframework.cloud</groupId>
     4         <artifactId>spring-cloud-config-server</artifactId>
     5     </dependency>
     6     <dependency>
     7         <groupId>org.springframework.cloud</groupId>
     8         <artifactId>spring-cloud-starter-eureka</artifactId>
     9     </dependency>
    10 </dependencies>

    需要多引入spring-cloud-starter-eureka包,来添加对eureka的支持。

    2、配置文件

     1 server:
     2 server:
     3   port: 8001
     4 spring:
     5   application:
     6     name: spring-cloud-config-server
     7   cloud:
     8     config:
     9       server:
    10         git:
    11           uri: https://github.com/ityouknow/spring-cloud-starter/     # 配置git仓库的地址
    12           search-paths: config-repo                             # git仓库地址下的相对地址,可以配置多个,用,分割。
    13           username: username                                        # git仓库的账号
    14           password: password                                    # git仓库的密码
    15 eureka:
    16   client:
    17     serviceUrl:
    18       defaultZone: http://localhost:8000/eureka/   ## 注册中心eurka地址

    增加了eureka注册中心的配置

    3、启动类

    启动类添加@EnableDiscoveryClient激活对配置中心的支持

    1 @EnableDiscoveryClient
    2 @EnableConfigServer
    3 @SpringBootApplication
    4 public class ConfigServerApplication {
    5 
    6     public static void main(String[] args) {
    7         SpringApplication.run(ConfigServerApplication.class, args);
    8     }
    9 }

    这样server端的改造就完成了。先启动eureka注册中心,在启动server端,在浏览器中访问:http://localhost:8000/就会看到server端已经注册了到注册中心了。

    按照上篇的测试步骤对server端进行测试服务正常。

    客户端改造

    1、添加依赖

     1 <dependencies>
     2     <dependency>
     3         <groupId>org.springframework.cloud</groupId>
     4         <artifactId>spring-cloud-starter-config</artifactId>
     5     </dependency>
     6     <dependency>
     7         <groupId>org.springframework.boot</groupId>
     8         <artifactId>spring-boot-starter-web</artifactId>
     9     </dependency>
    10     <dependency>
    11         <groupId>org.springframework.cloud</groupId>
    12         <artifactId>spring-cloud-starter-eureka</artifactId>
    13     </dependency>
    14     <dependency>
    15         <groupId>org.springframework.boot</groupId>
    16         <artifactId>spring-boot-starter-test</artifactId>
    17         <scope>test</scope>
    18     </dependency>
    19 </dependencies>

    需要多引入spring-cloud-starter-eureka包,来添加对eureka的支持。

    2、配置文件

     1 spring.application.name=spring-cloud-config-client
     2 server.port=8002
     3 
     4 spring.cloud.config.name=neo-config
     5 spring.cloud.config.profile=dev
     6 spring.cloud.config.label=master
     7 spring.cloud.config.discovery.enabled=true
     8 spring.cloud.config.discovery.serviceId=spring-cloud-config-server
     9 
    10 eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

    主要是去掉了spring.cloud.config.uri直接指向server端地址的配置,增加了最后的三个配置:

    • spring.cloud.config.discovery.enabled :开启Config服务发现支持
    • spring.cloud.config.discovery.serviceId :指定server端的name,也就是server端spring.application.name的值
    • eureka.client.serviceUrl.defaultZone :指向配置中心的地址

    这三个配置文件都需要放到bootstrap.properties的配置中

    3、启动类

    启动类添加@EnableDiscoveryClient激活对配置中心的支持

    1 @EnableDiscoveryClient
    2 @SpringBootApplication
    3 public class ConfigClientApplication {
    4 
    5     public static void main(String[] args) {
    6         SpringApplication.run(ConfigClientApplication.class, args);
    7     }
    8 }

    启动client端,在浏览器中访问:http://localhost:8000/ 就会看到server端和client端都已经注册了到注册中心了。

    高可用

    为了模拟生产集群环境,我们改动server端的端口为8003,再启动一个server端来做服务的负载,提供高可用的server端支持。

    如上图就可发现会有两个server端同时提供配置中心的服务,防止某一台down掉之后影响整个系统的使用。

    我们先单独测试服务端,分别访问:http://localhost:8001/neo-config/devhttp://localhost:8003/neo-config/dev返回信息:

     1 {
     2     "name": "neo-config", 
     3     "profiles": [
     4         "dev"
     5     ], 
     6     "label": null, 
     7     "version": null, 
     8     "state": null, 
     9     "propertySources": [
    10         {
    11             "name": "https://github.com/ityouknow/spring-cloud-starter/config-repo/neo-config-dev.properties", 
    12             "source": {
    13                 "neo.hello": "hello im dev"
    14             }
    15         }
    16     ]
    17 }

    说明两个server端都正常读取到了配置信息。

    再次访问:http://localhost:8002/hello,返回:hello im dev update。说明客户端已经读取到了server端的内容,我们随机停掉一台server端的服务,再次访问http://localhost:8002/hello,返回:hello im dev update,说明达到了高可用的目的。

    示例代码

  • 相关阅读:
    BZOJ2219数论之神——BSGS+中国剩余定理+原根与指标+欧拉定理+exgcd
    Luogu 3690 Link Cut Tree
    CF1009F Dominant Indices
    CF600E Lomsat gelral
    bzoj 4303 数列
    CF1114F Please, another Queries on Array?
    CF1114B Yet Another Array Partitioning Task
    bzoj 1858 序列操作
    bzoj 4852 炸弹攻击
    bzoj 3564 信号增幅仪
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/7510995.html
Copyright © 2011-2022 走看看