說明
SpringCloud Eureka是SpringCloud Netflix微服務套件中的一部分,它基于Netflix Eureka做了二次封裝,主要負責完成微服務架構中的服務治理功能,Eureka是用于微服務注冊和發現的工具。
服務治理:服務治理可以說是微服務架構中最為核心和基礎的子產品,它主要用于實作各個微服務執行個體的自動化注冊與發現。主要通過服務注冊與服務發現機制來完成對微服務應用執行個體的自動化管理
服務注冊:在服務治理架構中,通常會建構一個注冊中心,每個服務單元向注冊中心登記自己提供的服務,,将主機與端口号、版本号、通信協定等一些附加資訊高速注冊中心,注冊中心按服務名分類組織服務清單。
服務發現:在服務治理架構的運作下,服務間的調用不再通過指定具體的執行個體位址來實作,而是通過向服務名發起請求調用實作。是以,服務調用方在調用服務提供方接口時,并不知道具體的服務執行個體位置。是以,調用方需要向注冊中心咨詢服務,并擷取所有服務的執行個體清單,以實作對具體服務執行個體的通路。
目标
使用SpringCloud Eureka搭建兩個互相注冊的注冊中心來形成一個簡單的高可用性Eureka叢集。
快速使用
首先給出項目結構
1 更新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>yunlingfly</groupId>
<artifactId>mavenspringcloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>mavenspringcloud</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.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>
</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-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<!--<scope>provided</scope>-->
</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>Dalston.SR2</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 編寫配置檔案application.yml
server:
port: 8761
spring:
application:
name: peer1
eureka:
instance:
#填寫伺服器IP
hostname: xxx.xxx.xxx.xxx
#使得可以用IP配置位址
prefer-ip-address: true
client:
# 注冊自己
registerWithEureka: true
# 發現服務
fetchRegistry: true
service-url:
#設定注冊位址
defaultZone: http://${eureka.instance.hostname}:8762/eureka/
info:
name: peer1
version: 0.0.1
3 編寫啟動類
package yunlingfly.mavenspringcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class MavenspringcloudApplication {
public static void main(String[] args) {
SpringApplication.run(MavenspringcloudApplication.class, args);
}
}
4 重新建立一個項目,配置與上面一樣,隻需更改application.yml如下
server:
port: 8762
spring:
application:
name: peer2
eureka:
instance:
hostname: xxx.xxx.xxx.xxx
#使得可以用IP配置位址
prefer-ip-address: true
client:
registerWithEureka: true
fetchRegistry: true
service-url:
#設定注冊位址
defaultZone: http://${eureka.instance.hostname}:8761/eureka/
info:
name: peer2
version: 0.0.1
5 分别啟動兩個服務->啟動類右鍵->Run(無順序要求),或者使用Maven install打包成jar,然後使用java -jar jar包名來運作。然後浏覽器輸入你前面填的IP位址:8761,例如http://localhost:8761,即可進入Eureka圖形化界面
注意事項:
如果是在伺服器上使用Eureka注意開啟相應端口号否則外網無法通路,Eureka部署在伺服器上(例如CentOS)需要保持運作狀态,可以檢視我的另一篇文章->使用tmux在CentOS7下開啟SpringBoot項目