SpringCloud之Feign遠(yuǎn)程接口映射的實(shí)現(xiàn)
一.簡介
SpringCloud是基于Restful的遠(yuǎn)程調(diào)用框架,引入Ribbon負(fù)載均衡組件后還需要客戶端使用RestTemplate調(diào)用遠(yuǎn)程接口,操作起來還顯得繁瑣。SpringCloud提供了遠(yuǎn)程接口映射,將遠(yuǎn)程Restful服務(wù)映射為遠(yuǎn)程接口,消費(fèi)端注入遠(yuǎn)程接口即可實(shí)現(xiàn)方法調(diào)用。
二.流程
1.新建遠(yuǎn)程接口映射模塊service-api,并引入Feign接口映射依賴
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
</dependencies>
2.編寫接口映射接口
package com.vincent.service;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient("SERVICE-USER")
@RequestMapping("/service-user")
public interface IUserService {
@GetMapping("/detail")
Object detail(@RequestParam("id") Integer id);
}
3.配置消費(fèi)端application.yml
server: port: 9001 eureka: client: service-url: defaultZone: http://localhost:7001/service-eureka/eureka register-with-eureka: false
4.消費(fèi)端添加映射模塊依賴
<dependency> <groupId>com.vincent</groupId> <artifactId>service-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
5.客戶端注入需要使用的服務(wù)接口映射
package com.vincent.controller;
import com.vincent.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
private IUserService userService;
@GetMapping("/detail")
public Object detail(Integer id){
return this.userService.detail(id);
}
}
5.編寫消費(fèi)端啟動(dòng)類
package com.vincent;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients({"com.vincent.service"})
@EnableEurekaClient
public class ConsumerApp {
public static void main(String[] args) {
SpringApplication.run(ConsumerApp.class,args);
}
}
@EnableFeignClients 定義Feign接口映射掃描包,IOC容器會(huì)自動(dòng)創(chuàng)建接口實(shí)現(xiàn)類
6.訪問 http://localhost:9001/detail?id=1

三.總結(jié)
Feign接口映射服務(wù)端Restful接口會(huì)自動(dòng)依賴Ribbon組件,實(shí)現(xiàn)客戶端負(fù)載均衡。使用接口調(diào)用消費(fèi)端遠(yuǎn)程接口就像調(diào)用本地方法一樣。
到此這篇關(guān)于SpringCloud之Feign遠(yuǎn)程接口映射的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringCloud Feign遠(yuǎn)程接口映射內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java中你真的會(huì)用Constructor構(gòu)造器嗎之看完本篇你就真的會(huì)了
顯式初始化要求我們?cè)趯懗绦驎r(shí)就確定初始值,這有時(shí)很不方便。我們可以使用構(gòu)造器(constructor)來初始化對(duì)象。構(gòu)造器可以初始化數(shù)據(jù)成員,還可以規(guī)定特定的操作。這些操作會(huì)在創(chuàng)建對(duì)象時(shí)自動(dòng)執(zhí)行。下面文字將對(duì)該內(nèi)容做詳細(xì)介紹,需要的小伙伴請(qǐng)參考2021-09-09
java代碼獲取jenkins數(shù)據(jù),構(gòu)建歷史等信息方式
這篇文章主要介紹了java代碼獲取jenkins數(shù)據(jù),構(gòu)建歷史等信息方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-05-05
Java基于正則實(shí)現(xiàn)的日期校驗(yàn)功能示例
這篇文章主要介紹了Java基于正則實(shí)現(xiàn)的日期校驗(yàn)功能,涉及java文件讀取、日期轉(zhuǎn)換及字符串正則匹配相關(guān)操作技巧,需要的朋友可以參考下2017-03-03
Java中Velocity快速對(duì)變量中的引號(hào)特殊字符進(jìn)行轉(zhuǎn)義
Velocity是一個(gè)基于Java的模板引擎,與Freemarker類似,這篇文章主要介紹了Java中Velocity如何對(duì)變量中的引號(hào)特殊字符進(jìn)行轉(zhuǎn)義,主要記錄一下在使用中碰到的要對(duì)引號(hào)特殊字符進(jìn)行轉(zhuǎn)義的問題,需要的朋友可以參考下2023-07-07

