十五:Spring Cloud 之Eureka服务注册中心(HA版)

版权声明:本文为博主原创文章,觉得稍有帮助,可点赞、转载注明出处。 https://blog.csdn.net/chenghuaying/article/details/82821664

1. Eureka简介

二:Spring Cloud 之Eureka服务注册中心(单机版) 记录了eureka的单机版,这里通过eureka自带的特性实现HA版本。

2. 代码实现

2.1涉及的模块

  • eureka-server-ha:通过profiles指定不同的端口来模拟多服务实例。
  • eureka-service:服务提供者

2.2 源代码

2.2.1 Github地址

https://github.com/andyChenHuaYing/spring-cloud-demo

2.3 eureka-server-ha

2.3.1整体步骤

  1. 利用eureka-server相互注册功能来实现eureka的HA,所以代码很简单
  2. 使用8781、8782、8783三个端口来启动eureka-server使用8781、8782、8783三个端口来启动eureka-server
  3. 每个profile指定当前eureka服务的eureka-server地址是另外两个eureka服务
  4. 启动三个服务,观察eureka信息界面

2.3.2 pom文件添加依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring-cloud-finchley-demo</artifactId>
        <groupId>org.oscar.scd</groupId>
        <version>1.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>eureka-server-ha</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>
</project>

2.3.3 application-peer8781.yml

指定eureka-server相关设置,并且把自己注册另外两个服务中。
spring:
  application:
    name: eureka-server-ha

server:
  port: 8781

eureka:
  instance:
    hostname: peer8781
  client:
    serviceUrl:
      defaultZone: http://peer8782:8782/eureka/,http://peer8783:8783/eureka/ #Register self to another two eureka server

2.3.4 application-peer8782.yml

指定eureka-server相关设置,并且把自己注册另外两个服务中。
spring:
  application:
    name: eureka-server-ha

server:
  port: 8782

eureka:
  instance:
    hostname: peer8782
  client:
    serviceUrl:
      defaultZone: http://peer8781:8781/eureka/,http://peer8783:8783/eureka/ #Register self to another two eureka server

2.3.5 application-peer8783.yml

指定eureka-server相关设置,并且把自己注册另外两个服务中。
spring:
  application:
    name: eureka-server-ha

server:
  port: 8783

eureka:
  instance:
    hostname: peer8783
  client:
    serviceUrl:
      defaultZone: http://peer8782:8782/eureka/,http://peer8781:8781/eureka/ #Register self to another two eureka server

2.3.6 EurekaServerHaApplication

package org.oscar.scd.eureka.server.ha;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerHaApplication {

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

3. 验证

3.1 创建SpringBoot启动类

3.1.1 EurekaServerHaApplication-peer8783

3.1.2 EurekaServerHaApplication-peer8782

与上一步基本相同,只是将Active profiles修改成:peer8782

3.1.3 EurekaServerHaApplication-peer8781

与上一步基本相同,只是将Active profiles修改成:peer8781

3.2 将一下信息添加到hosts文件中

  • windows系统文件位置:C:\Windows\System32\drivers\etc\hosts
  • linux系统文件位置:`/etc/hosts
127.0.0.1 peer8781
127.0.0.1 peer8782
127.0.0.1 peer8783

3.3 启动SpringBoot启动类

  • EurekaServerHaApplication-peer8781
  • EurekaServerHaApplication-peer8782
  • EurekaServerHaApplication-peer8783

3.4 访问注册中心

访问任一eureka-server地址,如:http://localhost:8781/  可以查看eureka-server已经启动,并且其他两个eureka-server已经注册:

在这里插入图片描述

4. 思考

  • 是否支持zookeeper
  • 服务的自动注册与注销由哪些配置可控,如检测时间
  • 如何保持心跳连接

5. 补充

5.1 资料

http://cloud.spring.io/spring-cloud-static/Finchley.SR1/multi/multi_spring-cloud-eureka-server.html

http://cloud.spring.io/spring-cloud-static/Finchley.SR1/multi/multi_spring-cloud-eureka-server.html#spring-cloud-eureka-server-zones-and-regions

二:Spring Cloud 之Eureka服务注册中心(单机版)

四:对微服务所需的服务发现的理解

猜你喜欢

转载自blog.csdn.net/chenghuaying/article/details/82821664