Spring Cloud Eureka 註冊中心集羣搭建,Greenwich 最新版!

Spring Cloud 的註冊中心能夠由 Eureka、Consul、Zookeeper、ETCD 等來實現,這裏推薦使用 Spring Cloud Eureka 來實現註冊中心,它基於 Netflix 的 Eureka 作了二次封裝,完成分佈式服務中服務治理的功能,微服務系統中的服務註冊與發現都經過這個註冊中心來進行管理。java

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

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

快速構建一個 Eureka Server 項目

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

https://start.spring.io/微信

棧長這裏是生成了一個 Maven 示例項目。app

<?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

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

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 教程,轉載請原樣保留本信息。

相關文章
相關標籤/搜索