Spring Cloud(二):Spring Cloud Eureka Server高可用注册服务中心的配置

前言

Eureka 作为一个云端负载均衡,本身是一个基于REST的服务,在 Spring Cloud 中用于发现和注册服务。 
那么当成千上万个微服务注册到Eureka Server中的时候,Eureka Server 的负载将会很大,这样一旦Eureka Server服务挂掉了,整个微服务架构也就瘫掉了,所以在实际生产环境中不光要对注册在Eureka Server中的微服务进行集群管理,还要对Eureka Server 本身进行集群管理,使整个微服务更加健壮,更加高可用。

背景介绍

服务中心

服务中心又称注册中心,管理各种服务功能包括服务的注册、发现、熔断、负载、降级等,比如dubbo admin后台的各种功能。

有了服务中心调用关系会有什么变化,画几个简图来帮忙理解

项目A调用项目B

正常调用项目A请求项目B

Spring Cloud(二):Spring Cloud Eureka Server高可用注册服务中心的配置

有了服务中心之后,任何一个服务都不能直接去掉用,都需要通过服务中心来调用

Spring Cloud(二):Spring Cloud Eureka Server高可用注册服务中心的配置

项目A调用项目B,项目B在调用项目C

Spring Cloud(二):Spring Cloud Eureka Server高可用注册服务中心的配置

这时候调用的步骤就会为两步:第一步,项目A首先从服务中心请求项目B服务器,然后项目B在从服务中心请求项目C服务。

Spring Cloud(二):Spring Cloud Eureka Server高可用注册服务中心的配置

上面的项目只是两三个相互之间的简单调用,但是如果项目超过20个30个呢,在15年底的时候我司分布式的项目就达到了二十几个,画一张图来描述几十个项目之间的相互调用关系全是线条,任何其中的一个项目改动,就会牵连好几个项目跟着重启,巨麻烦而且容易出错。通过服务中心来获取服务你不需要关注你调用的项目IP地址,由几台服务器组成,每次直接去服务中心获取可以使用的服务去调用既可。

由于各种服务都注册到了服务中心,就有了去做很多高级功能条件。比如几台服务提供相同服务来做均衡负载;监控服务器调用成功率来做熔断,移除服务列表中的故障点;监控服务调用时间来对不同的服务器设置不同的权重等等。

说Eureka之前我先八卦一下Netflix

Netflix

以下介绍来自于百度百科:

Netflix是一家美国公司,在美国、加拿大提供互联网随选流媒体播放,定制DVD、蓝光光碟在线出租业务。该公司成立于1997年,总部位于加利福尼亚州洛斯盖图,1999年开始订阅服务。2009年,该公司可提供多达10万部DVD电影,并有1千万的订户。2007年2月25日,Netflix宣布已经售出第10亿份DVD。HIS一份报告中表示,2011年Netflix网络电影销量占据美国用户在线电影总销量的45%。

我第一次看到这个单词的时候,是在各种美剧或者电影的开头,Netflix拍摄的代表性的美剧有《纸牌屋》、《毒枭》、《怪奇物语》。后来研究springcloud的时候发现了Netflix公司,就在想它们是不是同一家公司,经过核对github上面邮件后缀判定确实是同一家公司,其实springcloud的微服务就基于Netflix公司的开源产品来做的

Netflix的开源框架组件已经在Netflix的大规模分布式微服务环境中经过多年的生产实战验证,正逐步被社区接受为构造微服务框架的标准组件。Spring Cloud开源产品,主要是基于对Netflix开源组件的进一步封装,方便Spring开发人员构建微服务基础框架。对于一些打算构建微服务框架体系的公司来说,充分利用或参考借鉴Netflix的开源微服务组件(或Spring Cloud),在此基础上进行必要的企业定制,无疑是通向微服务架构的捷径。

Eureka

按照官方介绍:

Eureka is a REST (Representational State Transfer) based service that is primarily used in the AWS cloud for locating services for the purpose of load balancing and failover of middle-tier servers.

Eureka 是一个基于 REST 的服务,主要在 AWS 云中使用, 定位服务来进行中间层服务器的负载均衡和故障转移。

Spring Cloud 封装了 Netflix 公司开发的 Eureka 模块来实现服务注册和发现。Eureka 采用了 C-S 的设计架构。Eureka Server 作为服务注册功能的服务器,它是服务注册中心。而系统中的其他微服务,使用 Eureka 的客户端连接到 Eureka Server,并维持心跳连接。这样系统的维护人员就可以通过 Eureka Server 来监控系统中各个微服务是否正常运行。Spring Cloud 的一些其他模块(比如Zuul)就可以通过 Eureka Server 来发现系统中的其他微服务,并执行相关的逻辑。

Eureka由两个组件组成:Eureka服务器和Eureka客户端。Eureka服务器用作服务注册服务器。Eureka客户端是一个java客户端,用来简化与服务器的交互、作为轮询负载均衡器,并提供服务的故障切换支持。Netflix在其生产环境中使用的是另外的客户端,它提供基于流量、资源利用率以及出错状态的加权负载均衡。

用一张图来认识以下:

Spring Cloud(二):Spring Cloud Eureka Server高可用注册服务中心的配置

上图简要描述了Eureka的基本架构,由3个角色组成:

1、Eureka Server

  • 提供服务注册和发现

2、Service Provider

  • 服务提供方
  • 将自身服务注册到Eureka,从而使服务消费方能够找到

3、Service Consumer

  • 服务消费方
  • 从Eureka获取注册服务列表,从而能够消费服务

开始介绍Eureka Server 的集群搭建

一、准备工作

1.1 准备三台机器用于集群搭建

192.168.1.11 
192.168.1.12 
192.168.1.13

1.2 修改三台机器的host文件

windows目录:C:WindowsSystem32driversetchosts 
linux目录:/etc/hosts

192.168.1.11  peer1
192.168.1.12  peer2
192.168.1.13  peer3

二、创建Eureka Server工程

2.1 创建一个spring boot 的 maven工程

在pom.xml中加入eureka-server的引用 
完整pom.xml 文件内容

<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">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>EurekaServerHA</groupId>
    <artifactId>EurekaServerHA</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>EurekaServer</artifactId>
  
  
  <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
  
  
  <dependencyManagement>
      <dependencies>
          <dependency>
              <groupId>org.springframework.cloud</groupId>
              <artifactId>spring-cloud-dependencies</artifactId>
              <version>Camden.SR7</version>
              <type>pom</type>
            <scope>import</scope>
          </dependency>
      </dependencies>
  </dependencyManagement>
  
  
  <build>
        <resources>
            <!-- 控制资源文件的拷贝 -->
            <!--<resource> <directory>src/main/resources</directory> <targetPath>${project.build.directory}</targetPath> 
                <filtering>true</filtering> <includes> <include>**/*</include> </includes> 
                </resource> -->
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <!--changeaddClasspathtotrueifdaksisdesktopversion -->
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>com.eurekaha.BootApplication</mainClass>
                            <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                            <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
                        </manifest>
                        <manifestEntries>
                            <Permissions>${Permissions}</Permissions>
                            <Caller-Allowable-Codebase>${Caller-Allowable-Codebase}</Caller-Allowable-Codebase>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
            <!-- 解决资源文件的编码问题 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <configuration>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                            <outputDirectory>
                                ${project.build.directory}/lib
                            </outputDirectory>
                        </configuration>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
  
  
  <dependencies>
      <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-eureka-server</artifactId>
      </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
  </dependencies>
  
</project>
2.2 建立三个配置文件

注意 eureka.client.serviceUrl.defaultZone 这行配置,每一个配置里面配置其他的eureka server 地址即可,多个服务用 逗号分隔

application-peer1.properties

server.port=8761
spring.application.name = eureka_server
eureka.instance.hostname=peer1
eureka.client.serviceUrl.defaultZone=http://peer2:8761/eureka/,http://peer3:8761/eureka/

application-peer2.properties

server.port=8761
spring.application.name = eureka_server
eureka.instance.hostname=peer2
eureka.client.serviceUrl.defaultZone=http://peer1:8761/eureka/,http://peer3:8761/eureka/

application-peer3.properties

server.port=8761
spring.application.name = eureka_server
eureka.instance.hostname=peer3
eureka.client.serviceUrl.defaultZone=http://peer1:8761/eureka/,http://peer2:8761/eureka/
2.3 启用EurekaServer

用 @EnableEurekaServer 注解来表明是一个Eureka Server

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

三、打包发布

用 maven 打成jar包,并将jar包分别放到三个服务器上 
在三个服务器上分别启动服务,分别执行下面三行命令

java -jar eureka-server-1.0.0.jar --spring.profiles.active=s1
java -jar eureka-server-1.0.0.jar --spring.profiles.active=s2
java -jar eureka-server-1.0.0.jar --spring.profiles.active=s3

访问 http://192.168.1.11:8761 

Spring Cloud(二):Spring Cloud Eureka Server高可用注册服务中心的配置


在界面中 DS Replicas 下会看到 peer2,peer3 节点 
同理访问peer2里面会有peer1和peer3节点,访问peer3里也会有peer1和peer2节点

注意点:三个节点的配置文件的端口要一样的,不然无效

四、客户端注册

在client端,只需要把 eureka.client.serviceUrl.defaultZone 改成相应的集群地址即可,多个服务用逗号分隔

eureka.client.serviceUrl.defaultZone = http://peer1:8761/eureka/,http://peer2:8761/eureka/,http://peer3:8761/eureka/

五、测试

访问client端资源,能正常访问 
停掉Eureka Server 其中1台或者2台,资源依然能正常访问

OK,Eureka Server 集群就已经搭建好了