1.概述

业务微服务化以后,我们要求服务高可用,于是我们可以部署多个相同的服务实例,并引入负载均衡机制。而微服务注册中心作为微服务化系统的重要单元,其高可用也是非常必要的,因此在生产中我们可能需要多个微服务注册中心实例来保证服务注册中心的稳定性。而在实际使用中可能会实现动态添加Eureka服务端。

2.Springcloud Config实现动态扩容Eureka

2.1 原理图

在这里插入图片描述

2.2 代码实现

2.2.1 创建工程

在这里插入图片描述

2.2.2 eureka-expansion

2.2.2.1 添加依赖
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.9.RELEASE</version>
</parent>
<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>
<!-- 版本信息统一管理-->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Finchley.SR3</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

2.2.3 配置中心(config-server)

2.2.3.1 添加依赖
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
</dependencies>
2.2.3.2 启动类
@SpringBootApplication
//开启分布式配置中心服务器端
@EnableConfigServer
public class ConfigApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigApplication.class,args);
    }
}
2.2.3.3 配置文件
spring:
  application:
    ####注册中心应用名称
    name: config-server
  cloud:
    config:
      server:
        git:
          ###git环境地址
          uri: https://gitee.com/itmyx/testconfig.git
          ####搜索目录
          search-paths:
            - eureka
      ####读取分支
      label: master
####端口号
server:
  port: 8200

2.2.4 eureka服务端(eureka-server)

2.2.4.1 添加依赖
<dependencies>
    <!--SpringCloud eureka-server里面包含web相关依赖 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>
2.2.4.2 启动类
@SpringBootApplication
@EnableEurekaServer
@RestController
public class EurekaSimpleApplication {
    public static void main(String[] args) {
        Scanner scanner =new Scanner(System.in);
        new SpringApplicationBuilder(EurekaSimpleApplication.class).profiles(scanner.nextLine()).run(args);
    }
    @Autowired
    EurekaClientConfigBean eurekaClientConfigBean;
    @GetMapping("/eureka-service-info")
    public Object getEurekaServerUrl(){
        return eurekaClientConfigBean.getServiceUrl();
    }
}
2.2.4.3 配置文件(bootstrap.yml)
spring:
  application:
    name: eureka-server
  cloud:
    config:
      uri: http://localhost:8200

2.2.5 eureka客户端(eureka-client)

2.2.5.1 添加依赖
<dependencies>
    <!-- SpringBoot整合Web组件 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- SpringBoot整合eureka客户端 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-client</artifactId>
    </dependency>
</dependencies>
2.2.5.2 启动类
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class EurekaClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaClientApplication.class,args);
    }
    @Autowired
    EurekaClientConfigBean eurekaClientConfigBean;

    @GetMapping("/eureka-service-info")
    public Object getEurekaServerUrl(){
        return eurekaClientConfigBean.getServiceUrl();
    }
}
2.2.5.3 配置文件(bootstrap.yml)
spring:
  cloud:
    config:
      uri: http://localhost:8200
  application:
    name: eureka-client
  profiles:
    active: peer1
server:
  port: 8300

management:
  endpoints:
    web:
      exposure:
        include: "*"

2.2.6 git仓库

[外链图片转存失败(img-GKabyJQo-1563885791208)(https://note.youdao.com/yws/res/28044/0CFA55BDFD8E492091FE405FED96CB9D)]

  • eureka-client-peer1.yml
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8100/eureka
  • eureka-server-peer1.yml
###服务端口号
server:
  port: 8100
###eureka 基本信息配置
eureka:
  instance:
    ###注册到eurekaip地址
    hostname: 127.0.0.1
  client:
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
    ###因为自己是为注册中心,不需要自己注册自己
    register-with-eureka: false
    ###因为自己是为注册中心,不需要检索服务
    fetch-registry: false

management:
  endpoints:
    web:
      exposure:
        include: "*"
  • eureka-server-peer2.yml
###服务端口号
server:
  port: 8400
###eureka 基本信息配置
eureka:
  instance:
    ###注册到eurekaip地址
    hostname: 127.0.0.1
  client:
    serviceUrl:
      defaultZone: http://localhost:8100/eureka/
    ###因为自己是为注册中心,不需要自己注册自己
    register-with-eureka: false
    ###因为自己是为注册中心,不需要检索服务
    fetch-registry: false

2.2.7 动态扩容演示

2.2.7.1 启动服务

分别启动config-server、eureka-server(输入peer1)、eureka-client
在这里插入图片描述

2.2.7.2 在启动eureka-server(输入peer2)

在这里插入图片描述

2.2.7.3 修改配置文件
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8100/eureka,http://localhost:8400/eureka
2.2.7.4 调用接口
post  127.0.0.1:8300/actuator/refresh
post  127.0.0.1:8100/actuator/refresh

[外链图片转存失败(img-6YKzDMVQ-1563885791223)(https://note.youdao.com/yws/res/28073/9BD017A4FB28498B9176965DC34CC1D6)]

2.2.7.5 模拟故障

停止8100注册中心
[外链图片转存失败(img-ZPjbHp5D-1563885791226)(https://note.youdao.com/yws/res/28081/585671911961465EB37BE668DE472AD7)]
[外链图片转存失败(img-wwdZGD1x-1563885791229)(https://note.youdao.com/yws/res/28079/6E0A7F3DACE84CAE84FB739229889C6E)]

3.apollo实现eureka动态扩容

3.1 原理图

[外链图片转存失败(img-pa1vTWIh-1563885791231)(https://note.youdao.com/yws/res/28089/EBDE235409DC48AEB1E0B53F4117FF07)]

3.2 代码实现

3.2.1 Apollo配置中心搭建

3.2.2 注册中心服务(eureka-server)

3.2.2.1 添加依赖
<dependencies>
    <!--SpringCloud eureka-server里面包含web相关依赖 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>com.ctrip.framework.apollo</groupId>
        <artifactId>apollo-client</artifactId>
        <version>1.1.0</version>
    </dependency>
    <!--通用工具包-->
    <dependency>
        <groupId>cn.hutool</groupId>
        <artifactId>hutool-all</artifactId>
        <version>${hutool.version}</version>
    </dependency>
</dependencies>
3.2.2.2 启动类
@SpringBootApplication
@EnableEurekaServer
@RestController
public class EurekaSimpleApplication {
    public static void main(String[] args) {
        Scanner scanner =new Scanner(System.in);
        new SpringApplicationBuilder(EurekaSimpleApplication.class).properties("server.port="+scanner.nextLine()).run(args);
    }
}
3.2.2.3 配置文件(bootstrap.yml)
app:
  id: eureka-server
apollo:
  meta: http://11.11.113.83:8080
  bootstrap:
    enabled: true
    eagerLoad:
      enabled: true

3.2.3 注册中心客户端(eureka-client)

3.2.3.1 添加依赖
<dependencies>
    <!--SpringCloud eureka-server里面包含web相关依赖 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>com.ctrip.framework.apollo</groupId>
        <artifactId>apollo-client</artifactId>
        <version>1.1.0</version>
    </dependency>
    <!--通用工具包-->
    <dependency>
        <groupId>cn.hutool</groupId>
        <artifactId>hutool-all</artifactId>
        <version>${hutool.version}</version>
    </dependency>
</dependencies>
3.2.3.2 启动类
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class EurekaClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaClientApplication.class,args);
    }

    @Autowired
    EurekaClientConfigBean eurekaClientConfigBean;

    @GetMapping("/eureka-service-info")
    public Object getEurekaServerUrl(){
        return eurekaClientConfigBean.getServiceUrl();
    }
}
3.2.3.3 监听类
@Component
public class EurekaRefresher  {
    @Autowired
    EurekaClientConfigBean eurekaClientConfigBean;

    @ApolloConfigChangeListener
    public void onChange(ConfigChangeEvent changeEvent) {
        if(changeEvent.isChanged("eureka.client.service-url.defaultZone")){
            //自行修改配置文件方式
            HttpUtil.createPost("http://localhost:8300/actuator/refresh").body("{}").execute();
            System.out.println("eureka重新加载成功");
        }
    }
}
3.2.3.4 配置文件(bootstrap.yml)
app:
  id: eureka-client
apollo:
  meta: http://11.11.113.83:8080
  bootstrap:
    enabled: true
    eagerLoad:
      enabled: true

3.2.4 配置文件

  • eureka-client
    [外链图片转存失败(img-TyIZsSoE-1563885791232)(https://note.youdao.com/yws/res/28114/070AD10319334AFE93B81FA01AD1D013)]
  • eureka-server
    [外链图片转存失败(img-azwpvPa2-1563885791234)(https://note.youdao.com/yws/res/28117/FF640E0A51694B26B5219E7352C1ECE7)]

3.2.5 测试

  • 启动eureka-server输入8100
  • 启动eureka-client
    [外链图片转存失败(img-uZkGgbGs-1563885791235)(https://note.youdao.com/yws/res/28132/207E13CF833B422A9D0018FB76EA044F)]
  • 修改eureka-client配置文件发布
    [外链图片转存失败(img-i6RRqkvJ-1563885791244)(https://note.youdao.com/yws/res/28134/CA9D6FE40BAA4FEE99E6495966E01262)]
    [外链图片转存失败(img-T7rcVV7P-1563885791245)(https://note.youdao.com/yws/res/28136/3D7FC1C933BC469D9E0373F0C0E29E41)]
  • 在启动eureka-server输入8400
    [外链图片转存失败(img-QSwlsfS7-1563885791247)(https://note.youdao.com/yws/res/28139/3CC3696EC8BA4742A91D14793643B752)]
  • 停止8100服务查看8400效果
    [外链图片转存失败(img-wRF33qcK-1563885791248)(https://note.youdao.com/yws/res/28141/7826705A16854DD89148E34793F6BF99)]
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐