SpringCloud(二):服務的註冊與發現(Eureka)
一、什麼是服務註冊與發現 Spring Cloud Eureka 模組提供的功能是被動式的服務發現。
服務註冊:每個使用者去聊天室伺服器上註冊。
服務發現:這樣他的好友們就能看到你,你同時也將獲取好友的上線列表.
微服務中,服務就相當於聊天室的使用者,而服務註冊中心就像聊天室伺服器一樣。
目前服務發現的解決方案有Eureka,Consul,Zookeeper等等。SpringCloud預設使用eureka作為服務註冊中心。
二、Eureka使用過程
簡單一點就是:
上圖簡要描述了Eureka的基本架構,由3個角色組成
1、Eureka Server 提供服務註冊和發現
2、Service Provider 服務提供方 將自身服務註冊到Eureka,從而使服務消費方能夠找到
3、Service Consumer 服務消費方 從Eureka獲取註冊服務列表,從而能夠消費服務
三、環境準備 為了簡介,以後的文章都不會貼出所有的Maven Pom依賴,除非特殊說明,否則版本都是以下面的版本為準。
SpringBoot 1.5.12.RELEASE SpringCloud Dalston.SR5 四、服務註冊中心 Eureka Server
簡單來說就是三步:
Maven依賴 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>
<groupId>cn.saytime</groupId> <artifactId>eureka-server</artifactId> <version>1.0</version> <packaging>jar</packaging>
<name>eureka-server</name> <description>SpringCloud Eureka Server</description>
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.12.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent>
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Dalston.SR5</spring-cloud.version> </properties>
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-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>
註冊中心配置 application.properties ## 該註冊伺服器的埠 server.port=8001
## 應用啟動名稱 spring.application.name=eureka-server
## eureka配置 ## 註冊伺服器的名稱 eureka.instance.hostname=localhost ## 在預設設定下,該服務註冊中心也會將自己作為客戶端來嘗試註冊它自己,所以我們需要禁用它的客戶端註冊行為 ## 是否從eureka伺服器獲取註冊資訊,所以這裡是false eureka.client.fetch-registry=false ## 是否註冊自身到eureka伺服器,因為當前這個應用就是eureka伺服器,沒必要註冊自身,所以這裡是false eureka.client.register-with-eureka=false ## 服務釋出的地址 eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
SpringcloudDemoEurekaServerApplication.java
@EnableEurekaServer package cn.saytime;
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, args); } } 啟動訪問:http://localhost:8001
可以看到Application中No instances available,因為還沒有任何服務註冊。接下來我們寫一個使用者服務註冊到eureka註冊中心
四、服務提供方 Service Provider
這裡簡單寫一個hello service,為了看起來簡單,介面也直接放在啟動類裡面。
pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency>
application.properties
## 該服務釋出的埠 server.port=8011
## 注意這裡是作為服務註冊名稱,在eureka註冊中心註冊這個名稱,自動轉化為全大寫,之後呼叫服務用服務名呼叫 spring.application.name=user-service eureka.client.register-with-eureka=true eureka.client.fetch-registry=true ## 註冊服務中心的配置 eureka.client.service-url.defaultZone=http://localhost:8001/eureka/
啟動類
@EnableEurekaClient
package cn.saytime;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication @EnableEurekaServer @RestController public class HelloServiceApplication {
public static void main(String[] args) { SpringApplication.run(HelloServiceApplication.class, args); }
@RequestMapping("hello") public String hello (String name) { return "hello, " + name; } } 啟動服務,然後繼續檢視eureka註冊中心,重新整理,檢視變化,發現Hello-SERVICE服務已經註冊在註冊中心
測試使用者服務:
http://localhost:8011/hello?name=eureka
hello, eureka
五、服務消費方
服務消費方式有restTemplate+ribbon以及feign,所以下章在講如何消費註冊中心的服務。