Eureka高可用集群搭建

Eureka Server可通過運(yùn)行多臺(tái)實(shí)例并相互注冊(cè)的方式來實(shí)現(xiàn)高可用,各實(shí)例是平等的,沒有master和slave之分,不象zookeeper一樣有選舉機(jī)制。

本例用三臺(tái)eureka server+一臺(tái)eureka clien搭建高可用環(huán)境:

一、Eureka Server
1、修改host文件:
127.0.0.1 peer1 peer2 peer3

2、application.yml配置文件

spring:
application:
  name: microservice-discovery-eureka-server

logging:
level:
  com:
    netflix:
      eureka: on
      discovery: on

---
spring:
# 指定profile=peer1
profiles: peer1

server:
port: 8761

eureka:
instance:
  # 當(dāng)指定profile=peer1時(shí),主機(jī)名是peer1
  hostname: peer1
client:
  service-url:
    defaultZone: http://peer2:8762/eureka,http://peer3:8763/eureka

server:
  #關(guān)閉自我保護(hù),從而會(huì)保證注銷微服務(wù)
  enable-self-preservation: false
  # 清理時(shí)間間隔(單位毫秒,默認(rèn)60*1000)
  eviction-interval-timer-in-ms: 3000
---
spring:
# 指定profile=peer2
profiles: peer2

server:
port: 8762

eureka:
instance:
  # 當(dāng)指定profile=peer2時(shí),主機(jī)名是peer2
  hostname: peer2
client:
  service-url:
    defaultZone: http://peer1:8761/eureka,http://peer3:8763/eureka

server:
  #關(guān)閉自我保護(hù),從而會(huì)保證注銷微服務(wù)
  enable-self-preservation: false
  # 清理時(shí)間間隔(單位毫秒,默認(rèn)60*1000)
  eviction-interval-timer-in-ms: 3000
---
spring:
# 指定profile=peer3
profiles: peer3

server:
port: 8763

eureka:
instance:
  # 當(dāng)指定profile=peer3時(shí),主機(jī)名是peer3
  hostname: peer3
client:
  service-url:
    defaultZone: http://peer1:8761/eureka,http://peer2:8762/eureka

server:
  #關(guān)閉自我保護(hù),從而會(huì)保證注銷微服務(wù)
  enable-self-preservation: false
  # 清理時(shí)間間隔(單位毫秒,默認(rèn)60*1000)
  eviction-interval-timer-in-ms: 3000

# 說明:
# 1、使用連接字符串(---)將配置文件分為四段。第二、第三、第四段分別為spring.properties指定了一個(gè)值,該值表示它所在的那段內(nèi)容應(yīng)在哪個(gè)profile里。
# 第一段由于沒有指定spring。profiles,因此這段內(nèi)容會(huì)對(duì)所有profile生效。
# 2、因配置中使用了主機(jī)名,故需修改host文件(添加127.0.0.1 peer1 peer2 peer3)

3、啟動(dòng)類:

package com.ding.eureka.server;

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

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

4、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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.ding</groupId>
    <artifactId>eureka-ha-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>eureka-ha-server</name>
    <description>Eureka HA Server for Spring Cloud</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.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>
        <spring-cloud.version>Finchley.M9</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</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>${spring-cloud.version}</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>

二、Eureka Client
1、application.yml配置文件

spring:
  application:
    name: a-bootiful-client
  cloud:
    client:
      ipAddress: 192.168.0.50
server:
  port: 8080

eureka:
  instance:
    # eureka client向eureka server發(fā)送心跳的時(shí)間間隔,默認(rèn)30秒,將此值改小可解決eureka注冊(cè)慢的問題
    lease-renewal-interval-in-seconds: 5
    # 續(xù)約到期時(shí)間(默認(rèn)90秒)
    lease-expiration-duration-in-seconds: 5
    # 將instance id設(shè)置成IP:端口的形式
    # 說明:通過試驗(yàn),spring.cloud.client.ipAddress及server.port要配置文件中必須有值
    instance-id: ${spring.cloud.client.ipAddress}:${server.port}
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/

2、啟動(dòng)類:

package com.ding.eureka.client;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@EnableDiscoveryClient
@SpringBootApplication
public class EurekaClientApplication {

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

@RestController
class ServiceInstanceRestController {

    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping("/service-instances/{applicationName}")
    public List<ServiceInstance> serviceInstancesByApplicationName(@PathVariable String applicationName) {
        return this.discoveryClient.getInstances(applicationName);
    }

    @RequestMapping("/test")
    public String test() {
        return "hello";
    }
}

3、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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.ding</groupId>
    <artifactId>eureka-ha-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>eureka-ha-client</name>
    <description>Eureka HA Client for Spring Cloud</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.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-netflix-eureka-server</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</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>Finchley.M9</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/libs-milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

</project>

三、測試步驟
1、打包Eureka Server 及Eureka Client
可使用該命令跳過junit測試:mvn clean package -Dmaven.test.skip=true

2、在jar包對(duì)應(yīng)的目錄下分別執(zhí)行以下三個(gè)命令,以啟動(dòng)三個(gè)eureka-ha-server實(shí)例
java -jar eureka-ha-server-1-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1
java -jar eureka-ha-server-1-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2
java -jar eureka-ha-server-1-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer3
說明:通過--spring.profiles.active=peer3以指定使用哪個(gè)profile啟動(dòng)

3、啟動(dòng)eureka-ha-client

4、分別訪問http://peer1:8761,http://peer2:8762,http://peer3:8763來查看

image.png


image.png

image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容