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
分类:
乐优商城项目
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)