這是一個基於Spring Cloud Eureka服務註冊發現的框架demo,希望讀者可以通過這篇文章大概能看懂這一個簡單的框架搭建。後續我會陸續更新,微服務架構(Spring Boot、Spring Cloud)、分佈式架構(Dobbo+Zookeeper)以及源碼解析等相關的文章,感興趣的話可以關注一下。
一、Eureka簡介
Eureka是由Netflix開發的一款服務治理開源框架,Spring-cloud對其進行了集成。
Eureka包含兩個組件:Eureka Server和Eureka Client。
Eureka服務端是一個服務註冊中心(Eureka Server),提供服務的註冊和發現,即當前有哪些服務註冊進來可供使用;各個節點啓動後,會在Eureka Server中進行註冊,這樣EurekaServer中的服務註冊表中將會存儲所有可用服務節點的信息,服務節點的信息可以在界面中直觀的看到。
Eureka客戶端爲服務提供者(Server Provider),t是一個java客戶端,用於簡化與Eureka Server的交互,客戶端同時也就是一個內置的、使用輪詢(round-robin)負載算法的負載均衡器。在應用啓動後,它將自己提供的服務註冊到Eureka服務端,並週期性地發送心跳來更新它的服務租約,同時也能從服務端查詢當前註冊的服務信息並把它們緩存到本地並週期性地刷新服務狀態。這樣服務消費者(Server Consumer)便可以從服務註冊中心獲取服務名稱,並消費服務。
二、eureka-server(服務註冊中心)
1、pom.xml
POM是項目對象模型(Project Object Model)的簡稱,它是Maven項目中的文件,使用XML表示,名稱叫做pom.xml。作用類似ant的build.xml文件,功能更強大。該文件用於管理:源代碼、配置文件、開發者的信息和角色、問題追蹤系統、組織信息、項目授權、項目的url、項目的依賴關係等等。
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cloud</groupId>
<artifactId>eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-server</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/>
</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>
<dependencyManagement>
<!-- Spring Cloud版本 -->
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<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>
<!-- 引入服務註冊發現組件 eureka依賴 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、application.yml
Spring Boot配置文件有兩種:application.yml和application.properties,作用是一樣的,不過因爲yml文件是樹狀結構,寫起來有更好的層次感,更易於理解,所以很多人都選擇了yml文件。
spring:
application:
name: eureka-server
server:
port: 8001
eureka:
instance:
hostname: localhost
client:
fetch-registry: false #是否註冊自身 默認true-註冊
register-with-eureka: false #是否檢查服務清單 默認true-檢查
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
3、EurekaServerApplication
在啓動類上添加@EnableEurekaServer註解,表明這是一個Eureka服務端。
@SpringBootApplication
@EnableEurekaServer //啓用服務註冊服務
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
啓動服務,訪問http://localhost:8001/,由於還沒有Eureka客戶端將服務註冊進來,所以Eureka列表是空的。**
三、eureka-provider(服務提供者)
1、pom.xml
和eureka-server的依賴基本相同,只有一處換成spring-cloud-starter-netflix-eureka-client。
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cloud</groupId>
<artifactId>eureka-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-provider</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/>
</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>
<dependencyManagement>
<!-- Spring Cloud版本 -->
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<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>
<!-- 引入服務註冊發現組件 eureka依賴 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、application.yml
spring:
application:
name: eureka-provider
server:
port: 8002
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/ #服務註冊中心的Url
3、HelloController
編寫HelloController,對外提供一些REST服務。
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(String name) {
return name + ", Welcome to Spring Cloud Eureka";
}
}
4、EurekaProviderApplication
在啓動類上添加@EnableDiscoveryClient 註解,表明這是一個Eureka客戶端。
@SpringBootApplication
@EnableDiscoveryClient //啓用服務註冊與發現
public class EurekaProviderApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaProviderApplication.class, args);
}
}
5、EnableDiscoveryClient與EnableEurekaClient的區別
- @EnableDiscoveryClient註解是基於spring-cloud-commons依賴,並且在classpath中實現。
- @EnableEurekaClient註解是基於spring-cloud-netflix依賴,只能爲eureka作用。
- @EnableEurekaClient只適用於Eureka作爲註冊中心,@EnableDiscoveryClient 可以是其他註冊中心。
相同點:都用於SpringCloud的服務發現功能。
官方建議使用EnableDiscoveryClient。
四、eureka-consumer(服務消費者)
1、pom.xml
和eureka-provider的依賴基本相同。
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cloud</groupId>
<artifactId>eureka-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-consumer</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencyManagement>
<!-- Spring Cloud版本 -->
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<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>
<!-- 引入服務註冊發現組件 eureka依賴 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、application.yml
spring:
application:
name: eureka-consumer
server:
port: 8003
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/ #服務註冊中心的Url
3、EurekaConsumerApplication
在啓動類上加@EnableDiscoveryClient註解,表明這是一個Eureka客戶端。
@SpringBootApplication
@EnableDiscoveryClient //啓用服務註冊與發現
public class EurekaConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaConsumerApplication.class, args);
}
}
4、TestController
@RestController
public class TestController {
@Autowired
RestTemplate restTemplate;
@LoadBalanced
@Bean
public RestTemplate rest() {
return new RestTemplate();
}
@RequestMapping("/testHello")
public String hello(String name) {
return restTemplate.getForObject("http://eureka-provider/hello?name=" + name, String.class);
}
}
5、RestTemplate和 @LoadBalanced
使用了RestTemplate並且開啓了客戶端負載均衡功能,開啓負載均衡很簡單,只需要在RestTemplate的bean上再添加一個@LoadBalanced註解即可。
五、項目演示
1、先啓動服務註冊中心(eureka-server),以提供服務的註冊。
2、再啓動服務提供者(eureka-provider),將服務註冊到服務註冊中心,以便服務消費者進行調用。
啓動成功後,我們也可以從控制檯中看到註冊成功的消息。
Registered Applications size is zero : true
Application version is -1: true
Getting all instance registry info from the eureka server
The response status is 200
Starting heartbeat executor: renew interval is: 30
InstanceInfoReplicator onDemand update allowed rate per min is 4
Discovery Client initialized at timestamp 1577329890774 with initial instances count: 0
Registering application EUREKA-PROVIDER with eureka with status UP
Saw local status change event StatusChangeEvent [timestamp=1577329890780, current=UP, previous=STARTING]
DiscoveryClient_EUREKA-PROVIDER/localhost:eureka-provider:8002: registering service...
Tomcat started on port(s): 8002 (http) with context path ''
Updating port to 8002
Started EurekaProviderApplication in 6.562 seconds (JVM running for 7.765)
服務註冊中心的註冊信息:
3、最後啓動服務消費者(eureka-consumer),消費其服務註冊中心的服務。
4、服務全部啓動成功。
5、服務消費者發送請求,調用服務提供者在服務註冊中心的註冊服務。
到此,調用服務通了!感興趣的話可以去學習下。
想要demo源碼的可以留下郵箱地址。