目錄
- Feign
- 使用示例
- 1.匯入依賴:
- 2.新建Feign Interface
- 3.創建服務消費者
- 4.測驗
- 補充:
- 使用示例
??上一篇介紹微服務構建起來后,使用Ribbon來解決多個服務的負載均衡問題, Spring Cloud認知學習(二):Ribbon使用
??這一篇介紹一個用于宣告式呼叫服務的組件Fegin,主要用于解決前面的服務呼叫與restTemplate耦合緊密的問題,
Feign
??Feign用于宣告式呼叫服務
??在上面的服務呼叫中,我們始侄訓是沒有擺脫restTemplate,我們呼叫別的服務始終要使用restTemplate來發起,想想我們以前是怎么開發的(三層架構,controller呼叫service,service呼叫dao),controller呼叫service,feign就是為這種面向介面化編程需求而產生的,為什么說他能面向介面化編程呢?我們下面來演示,

使用示例
代碼參考:Feign簡單使用實驗
考慮到服務可能有多個消費者,所以我們把共有的代碼寫到spring-cloud-common-data中,這樣所有的消費者都可以通過繼承這個依賴包來獲取Feign修飾的介面,
1.匯入依賴:
在spring-cloud-common-data中匯入依賴:
<dependencies>
<!--增加feign依賴 start-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<!--舊版本的-->
<!--<artifactId>spring-cloud-starter-feign</artifactId>-->
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--增加feign依賴 end-->
</dependencies>
2.新建Feign Interface
在spring-cloud-common-data創建一個interface:
package com.progor.study.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.Map;
// 由于這種服務的服務消費者可能比較多,放到共有依賴中,
@FeignClient(value = "https://www.cnblogs.com/progor/p/MESSAGESERIVE")
public interface MessageService {
// 這里使用RequestMapping將服務提供者的方法與本地Service方法建立映射
@RequestMapping(value = "https://www.cnblogs.com/msg/list", method = RequestMethod.GET)
Map<String, String> list();
}
??@FeignClient用來配置資訊,可以配置當前interface對應哪個服務,搭配@RequestMapping的效果就是呼叫對應服務的對應API介面,上面代碼的效果就是從eureka中獲取名為MESSAGESERIVE的服務的服務示例,呼叫服務實體的/msg/list路由方法,
3.創建服務消費者
3.1 在服務消費者spring-cloud-user-consumer-80中創建一個MessageController2,注入MessageService:
package com.progor.study.Controller;
import com.progor.study.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.Map;
// 這個控制器用來處理使用fegin的情況
@RestController
public class MessageController2 {
// 注入MessageService
@Autowired
private MessageService messageService;
@GetMapping("/msg2/list")
public Map<String, String> list() {
return messageService.list();
}
}
3.2.在spring-cloud-user-consumer-80啟用Feign:
??由于在spring-cloud-common-data中匯入了fegin依賴,所以這里不需要再匯入了,
@SpringBootApplication
@EnableEurekaClient
@RibbonClient(name = "USERSERIVE", configuration = MyConfig.class)
@EnableFeignClients // 使用feign
public class UserConsumer80Application {
public static void main(String[] args) {
SpringApplication.run(UserConsumer80Application.class, args);
}
}
4.測驗
4.1:啟動spring-cloud-eureka-server-7001,spring-cloud-message-service-8004,spring-cloud-message-service-8005
4.2:訪問http://localhost/msg2/list,這個URL呼叫的是MessageController2的API,而MessageController2里面呼叫的是被Fegin封裝的MessageService的方法,
如果能訪問成功,那說明了是我們使用Feign成功了,
從上面可以看出,使用了Feign之后,我們可以像以前一樣呼叫Service來呼叫業務邏輯了,
補充:
- Feign默認是有負載均衡的,看一下
spring-cloud-starter-openfeign依賴包,你會發現它有匯入依賴spring-cloud-starter-netflix-ribbon - 更多內容包括其作業原理,將會單章講解,咕咕咕,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/194749.html
標籤:Java
上一篇:Java GUI編程
下一篇:Java8的List過濾
