SpringCloud之Eureka注册中心集群篇

spring注册中心(eureka),但是存在一个单点故障的问题,一个注册中心远远无法满足实际的生产环境

,那么我们需要多个注册中心进行集群,达到真正的高可用。今天我们实战来搭建一个Eureka集群。

创建Eureka Server

1).创建一个Maven工程helloworld.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>

  <groupId>com.lx</groupId>
  <artifactId>helloworld.eureka.consumer</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>helloworld.eureka.consumer</name>
  <url>http://maven.apache.org</url>
 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
    <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
    <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

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

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

<repositories>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>
</project>

 2). 用Spring Boot创建一个服务类EurekaServerApplication,需要一个注解@EnableEurekaServer加在springboot工程的启动类上

/**
 * 
 */
package com.lx.helloworld.eureka.server;

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

/* author:tracy.bai
date:2018年10月18日 下午3:12:13
 **/
/**
 * @author tracy.bai
 *
 */

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

}

  3).eureka server的配置文件appication.yml,其中registerWithEureka:false和fetchRegistry:false表明自己是一个eureka server

defaultZone: http://localhost:8762/eureka/,8762 为另外一台服务器的端口
server:
   port: 8761

eureka:
   instance:
       hostname: master
   client:
       registerWithEureka: false
       fetchRegistry: false
       serviceUrl:
           defaultZone: http://localhost:8762/eureka/

  4)启动eureka server,然后访问http://localhost:8761, 界面如下, "No instances available" 表示无client注册

 

创建Eureka Server backup

pom.xml文件不变

eureka server的配置文件appication.yml,其中registerWithEureka:false和fetchRegistry:false表明自己是一个eureka server

defaultZone: http://localhost:8761/eureka/,8761为另外一台服务器的端口
server:
   port: 8762

eureka:
   instance:
       hostname: backup
   client:
       registerWithEureka: false
       fetchRegistry: false
       serviceUrl:
           defaultZone: http://localhost:8761/eureka/
View Code

启动eureka server,然后访问http://localhost:8762, 界面如下

 

 

 二:服务端注册

 

 设置了多个注册中心后,我们的服务配置不需要太多改动只需将biz-service-0工程的application.properties的serviceUrl修改下

 

1
 eureka.client.serviceUrl.defaultZone=http://master:7998/eureka/,http://backup:7999/eureka/,

 

 当然也可以绑定一个地址,eureka.client.serviceUrl.defaultZone=http://master:7998/eureka/  这两种绑定都可以

 

 

 三:集群理解

 

 虽然上面我们以双节点作为例子,但是实际上因负载等原因,我们往往可能需要在生产环境构建多于两个的Eureka Server节点。那么对于如何配置serviceUrl来让集群中的服务进行同步,需要我们更深入的理解节点间的同步机制来做出决策。我们通过一张图来理解集群

 

posted @ 2018-10-19 17:34  love fendi  阅读(299)  评论(0编辑  收藏  举报