0063-Conifg Server集群搭建

本文介绍如何搭建ConfigServer集群并实现负载均衡,所有微服务提供者作为ConfigClient,从ConfigServer获取配置,通过注册到EurekaServer进行服务发现。

1. 前言

所有的微服务提供者都是Config Client,都需要从Config Server获取配置,所以Config Server的访问压力会很大,需要集群搭建;将所有的Config Server注册到Eureka Server中,所有的Config Client 从Eureka Server获取Config Server的地址做负载均衡。

2. Config Server集群搭建

2.1 pom依赖

添加Eureka Client的依赖

 <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.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <!--向注册中心注册-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
</dependencies>
2.2 yml配置
server:
  port: 9800

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/wenrongyao/springcloudconfig/
          search-paths: shared
          username: # 公共仓库可以不写
          password: # 公共仓库可以不写

###### 向Eureka Server 注册自己
eureka:
  client:
    serviceUrl:
      defaultZone: http://eureka-server-7001:7001/eureka/,http://eureka-server-7002:7002/eureka/,http://eureka-server-7003:7003/eureka/
  instance:
    instance-id: config-server-9800 # 服务名称
    prefer-ip-address: true # 显示ip地址

info: # 点击注册列表未服务出现的信息
  app.name: springcloud
  company.name: www.honor.com
  build.artifactId: @project.artifactId@
  build.version: @project.version@
2.3 主启动类
@SpringBootApplication
@EnableConfigServer
public class ConfigServer9800Application {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServer9800Application.class, args);
    }
}
2.4 测试

依次访问三台机的配置文件都正常,且三台机都成功向Eureka Server注册
http://localhost:9800/release1.0/config-client-dev.yml

http://localhost:9801/release1.0/config-client-dev.yml

http://localhost:9802/release1.0/config-client-dev.yml

3. Config Client搭建

3.1 pom依赖
<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.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <!--向注册中心注册-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
</dependencies>
3.2 bootstrap.yml配置
#####集群版Config Server从Eureka Server获取服务#######
spring:
  application:
    name: config-client
  cloud:
    config:
      label: release1.0
      discovery:
        service-id: config-server
        enabled: true
      fail-fast: true
      profile: dev

eureka:
  client:
    serviceUrl:
      defaultZone: http://eureka-server-7001:7001/eureka/,http://eureka-server-7002:7002/eureka/,http://eureka-server-7003:7003/eureka/
  instance:
    instance-id: config-client-9900 # 服务名称
    prefer-ip-address: true # 显示ip地址

info: # 点击注册列表未服务出现的信息
  app.name: springcloud
  company.name: www.honor.com
  build.artifactId: @project.artifactId@
  build.version: @project.version@
3.3 主启动类

打开EnableEurekaClient获取服务注册地址

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

    // 配置文件注入
    @Value("${foo}")
    private String foo;
    @Value("${label:master}")
    private String label;

    @GetMapping("/getconfig")
    public String getConfig() {
        return "Current label is " + label + ", and the foo content is " + foo + ".";
    }
}
3.4 测试

启动页面日志可以看到获取的Config Server的地址

2020-04-06 15:29:24.250  INFO 20548 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Multiple Config Server Urls found listed.
2020-04-06 15:29:24.250  INFO 20548 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://10.127.127.1:9801/
2020-04-06 15:29:26.830  INFO 20548 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=config-client, profiles=[dev], label=release1.0, version=428cf9453635640ee6c9191a7ef4b384cbbfadcc, state=null

启动成功以后访问
http://localhost:9900/getconfig
可以获取

Current label is release1.0, and the foo content is dev foo version.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值