今天就跟大家聊聊有關如何使用SpringCloud Feign 調用服務,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
服務提供者
提供者除了要在注冊中心注冊之外,不需要引入其他東西,注意一下幾點即可:
1、如果使用對象接參,必須使用@RequestBody,否則接不到數據
2、接參只能出現一個復雜對象,例:public Result<List<UserVo>> list(@RequestBody UserVo entityVo) { ... }
3、提供者如果又要向其他消費者提供服務,又要向瀏覽器提供服務,建議保持原先的Controller,新建一個專門給消費者的Controller
測試接口
@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);
}
}服務消費者
消費者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);
}/**
* feign調用
*/
@GetMapping("feign/get/{id}")
Result<UserVo> get(@PathVariable("id") Integer id){
return myspringbootFeign.get(id);
}
/**
* feign調用
*/
@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);
}
}效果
成功注冊兩個服務

成功調用


報錯記錄
1、啟動時報了個SQL錯誤

解決:配置文件連接數據時指定serverTimezone=GMT%2B8

2、當我將之前搭好的一個springboot-springdata-jpa整合項目在eureka注冊時出現了一個報錯

然后在網上查了下說是因為springboot版本問題,之前這個項目用的是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>
看完上述內容,你們對如何使用SpringCloud Feign 調用服務有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。