一.前言
在Spring Cloud - Eureka的真情獨白中我們瞭解了Eureka的概念,今天我們來學習運用Eureka來搭建服務註冊中心;
二.創建項目
經過上面的一頓操作,我們已經成功創建項目,下面我們來看下項目結構
緊着者,我們來看下目前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.milo</groupId>
<artifactId>milgenius-springcloud</artifactId>
<version>1.0.0</version>
</parent>
<groupId>com.milo</groupId>
<artifactId>springcloud-eureka-server</artifactId>
<version>1.0.0</version>
<name>springcloud-eureka-server</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependencies>
<!--搭建Eureka Server需要添加的依賴-->
<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>
三.添加註解@EnableEurekaServer
我們需要添加@EnableEurekaServer來開啓服務
/**
* @author: milogenius
* @create: 2019/6/28 11:17
* @description: EurekaServer
**/
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
四.配置文件
server:
port: 8761
#EurekaServer配置
eureka:
instance:
hostname: localhost
client:
#代表不向註冊中心註冊自己
register-with-eureka: false
#由於註冊中心的職責就是維護服務實例,它並不需要去檢索服務,設置爲false
fetch-registry: false
#註冊中心地址
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
五.訪問註冊中心頁面
我們啓動項目,如下圖所示
接下來我們訪問http://localhost:8761,會跳轉到下面頁面
如果我們走到這一步,說明我們成功搭建了Eureka Server,但是目前我們沒有服務可用,在Spring Cloud 之Eureka Client中,我們將學習如何搭建Eureka Client並註冊服務到服務註冊中心,如何完成服務間的調用;