需要的組件:Spring Cloud Netflix 的 Eureka (服務註冊和發現模塊)
Netflix :內特弗麗克斯
Rureka: 尤里卡 (還是個希臘語)
Eureka 和 Zookeeper 都是服務註冊和發現模塊 功能幾乎一摸一樣
和其他服務一樣,先創建一個服務模塊
eg; hello-spring-cloud-rureka
在其目錄下創建 Pom.xml文件
pom.xml
<?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>com.funtl</groupId>
<artifactId>hello-spring-cloud-dependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../hello-spring-cloud-dependencies/pom.xml</relativePath>
</parent>
<artifactId>hello-spring-cloud-eureka</artifactId>
<packaging>jar</packaging>
<name>hello-spring-cloud-eureka</name>
<url>http://www.funtl.com</url>
<inceptionYear>2018-Now</inceptionYear>
<dependencies>
<!-- Spring Boot Begin -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Boot End -->
<!-- Spring Cloud Begin -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- Spring Cloud End -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.einblatt.hello.spring.cloud.eureka.EurekaApplication</mainClass>
<!--mainClass的路徑爲SpringBoot啓動類-->
</configuration>
</plugin>
</plugins>
</build>
</project>
別忘配置SDK 和 最新特性
目錄結構
在Pom.xml中配置好了之後就要使用它 ,讓Eureka啓動 (啓動一個服務中心只需一個註解)
在SpringBootApplication.java 類上啓用 @EnableEurekaServer 註解
package com.einblatt.hello.spring.cloud.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class);
}
}
Eureka 是一個高可用的組件,它沒有後端緩存,每個實例註冊之後需要向註冊中心發送心跳(可以在內存中完成),在默認情況下
Eureka Server 也是一個Eureka Client,必須要指定一個Server
Eureka是一個服務 所以需要在application.yml文件中配置一個Eureka的服務
application.yml文件
spring:
application:
name: hello-spring-cloud-eureka
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false # registerWithEureka 和 fetchRegistry 爲false 代表Eureka此時
fetchRegistry: false 是個服務
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/