上一篇文章《微服務注冊中心原理,看這篇就夠了!》介紹了注冊中心的概念和原理,本文將介紹下利用Eureka搭建中心並注冊服務到注冊中心的過程。
本文目錄
一、Eureka介紹
Eureka是Netflix開發的服務發現框架,SpringCloud將它集成在其子項目spring-cloud-netflix中,以實現SpringCloud的服務發現功能。
Eureka包含兩個組件:Eureka Server和Eureka Client
- Eureka Server提供服務注冊服務,各個節點啟動后,會在Eureka Server中進行注冊,這樣EurekaServer中的服務注冊表中將會存儲所有可用服務節點的信息,服務節點的信息可以在界面中直觀的看到。
- Eureka Client是一個java客戶端,用於簡化與Eureka Server的交互,客戶端同時也就是一個內置的、使用輪詢(round-robin)負載算法的負載均衡器。
二、搭建注冊中心
首先新建一個SpringBoot項目,命名spring-cloud-eureka,然后按照下面步驟編寫代碼即可。
- pom.xml代碼
添加eureka-server的依賴,代碼如下:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<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>Finchley.RELEASE</version><!-- eureka版本 -->
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
- 啟動類代碼
啟動類添加注解@EnableEurekaServer即可,代碼如下:
@EnableEurekaServer
@SpringBootApplication
public class SpringCloudEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudEurekaApplication.class, args);
}
}
- 配置文件
使用yml的配置文件,application.yml配置如下:
server:
port: 9001 #服務端口
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false #是否將eureka自身作為應用注冊到eureka注冊中心
fetch-registry: false #為true時,可以啟動,但報異常:Cannot execute request on any known server
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
配置項說明:
1. server.port=9001表示設置該服務注冊中心的端口號
2. eureka.instance.hostname=localhost表示設置該服務注冊中心的hostname
3. eureka.client.register-with-eureka=false,由於我們目前創建的應用是一個服務注冊中心,而不是普通的應用。默認情況下,這個應用會向注冊中心(也是它自己)注冊它自己,設置為false表示禁止這種默認行為
4. eureka.client.fetch-registry=false,表示不去檢索其他的服務,因為服務注冊中心本身的職責就是維護服務實例,它也不需要去檢索其他服務
- 運行截圖
打開瀏覽器訪問http://localhost:9001/,可以看到注冊中心以及啟動,運行截圖如下:

注冊中心運行截圖
三:注冊服務
服務注冊中心有了之后,我們可以向這個服務注冊中心注冊一個服務提供者,新建一個SpringBoot項目,命名spring-cloud-user-service,提供用戶服務,然后按照下面步驟編寫代碼即可。
- pom.xml代碼
添加eureka-server的依賴,代碼如下:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version><!-- eureka版本 -->
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
- 啟動類代碼
啟動類添加注解@EnableEurekaClient即可,代碼如下:
@EnableEurekaClient
@SpringBootApplication
public class UserServiceDemoApplication {
public static void main(String[] args) {
SpringApplication.run(UserServiceDemoApplication.class, args);
}
}
- 配置文件
使用yml的配置文件,application.yml配置如下:
server:
port: 8081 #服務端口
eureka:
client:
serviceUrl:
defaultZone: http://localhost:9001/eureka/
spring:
application:
name: user-service
- 運行截圖
打開瀏覽器訪問http://localhost:9001/,與上圖相比可以看到注冊中心中已經注冊好了用戶服務,截圖如下:

注冊服務后注冊中心截圖
到此SpringCloud搭建服務注冊中心與服務注冊已經全部實現,有問題歡迎留言溝通哦!
完整源碼地址: https://github.com/suisui2019/springboot-study
推薦閱讀
1.SpringBoot整合ActiveMQ,看這篇就夠了!
2.別在 Java 代碼里亂打日志了,這才是正確的打日志姿勢!
3.編碼神器Lombok,學會后開發效率至少提高一倍!
4.利用Spring Boot+zxing,生成二維碼還能這么簡單
5.Spring Boot之Profile--快速搞定多環境使用與切換
限時領取免費Java相關資料,涵蓋了Java、Redis、MongoDB、MySQL、Zookeeper、Spring Cloud、Dubbo/Kafka、Hadoop、Hbase、Flink等高並發分布式、大數據、機器學習等技術。
關注下方公眾號即可免費領取:
