SpringCloud系列——Feign 服務呼叫
前言
前面我們已經實現了服務的註冊與發現(請戳: ofollow,noindex" target="_blank">SpringCloud系列——Eureka 服務註冊與發現 ),並且在註冊中心註冊了一個服務myspringboot,本文記錄多個服務之間使用Feign呼叫。
Feign是一個宣告性web服務客戶端。它使編寫web服務客戶機變得更容易,本質上就是一個http,內部進行了封裝而已。
GitHub/">GitHub地址: https://github.com/OpenFeign/feign
服務提供者
提供者除了要在註冊中心註冊之外,不需要引入其他東西,注意一下幾點即可:
1、如果使用物件接參,必須使用@RequestBody,否則接不到資料
2、接參只能出現一個複雜物件,例:public Result<List<UserVo>> list(@RequestBody UserVo entityVo) { ... }
3、提供者如果又要向其他消費者提供服務,又要向瀏覽器提供服務,建議保持原先的Controller,新建一個專門給消費者的Controller
服務消費者
消費者maven引入jar
<!-- feign --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
配置檔案
對日期的解析,消費者要跟提供者一致,不然會報json解析錯誤
#超時時間 feign.httpclient.connection-timeout=30000 #mvc接收引數時對日期進行格式化 spring.mvc.date-format=yyyy-MM-dd HH:mm:ss #jackson對響應回去的日期引數進行格式化 spring.jackson.date-format=yyyy-MM-dd HH:mm:ss spring.jackson.time-zone=GMT+8
服務呼叫
1、springdatejpa 應用名稱,是服務提供者在eureka註冊的名字,Feign會從註冊中心獲取例項
2、如果不想啟動eureka服務,直連本地開發:@FeignClient(name = "springdatejpa", path = "/user/",url = "http://localhost:10086")
3、如果使用@RequestMapping,最好指定呼叫方式
4、消費者的返回值必須與提供者的返回值一致,引數物件也要一致
更多@FeignClient註解引數配置,請參閱官方文件
@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); }
/** * controller呼叫 */ @GetMapping("feign/list") Result<List<UserVo>> list(UserVo userVo){ return myspringbootFeign.list(userVo); }
啟動類
啟動類加入註解:@EnableFeignClients
@EnableEurekaClient @EnableFeignClients @SpringBootApplication public class MyspringbootApplication{ public static void main(String[] args) { SpringApplication.run(MyspringbootApplication.class, args); } }
效果
成功註冊兩個服務
成功呼叫
報錯記錄
當我將之前搭好的一個springboot-springdata-jpa整合專案在eureka註冊時出現了一個報錯
然後在網上查了下說是因為springboot版本問題(請戳: http://www.cnblogs.com/hbbbs/articles/8444013.html ),之前這個專案用的是2.0.1.RELEASE,現在要在eureka註冊,pom引入了就出現了上面的報錯
<!-- 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>
解決:升級了springboot版本,2.1.0,專案正常啟動
<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>