返回 导航

SpringBoot / Cloud

hangge.com

分布式配置中心Spring Cloud Config使用详解2(加入到Eureka中)

作者:hangge | 2020-08-10 08:36

二、服务化配置中心(纳入Eureka服务治理体系)

1,高可用配置介绍

    当要将配置中心部署到生产环境中时,与服务注册中心一样,我们也希望它是一个高可用的应用。Spring Cloud Config 实现服务端的高可用非常简单,主要有以下两种方式:
  • 传统模式:不需要为这些服务端做任何额外的配置,只需要遵守一个配置规则,将所有的 Config Server 都指向同一个 Git 仓库,这样所有的配置内容就通过统一的共享文件系统来维护。而客户端在指定 Config Server 位置时,只需要配置 Config Server 上层的负载均衡设备地址即可,就如下图所示的结构。

  • 服务模式:我们也可以将 Config Server 作为个普通的微服务应用,纳入 Eureka 的服务治理体系中。这样我们的微服务应用就可以通过配置中心的服务名来获取配置信息,这种方式比起传统的实现模式来说更加有利于维护,因为对于服务端的负载均衡配置和客户端的配置中心指定都通过服务治理机制一并解决了,既实现了高可用,也实现了自维护。下面通过样例进行演示。

2,服务端配置

(1)首先在前文的配置中心 config-server 基础上进行修改,在其 pom.xml 中增加 spring-cloud-starter-netflix-eureka-client 依赖,以实现将分布式配置中心加入 Eureka 的服务治理体系。
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Hoxton.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>

(2)接着在 application.properties 中配置参数 eureka.client.serviceUrl.defaultZone 以指定服务注册中心的位置:
spring.application.name=config-server
server.port=7001

spring.cloud.config.server.git.uri=https://gitee.com/hangge/hangge.com
spring.cloud.config.server.git.search-paths=config-repo
spring.cloud.config.server.git.username=hangge
spring.cloud.config.server.git.password=123

eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

(3)最后在应用主类中新增 @EnableDiscoveryClient 注解,实现将 config-server 注册到服务注册中心上去:
@EnableDiscoveryClient
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

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

(4)启动应用,访问 http://localhost:1111/,可以在 Eureka Server 的信息面板中看到 config-server 已经被注册了。

3,客户端配置

(1)首先同样编辑客户端 hangge-client pom.xml 文件,增加 spring-cloud-starter-netflix-eureka-client 依赖,以实现客户端发现 config-server 服务:
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Hoxton.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

(2)接着编辑 bootstrap.properties 文件,按如下内容进行配置:
spring.application.name=hangge-client
server.port=7002

#配置服务注册中心
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

#开启通过服务来访问 Config Server 的功能
spring.cloud.config.discovery.enabled=true
#指定 Config Server注册的服务名
spring.cloud.config.discovery.service-id=config-server
#定位 Git 中的资源
spring.cloud.config.profile=dev

(3)然后在应用主类中新增 @EnableDiscoveryClient 注解,用来发现 config-server 服务,利用其来加载应用配置:
@EnableDiscoveryClient
@SpringBootApplication
public class HanggeClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(HanggeClientApplication.class, args);
    }
}

(4)最后我们仍然使用之前创建的 Controller 来加载 Git 中的配置信息(这个不需要修改):
@RefreshScope
@RestController
public class HelloController {
    @Value("${from}")
    private String from;

    @GetMapping("/test")
    public String test(){
        return this.from;
    }
}

(5)启动应用,访问 http://localhost:1111/,可以在 Eureka Server 的信息面板中该应用已经被注册了。

(6)而访问客户端提供的 http://localhost:7002/test 服务接口,可以看到页面成功根据配置内容输出对应环境的 from 内容了。
评论

全部评论(0)

回到顶部