天天看點

Spring Cloud Eureka 注冊中心叢集搭建,Greenwich 最新版!

Spring Cloud 的注冊中心可以由 Eureka、Consul、Zookeeper、ETCD 等來實作,這裡推薦使用 Spring Cloud Eureka 來實作注冊中心,它基于 Netflix 的 Eureka 做了二次封裝,完成分布式服務中服務治理的功能,微服務系統中的服務注冊與發現都通過這個注冊中心來進行管理。

今天棧長就來分享一個 Eureka 注冊中心玩法,從 0 到分布式叢集一步到位,單機版的咱就不玩了,沒意義。

本文基于最新的 Spring Cloud Greenwich.SR1 以及 Spring Boot 2.1.3 版本進行分享。

快速建構一個 Eureka Server 項目

打開 Spring 的快速建構網址,如下圖所示,選擇對應的參數,最後選擇 Eureka Server 依賴,生成項目示例代碼即可。

https://start.spring.io/
Spring Cloud Eureka 注冊中心叢集搭建,Greenwich 最新版!
Spring Cloud Eureka 注冊中心叢集搭建,Greenwich 最新版!
棧長這裡是生成了一個 Maven 示例項目。

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>cn.javastack</groupId>
    <artifactId>spring-cloud--eureka-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-cloud--eureka-server</name>
    <description>Demo project for Spring Cloud Eureka Server</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</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>

</project>
      

主要是加入了 Eureka Server 和 Spring Test 依賴包,還有 Spring Boot 和 Spring Cloud 的基礎依賴。

Maven就不多介紹了,不熟悉的,請關注Java技術棧微信公衆号,在背景回複:Maven,即可擷取棧長整理的一系列 Maven 系列教程文章。

開啟 Eureka Server 功能

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

}
      

在啟動類上加入 @EnableEurekaServer 注解,@EnableEurekaServer注解即開啟注冊中心伺服器的功能。

Spring Boot就不多介紹了,不熟悉的,請關注Java技術棧微信公衆号,在背景回複:Boot,即可擷取棧長整理的一系列 Spring Boot 系列教程文章。

添加 Eureka Server 配置

在 application.yml 中加入如下配置:

spring:
  application:
    name: register-center

eureka:
  instance:
    prefer-ip-address: false
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
    lease-expiration-duration-in-seconds: 30
    lease-renewal-interval-in-seconds: 5
  server:
    enable-self-preservation: true
    eviction-interval-timer-in-ms: 5000
  client:
    register-with-eureka: true
    fetch-registry: true
    serviceUrl:
      defaultZone: http://eureka1:8761/eureka/, http://eureka2:8762/eureka/


logging.level.com.netflix:
  eureka: OFF
  discovery: OFF

---
spring:
  profiles: rc1

server:
  port: 8761

eureka.instance.hostname: eureka1

---
spring:
  profiles: rc2

server:
  port: 8762

eureka.instance.hostname: eureka2
      

配置這裡不細講,下篇文章棧長單獨分享這些參數的含義,關注微信公衆号:Java技術棧,擷取第一時間推送。

這裡做了兩台注冊中心的高可用配置rc1,rc2,也可以做多台,既然是高可用,每個注冊中心都向别的注冊中心注冊自己。

注意不要用Localhost

Spring Cloud Eureka 注冊中心叢集搭建,Greenwich 最新版!

如上圖所示,如果大家在實戰中遇到叢集不可用,出現在 unavailable-replicas 裡面時,說明是你配置的問題。

如果 defaultZone 用了 localhost,prefer-ip-address 設定的是 false,則叢集不行,不能用 localhost,要配置 hosts,并代替 localhost。

127.0.0.1 localhost eureka1 eureka2
      

啟動 Eureka 注冊中心

這樣兩個注冊心的 Eureka Server 就搭好了,啟動的時候使用不同的 Profile 來指定不同的端口。

啟動 Eureka 注冊中心
這樣兩個注冊心的 Eureka Server 就搭好了,啟動的時候使用不同的 Profile 來指定不同的端口。

spring-boot:run -Dspring-boot.run.profiles=rc1
spring-boot:run -Dspring-boot.run.profiles=rc2      

按上方面指令啟動兩個 Eureka Server,然後再來驗證一下注冊情況,分别打開兩個 Eureka Server 控制台頁面。

http://localhost:8761/

http://localhost:8762/

Spring Cloud Eureka 注冊中心叢集搭建,Greenwich 最新版!

我們可以看到兩個注冊的注冊中心執行個體了。

好了,今天的分享就到這裡了,近期會分享更多 Eureka 進階玩法,棧長正在拼命撰寫中……關注Java技術棧微信公衆号可擷取及時推送。在公衆号背景回複:cloud,擷取棧長整理的更多的 Spring Cloud 教程,都是實戰幹貨,以下僅為部分預覽。

Spring Cloud 最新 Finchley 版本踩坑

Spring Cloud 多版本如何選擇

Spring Cloud 是什麼,和 Dubbo 對比

Spring Cloud 配置中心高可用搭建

Spring Cloud Eureka 自我保護機制

……