一、服務的注冊與發現
關系調用說明:
- 服務生產者啟動時,向服務注冊中心注冊自己提供的服務
- 服務消費者啟動時,在服務注冊中心訂閱自己所需要的服務
- 注冊中心返回服務提供者的地址信息個消費者
- 消費者從提供者中調用服務
二、Eureka簡介
Eureka是Spring Cloud Netflix微服務套件中的一部分,可以與Springboot構建的微服務很容易的整合起來。
Eureka包含了服務器端和客戶端組件。服務器端,也被稱作是服務注冊中心,用於提供服務的注冊與發現。Eureka支持高可用的配置,當集群中有分片出現故障時,Eureka就會轉入自動保護模式,它允許分片故障期間繼續提供服務的發現和注冊,當故障分片恢復正常時,集群中其他分片會把他們的狀態再次同步回來。
客戶端組件包含服務消費者與服務生產者。在應用程序運行時,Eureka客戶端向注冊中心注冊自身提供的服務並周期性的發送心跳來更新它的服務租約。同時也可以從服務端查詢當前注冊的服務信息並把他們緩存到本地並周期性的刷新服務狀態。
三、使用Eureka進行服務治理
1. 搭建服務注冊中心
單獨創建一個Springboot項目,並添加如下的依賴
<dependencies>
<!--添加Eureka服務器端依賴-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
在Springboot項目中的main入口,添加@EnableEurekaServer注解,來開啟服務注冊中心
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
在默認情況下,服務注冊中心也會把自己當做是一個服務,將自己注冊進服務注冊中心,所以我們可以通過配置來禁用他的客戶端注冊行為,在application.properties中添加如下配置:
server.port=3333
eureka.instance.hostname=localhost
#不要向注冊中心注冊自己
eureka.client.register-with-eureka=false
#禁止檢索服務
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka
啟動應用,並訪問http://localhost:3333/即可看到Eureka信息面板,如下:
從上圖看到,在"Instances currently registered with Eureka"信息中,沒有一個實例,說明目前還沒有服務注冊。
2. 注冊服務
基於文章[微服務系列] 微服務構建框架--Spring Boot中構建的第一個Springboot項目(在附件中的springbootdemo.zip實例),來進行改造。首先在pom文件中添加Eureka客戶端相關的依賴:
<dependencies>
<!---->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.RS5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
在之前的TestRestful方法中添加如下代碼,將org.springframework.cloud.client.discovery.DiscoveryClient;對象注入,並且在日志中打印出與服務相關的一些信息。
@RestController
public class TestRestful {
private final Logger logger=Logger.getLogger(getClass());
@Autowired
private DiscoveryClient client;
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String home(){
ServiceInstance instance=client.getLocalServiceInstance();
logger.info("serviceId"+instance.getServiceId()+"host:post="+instance.getHost()+":"+instance.getPort());
return "hello spring";
}
}
在主類上添加@EnableEurekaClient注解以實現Eureka中的DiscoveryClient實現。
@EnableEurekaClient
@SpringBootApplication
public class SpringbootdemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootdemoApplication.class, args);
}
}
最后在配置文件application.yml中配置與服務相關的一些基本信息,如服務名、注冊中心地址(即上一節中設置的注冊中心地址http://localhost:3333/eureka)
# 設置服務名
spring:
application:
name: hello-service
# 設置注冊中心地址
eureka:
client:
service-url:
defaultZone: http://localhost:3333/eureka
3. 測試
- 啟動注冊中心服務
- 啟動springbootdemo項目,可以看到如下的信息,說明此服務已經注冊在了注冊中心
同時訪問http://localhost:3333/可以在Instances currently registered with Eureka中看到已經有一個服務注冊了進來,並且名稱為HELLO-SERVICE的服務
- 訪問http://localhost:8088/hello可以在控制台中看到如下日志信息:
四、項目代碼截圖
基於Eureka的服務治理
注:本文著作權歸作者,由demo大師代發,拒絕轉載,轉載需要作者授權