服務(wù)注冊與發(fā)現(xiàn)

服務(wù)注冊與發(fā)現(xiàn)

? 微服務(wù)是有多個獨立的微小服務(wù)構(gòu)成,需要有一個有效的機制對架構(gòu)中所有的服務(wù)進行管理監(jiān)控,了解他們的狀態(tài)。就像體檢單一樣知道我們"身體各個器官"的狀態(tài)。這樣的機制是什么呢?

? 服務(wù)注冊與發(fā)現(xiàn)就是可以幫助我們很好管理每個微服務(wù)的機制,服務(wù)注冊是指向服務(wù)注冊中注冊一個服務(wù)實例,服務(wù)提供者將自己的信息告知服務(wù)注冊中心。服務(wù)發(fā)現(xiàn)是指一個服務(wù)需要消費另一個服務(wù)時 ,服務(wù)注冊中心給他消費服務(wù)的信息,如ip地址。這樣我們就可以掌握每個服務(wù)實例狀態(tài),同時服務(wù)間的消費也可以交給服務(wù)注冊中心。

? 在springCloud的中提供了Enable組件,方便我們搭建服務(wù)注冊中心及每個微服務(wù)進行服務(wù)注冊及服務(wù)消費。

  1. 服務(wù)注冊 - 服務(wù)運行向注冊中心注冊服務(wù),提供ip、端口、接口信息
  2. 服務(wù)續(xù)約 - 服務(wù)提供者和 注冊中心 定時保持通訊,間隔時間為30s,確保服務(wù)可正常調(diào)用
  3. 服務(wù)獲取 - 服務(wù)消費者從 注冊中心 上每30s進行服務(wù)清單同步。服務(wù)清單有服務(wù)提供者訪問連接等信息
  4. 服務(wù)訪問 - 服務(wù)消費者使用清單中的信息去訪問獲取服務(wù)提供者提供的資源
image.png

eureka作為服務(wù)注冊中心

? 服務(wù)注冊和服務(wù)發(fā)現(xiàn)是配套進行使用的,使用eureka作為注冊中心,服務(wù)提供者需要使用eureka client 作為配套使用

服務(wù)注冊中心 - eureka server

pom依賴
  • Spring-cloud-starter-netfix-eureka-server
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
 </dependency>
聲明 : 使用該聲明就表示該項目為eureka 服務(wù)中心
  • @EnableEurekaServer
@SpringBootApplication
@EnableEurekaServer
public class UserMemberApplication {
   public static void main(String[] args) {
      SpringApplication.run(UserMemberApplication.class, args);
   }
}
application.yml 配置
  • 默認端口 8761
  • server 不能注冊到服務(wù)中

在工程的配置文件進行服務(wù)名、端口號

# 注冊中心端口號
server.port=8761
# 本身不進行服務(wù)注冊
eureka.client.fetch-registry=false
# 不獲取注冊相關(guān)內(nèi)容
eureka.client.register-with-eureka=false
總結(jié)

eureka 注冊中心使用步驟

  1. pom文件依賴引入
  2. 啟動類聲明該項目為eureka服務(wù)注冊中心
  3. 配置端口,server本身不進行注冊

eureka client 服務(wù)提供者

pom 依賴
  • Spring-cloud-start-netfix-eureka-client
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
聲明
  • @EnableEurekaClient : eureka 提供的注解
  • @EnableDiscoveryClient : springCloud 提供的服務(wù)發(fā)現(xiàn)注解,建議使用
@SpringBootApplication
@EnableEurekaClient
public class UserApplication{
   public static void main(String[] args) {
      SpringApplication.run(UserApplication.class, args);
   }
}
配置
  • 該服務(wù)端口
  • 服務(wù)名稱
  • 注冊中心地址
#該服務(wù)端口  
server.port= 10082
#該服務(wù)命名
spring.application.name= user-server
# 服務(wù)注冊中心地址配置
eureka.client.service-url.defaultZone= http://localhost:10086/eureka/

啟動該服務(wù),服務(wù)注冊中心出現(xiàn)該服務(wù)

image.png

使用Feign 訪問服務(wù)

? Feign 是聲明式的Result web 服務(wù)客戶端,使用該組件可先調(diào)用方法一樣使用其他服務(wù)

創(chuàng)建 feign工程

創(chuàng)建一個spring-boot工程,工程名稱為 user-member,引入feign、eureka的起步依賴

pom依賴
  • Euraka-client 依賴
  • Openfeign 依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
application.yml 配置
# 服務(wù)端口
server:
  port: 10083

#服務(wù)注冊中心地址
eureka:
  client:
      service-url:
          defaultZone: http://localhost:10086/eureka/

#服務(wù)名稱
spring:
  application:
      name: user-memeber
啟動類注解

使用 EnableFeignClients 開啟 feign功能

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class UserMemberApplication {

public static void main(String[] args) {
SpringApplication.run(UserMemberApplication.class, args);
}

}
定義 feign接口

使用 @FeignClient 注解,@@FeignClient 注解中使用value指定調(diào)用的服務(wù),同時調(diào)用對應(yīng)接口,使用的是springMVC相同的注解

@FeignClient(value = "user-server")
public interface UserClient {

      @GetMapping("/hello")
      String hello();
}
使用feign接口

使用注入的方式進行feignClient調(diào)用

@RestController
@SuppressWarnings("all")
public class UserMemberController {

  @Autowired
  UserClient memberClient;

  //測試fign調(diào)用
  @GetMapping("/hello")
  public String hello(){
  
      return memberClient.hello();
  }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容