巧玩SpringCloud——使用Eureka搭建服務註冊中心與服務發現
轉載請註明出處:http://blog.csdn.net/dongdong9223/article/details/84549747
本文出自【我是幹勾魚的部落格】
Ingredient:
-
Java:Java SE Development Kit 8u162(Oracle Java Archive),Linux下安裝JDK修改環境變數
-
Eclipse:Eclipse Neon 3 Packages
-
SpringBoot: SPRING INITIALIZR(Spring Boot)
之前在巧玩SpringBoot——SpringBoot的第一個“Hello World!”
GitHub原始碼:springcloud-eureka。
1 SpringCloud是什麼
我們知道,SpringBoot是由Pivotal團隊提供的全新框架,其設計目的是用來簡化新Spring應用的初始搭建以及開發過程。也就是說SpringBoot其實不是什麼新的框架,它預設配置了很多框架的使用方式,使得搭建服務非常簡便,這使得她打上了微服務的快車,被廣泛應用於微服務領域的服務開發。
俗話說,家不可一日無主!既然每個“微(小)”的“服務”工作已經有人做了,那麼這些服務之間的管理又由誰來管呢?答案就是SpringCloud!SpringCloud就是微服務之間的大管家,統一協調服務系統之間的諸多工作的,比如服務發現註冊、配置中心、訊息匯流排、負載均衡、斷路器、資料監控等。
SpringBoot與SpringCloud本來是簡化Spring家族的系統操作的,不過自從搭上了微服務這個時下異常火熱的技術快車之後,已經蓬勃發展、圈粉無數,就如同周杰倫與方文山一樣組合成實力強悍、互為御用的好夥(ji)伴(you)^_^,一躍成為Spring家族的當家花旦!
2 建立服務註冊中心(Server)
這裡首先說明一點,雖然說SpringCloud與SpringBoot互為御用,從概念上說它們有不同分工,但實現上它們之間卻並不是各自獨立的實體,而是一種寄生關係:SpringCloud也要基於SpringBoot這個服務體來實現其功能。
2.1 官網下載Maven工程
2.1.1 選擇配置
來到SPRING INITIALIZR,選擇配置為:
- Generate a:Maven Project
- With:Java
- Spring Boot:2.1.0
在Project Metadata中配置好Group和Artifact。
在Dependencies中輸入Eureka Server並將其選擇。
最後點選Generate Project,生成一個Maven工程的模板並下載下來,使用Eclipse將其匯入。
2.1.2 pom.xml的配置
自動生成的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>com.yhd</groupId>
<artifactId>springcloudserver</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springcloudserver</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.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>Greenwich.M3</spring-cloud.version>
</properties>
<dependencies>
<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>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
2.1.3 修改SpringBoot的入口檔案
這裡的入口檔案為:
SpringcloudserverApplication.java
在其中加入註解:
@EnableEurekaServer
檔案內容如下:
package com.yhd.springcloudserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class SpringcloudserverApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudserverApplication.class, args);
}
}
2.1.4 編輯application.yml檔案
這裡使用yml的編輯方式。先將
application.properties
檔名改為:
application.yml
在其中加入:
server:
port: 7001
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
spring:
application:
name: eureka-server
2.2 執行Server
啟動檔案:
SpringcloudserverApplication.java
在瀏覽器中輸入:
這樣就進入了Eureka Server的介面,如圖所示:
這裡面的:
No instances available
表示還沒有發現註冊進來的服務。
3 建立服務提供者(Client)
OK服務中心建立好了,我們來建立一個服務提供者,也就是Eureka Client。
3.1 官網下載Maven工程
3.1.1 選擇配置
這裡面的配置大部分都同2.1.1中配置的一樣,除了下面:
在Dependencies中,要輸入:
- Web
- Eureka Discovery
最後點選Generate Project生成模板,下載下來並匯入進Eclipse裡面。
3.1.2 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>com.yhd</groupId>
<artifactId>springcloudclient</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springcloudclient</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.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>Greenwich.M3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</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>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
3.1.3 修改SpringBoot的入口檔案
這裡的入口檔案為:
SpringcloudclientApplication.java
這裡要做2件事:添加註解,新增一個controller。
3.1.3.1 添加註解
在其中加入註解:
@EnableEurekaClient
@RestController
3.1.3.2 新增controller
@RequestMapping("/hello")
public String home(@RequestParam(value = "name", defaultValue = "zhangsan") String name) {
return "Hello " + name + " ,your port is:" + port;
}
整體上檔案內容如下:
package com.yhd.springcloudclient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableEurekaClient
@RestController
public class SpringcloudclientApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudclientApplication.class, args);
}
@Value("${server.port}")
String port;
@RequestMapping("/hello")
public String home(@RequestParam(value = "name", defaultValue = "張三") String name) {
return "Hello " + name + " ,your port is:" + port;
}
}
3.1.4 編輯application.yml檔案
編輯application.yml
在其中加入:
server:
port: 7002
eureka:
instance:
hostname: service1
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://localhost:7001/eureka/
spring:
application:
name: eureka-service
這裡的設定,同2.1.4對比,去掉了:
eureka:
client:
#設定是否向註冊中心註冊,預設是true
registerWithEureka: false
#是否需要去檢索尋找服務,預設是true
fetchRegistry: false
由於這兩個配置預設都為true,對於client端就不需要設定了,這樣才會將自己的服務暴露給註冊中心。
3.2 執行client
啟動client的入口檔案:
SpringcloudclientApplication.java
4 檢視服務註冊中心
確保先執行Eureka Server,再執行Eureka Client,這樣將兩個服務都執行起來後,在瀏覽器中輸入:
進入Eureka Server的介面,如圖所示:
能夠看到註冊中心已經成功查詢到了服務並將其註冊了進來。
5 呼叫服務
再開啟一個瀏覽器頁面,輸入:
會得到返回結果的響應:
Hello zhangsan ,your port is:7002
如圖所示:
可見註冊成功的服務也被成功呼叫了!
6 參考:
巧玩SpringBoot——SpringBoot的第一個“Hello World!”
史上最簡單的 SpringCloud 教程 | 第一篇: 服務的註冊與發現Eureka(Finchley版本)