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/

棧長這里是生成了一個 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

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

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

127.0.0.1 localhost eureka1 eureka2

啟動 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/

我們可以看到兩個注冊的注冊中心實例了。

好了,今天的分享就到這里了,近期會分享更多 Eureka 高級玩法,棧長正在拼命撰寫中……關注Java技術棧微信公眾號可獲取及時推送。在公眾號后台回復:cloud,獲取棧長整理的更多的 Spring Cloud 教程,都是實戰干貨,以下僅為部分預覽。

  • Spring Cloud 最新 Finchley 版本踩坑
  • Spring Cloud 多版本如何選擇
  • Spring Cloud 是什么,和 Dubbo 對比
  • Spring Cloud 配置中心高可用搭建
  • Spring Cloud Eureka 自我保護機制
  • ……

大家有什么問題,也可以點擊這個鏈接加入Java技術棧知識星球,和大家共同討論,也可以向棧長提問,快 2000 人已加入。

本文原創首發於微信公眾號:Java技術棧(id:javastack),關注公眾號在后台回復 "cloud" 可獲取更多 Spring Cloud 教程,轉載請原樣保留本信息。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM