什麼是Eureak
Eureka 是 Netflix 公司開源的產品,它是一種基於 REST(Representational State Transfer)的服務,主要用於 AWS 雲。Eureka 提供了完整的 Service Registry 和 Service Discovery 實現,也是 Spring Cloud 體系中最重要最核心的組件之一。
簡單來說 ,Eureka 就是 Netflix 開源的一款提供服務註冊和發現的產品,並且提供了 Java 客戶端。當然在 Spring Cloud 大力優化後的 Eureka,已經不僅僅只是用於 AWS 雲,而是可以應用在任何需要使用註冊中心的場景。
Eureka 由兩個組件組成:Eureka 服務端和 Eureka 客戶端。Eureka 服務端就是註冊中心。Eureka 客戶端是一個 java 客戶端,用來簡化與服務端的交互、作爲輪詢負載均衡器,並提供服務的故障切換支持。
下面是Eureka的使用場景
從上面看Eureka Server擔任註冊中心的角色,提供了服務的發現和註冊功能
Service Provider 服務提供者,將自身的服務註冊到Eureka Server,同時通過心跳檢查服務的運行狀態
Service Consumer 服務調用者,從Eureka Server 得到註冊的服務列表,找到對應的服務地址在調用並使用
SpringBoot2.x整合SpringCloud
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.version>4.12</junit.version>
<log4j.version>1.2.17</log4j.version>
<lombok.version>1.16.18</lombok.version>
<mysql.version>5.1.47</mysql.version>
<druid.version>1.1.16</druid.version>
<mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- <dependency>-->
<!-- <groupId>come.alibaba.cloud</groupId>-->
<!-- <artifactId>spring-cloud-alibaba.dependencies</artifactId>-->
<!-- <version>2.1.0.RELEASE</version>-->
<!-- <type>pom</type>-->
<!-- <scope>import</scope>-->
<!-- </dependency>-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.spring.boot.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.1.4.RELEASE</version>
<configuration>
<fork>true</fork>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
在主啓動類中添加一下註解:表示該model爲服務註冊中心的提供者
@EnableEurekaServer
在hosts文件中添加如下:hosts文件地址爲->C:\Windows\System32\drivers\etc
- 127.0.0.1 eureka7001.com
- 127.0.0.1 eureka7002.com
修改application.yml文件
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://eureka7001.com:7001/eureka
成功後的頁面如下:
頁面內容分析:可以發現後臺頁面被分爲了五大部分
- System Status 代表的系統狀態
- DS Replicas 該服務從哪裏同步數據
- Instances currently registered with Eureka 註冊在Eureka的實例列表
- General Info 系統運行環境 如cpu、內存等信息
- Instance Info 本服務的基礎信息 如ip地址 狀態等
總結:搭建Eureka是非常簡單的,並且Eureka是可用高可用的,比如可用集羣;