010 SpringCloud 学习笔记6-----Feign
1.概述
Feign可以把Rest的请求进行隐藏,伪装成类似SpringMVC的Controller一样。你不用再自己拼接url,拼接参数等等操作,一切都交给Feign去做。
2.入门案例
2.1 服务提供方
user-service模块
server: port: 8201 spring: application: name: user-service datasource: url: jdbc:mysql://localhost:3306/day1?serverTimezone=UTC&useSSL=false username: root password: ****** driver-class-name: com.mysql.cj.jdbc.Driver eureka: client: register-with-eureka: true fetch-registry: true service-url: defaultZone: http://localhost:10086/eureka/
(1)StudentController
package com.hztest.controller; import com.hztest.domain.CommonResult; import com.hztest.domain.Student; import com.hztest.domain.User; import com.hztest.service.StudentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/student") public class StudentController { @Autowired private StudentService studentService; @PostMapping("/getId") public String getId(@RequestParam(value="id") int id) { String idStr = studentService.getStudentId(id); return idStr; } @PostMapping("/getInfo") public String getInfo() { String info = studentService.getTestResult(); return info; } }
(2)StudentService(接口)
package com.hztest.service; public interface StudentService { String getStudentId(int id); String getTestResult(); }
(3)StudentServiceImpl.java
package com.hztest.service.impl; import com.hztest.service.StudentService; import org.springframework.stereotype.Service; @Service public class StudentServiceImpl implements StudentService { @Override public String getStudentId(int id) { return "user-service:"+id; } @Override public String getTestResult() { return "hello feign"; } }
利用postman访问服务提供方
2.2 服务消费方
(1)导入依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
(2)
package com.hztest; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; @EnableFeignClients @EnableDiscoveryClient @SpringBootApplication public class FeignServiceApplication { public static void main(String[] args) { SpringApplication.run(FeignServiceApplication.class, args); } }
(3)使用注解@FeignClient 定义feign客户端
示例 : 该例子定义了一个feign客户端,将远程服务http://user-service/student/getId 映射为一个本地Java方法调用。
package com.hztest.api; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; import org.springframework.web.bind.annotation.*; @FeignClient(name = "user-service", path = "/student") @Component public interface StoreService { @PostMapping("/getInfo") String getInfo(); @PostMapping("/getId") String getId(@RequestParam(value="id") int id); }
(4)使用注解@Autowired使用上面所定义feign的客户端 ;
package com.hztest.controller; import com.hztest.api.StoreService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping(value="/store") public class StoreController { @Autowired StoreService storeService; //这里的使用本地Java API的方式调用远程的Restful接口 @PostMapping("/getStuId") public String getStuId(int id) { String info = "feign-service调用"+storeService.getId(id); return info; } @PostMapping("/getStoreTest") public String getStoreTest() { String info = storeService.getInfo(); return info; } }
利用postman访问服务调用方,服务调用方(feign-service)利用feign-client调用服务提供方(user-service)
参考文献:https://www.jianshu.com/p/62d6c4779c01