SpringCloud Feign 服務(wù)調(diào)用的實(shí)現(xiàn)
前言
前面我們已經(jīng)實(shí)現(xiàn)了服務(wù)的注冊(cè)與發(fā)現(xiàn)(請(qǐng)戳:SpringCloud系列——Eureka 服務(wù)注冊(cè)與發(fā)現(xiàn)),并且在注冊(cè)中心注冊(cè)了一個(gè)服務(wù)myspringboot,本文記錄多個(gè)服務(wù)之間使用Feign調(diào)用。
Feign是一個(gè)聲明性web服務(wù)客戶端。它使編寫(xiě)web服務(wù)客戶機(jī)變得更容易,本質(zhì)上就是一個(gè)http,內(nèi)部進(jìn)行了封裝而已。
GitHub地址:https://github.com/OpenFeign/feign
服務(wù)提供者
提供者除了要在注冊(cè)中心注冊(cè)之外,不需要引入其他東西,注意一下幾點(diǎn)即可:
1、如果使用對(duì)象接參,必須使用@RequestBody,否則接不到數(shù)據(jù)
2、接參只能出現(xiàn)一個(gè)復(fù)雜對(duì)象,例:public Result<List<UserVo>> list(@RequestBody UserVo entityVo) { ... }
3、提供者如果又要向其他消費(fèi)者提供服務(wù),又要向?yàn)g覽器提供服務(wù),建議保持原先的Controller,新建一個(gè)專(zhuān)門(mén)給消費(fèi)者的Controller
測(cè)試接口
@RestController
@RequestMapping("/user/")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("list")
public Result<List<UserVo>> list(@RequestBody UserVo entityVo) {
return userService.list(entityVo);
}
@RequestMapping("get/{id}")
public Result<UserVo> get(@PathVariable("id") Integer id) {
return userService.get(id);
}
}
服務(wù)消費(fèi)者
消費(fèi)者maven引入jar
<!-- feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
配置文件
對(duì)日期的解析,消費(fèi)者要跟提供者一致,不然會(huì)報(bào)json解析錯(cuò)誤
#超時(shí)時(shí)間 feign.httpclient.connection-timeout=30000 #mvc接收參數(shù)時(shí)對(duì)日期進(jìn)行格式化 spring.mvc.date-format=yyyy-MM-dd HH:mm:ss #jackson對(duì)響應(yīng)回去的日期參數(shù)進(jìn)行格式化 spring.jackson.date-format=yyyy-MM-dd HH:mm:ss spring.jackson.time-zone=GMT+8
服務(wù)調(diào)用
1、springdatejpa 應(yīng)用名稱,是服務(wù)提供者在eureka注冊(cè)的名字,F(xiàn)eign會(huì)從注冊(cè)中心獲取實(shí)例
2、如果不想啟動(dòng)eureka服務(wù),直連本地開(kāi)發(fā):@FeignClient(name = "springdatejpa", path = "/user/",url = "http://localhost:10086")
3、如果使用@RequestMapping,最好指定調(diào)用方式
4、消費(fèi)者的返回值必須與提供者的返回值一致,參數(shù)對(duì)象也要一致
更多@FeignClient注解參數(shù)配置,請(qǐng)參閱官方文檔
@FeignClient(name = "springdatejpa", path = "/user/")
public interface MyspringbootFeign {
@RequestMapping(value = "get/{id}")
Result<UserVo> get(@PathVariable("id") Integer id);
@RequestMapping(value = "list", method = RequestMethod.GET)
Result<List<UserVo>> list(@RequestBody UserVo entityVo);
}
/**
* feign調(diào)用
*/
@GetMapping("feign/get/{id}")
Result<UserVo> get(@PathVariable("id") Integer id){
return myspringbootFeign.get(id);
}
/**
* feign調(diào)用
*/
@GetMapping("feign/list")
Result<List<UserVo>> list(UserVo userVo){
return myspringbootFeign.list(userVo);
}
啟動(dòng)類(lèi)
啟動(dòng)類(lèi)加入注解:@EnableFeignClients
@EnableEurekaClient
@EnableFeignClients
@SpringBootApplication
public class MyspringbootApplication{
public static void main(String[] args) {
SpringApplication.run(MyspringbootApplication.class, args);
}
}
效果
成功注冊(cè)兩個(gè)服務(wù)

成功調(diào)用


報(bào)錯(cuò)記錄
1、啟動(dòng)時(shí)報(bào)了個(gè)SQL錯(cuò)誤

解決:配置文件連接數(shù)據(jù)時(shí)指定serverTimezone=GMT%2B8

2、當(dāng)我將之前搭好的一個(gè)springboot-springdata-jpa整合項(xiàng)目在eureka注冊(cè)時(shí)出現(xiàn)了一個(gè)報(bào)錯(cuò)

然后在網(wǎng)上查了下說(shuō)是因?yàn)閟pringboot版本問(wèn)題,之前這個(gè)項(xiàng)目用的是2.0.1.RELEASE,現(xiàn)在要在eureka注冊(cè),pom引入了就出現(xiàn)了上面的報(bào)錯(cuò)
<!-- eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- actuator -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
解決:升級(jí)了springboot版本,2.1.0,項(xiàng)目正常啟動(dòng)
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<!--<version>2.0.1.RELEASE</version>-->
<relativePath/> <!-- lookup parent from repository -->
</parent>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- springcloud使用feign調(diào)用服務(wù)時(shí)參數(shù)內(nèi)容過(guò)大問(wèn)題
- springcloud?feign服務(wù)之間調(diào)用,date類(lèi)型轉(zhuǎn)換錯(cuò)誤的問(wèn)題
- SpringCloud中的Feign遠(yuǎn)程調(diào)用接口傳參失敗問(wèn)題
- springcloud本地調(diào)試feign調(diào)用出現(xiàn)的詭異404問(wèn)題及解決
- SpringCloud 服務(wù)負(fù)載均衡和調(diào)用 Ribbon、OpenFeign的方法
- SpringCloud使用Feign實(shí)現(xiàn)服務(wù)調(diào)用
- SpringCloud服務(wù)之間Feign調(diào)用不會(huì)帶上請(qǐng)求頭header的解決方法
相關(guān)文章
SpringBoot中多環(huán)境配置和@Profile注解示例詳解
這篇文章主要介紹了SpringBoot中多環(huán)境配置和@Profile注解,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-01-01
Spring的@Autowired加到接口上但獲取的是實(shí)現(xiàn)類(lèi)的問(wèn)題
這篇文章主要介紹了Spring的@Autowired加到接口上但獲取的是實(shí)現(xiàn)類(lèi)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
Java調(diào)用wsdl接口的兩種方法(axis和wsimport)
本文主要介紹了Java調(diào)用wsdl接口的兩種方法(axis和wsimport),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
Java IO文件編碼轉(zhuǎn)換實(shí)現(xiàn)代碼
這篇文章主要介紹了Java IO文件編碼轉(zhuǎn)換實(shí)現(xiàn)代碼,有需要的朋友可以參考一下2013-12-12
java中l(wèi)ambda(函數(shù)式編程)一行解決foreach循環(huán)問(wèn)題
這篇文章主要介紹了java中l(wèi)ambda(函數(shù)式編程)一行解決foreach循環(huán)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
MybatisPlus #{param}和${param}的用法詳解
這篇文章主要介紹了MybatisPlus #{param}和${param}的用法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
nacos-discovery包名層級(jí)問(wèn)題解決
這篇文章主要為大家介紹了nacos-discovery包名層級(jí)問(wèn)題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07

