在父項目裏新建module,同樣爲maven,也可以選擇springInitializer。注意groupID和賦項目要相同。
aircraftId 設置爲 cloud-eureka
然後修改pom 文件的parent爲 上一章中的父項目。
然後添加eureka server 依賴
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.cloud.demo</groupId>
<artifactId>cloud</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cloud.demo</groupId>
<artifactId>cloud-eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>cloud-eureka</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
然後再啓動類上添加 @EnableEurekaServer
添加配置 application.yml
server:
port: 8888
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
spring:
application:
name: eureka-server
至此服務註冊中心已建好。打開127.0.0.1:8888即可看到註冊中心的可視化頁面。
註冊中心的高可用
註冊中心這麼重要的職責是必須保證高可用的,不然掛掉後所以服務都不可見了。
eureka的高可用其實很簡單,就是將註冊中心多跑幾個實例,然後每個實例的 service-url.defaultZone 的地址設置成其他實例的地址,這樣,當前實例就會註冊到其他註冊中心的實例中,大家互相註冊,這樣就有了註冊中心集羣,可以保證註冊中心的高可用。