开源地址
https://gitee.com/liupan1230/spring-series
正文
springcloud提供的微服务调用组建由两个,ribbon与feign,我们先说说两者之间的区别,就可以看出后面为什么我选择使用feign,而不使用ribbon
Ribbon和Feign都是用于调用其他服务的,不过方式不同。
1.启动类使用的注解不同,Ribbon用的是@RibbonClient,Feign用的是@EnableFeignClients。
2.服务的指定位置不同,Ribbon是在@RibbonClient注解上声明,Feign则是在定义抽象方法的接口中使用@FeignClient声明。
3.调用方式不同,Ribbon需要自己构建http请求,模拟http请求然后使用RestTemplate发送给其他服务,步骤相当繁琐。
Feign则是在Ribbon的基础上进行了一次改进,采用接口的方式,将需要调用的其他服务的方法定义成抽象方法即可,
不需要自己构建http请求。不过要注意的是抽象方法的注解、方法签名要和提供服务的方法完全一致。
光看文字可能看不出啥,我们用实例来说明
先在生产者里面暴露一个接口
import com.lp.common.ResultJson;import org.springframework.web.bind.annotation.*;/** * 测试controller */@RestController@RequestMapping("/test")public class TestController { @PostMapping public ResultJson saveTest(@RequestParam String txt) { System.out.println(txt); return new ResultJson(txt); }}
接下来我们分别用ribbon与feign去调用这个服务的接口
先看ribbon
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate;/** * 测试controller */@RestController@RequestMapping("/test")public class TestController { @Autowired private RestTemplate restTemplate; @PostMapping public String test(@RequestParam String txt) { return restTemplate.postForEntity("http://lp-service/service/test?txt={0}", null, String.class, txt).getBody(); }}
注意,我们在生产者返回的是ResultJson类,但是我们用了RestController注解,那么返回的数据是josn字符串,所以这里只能用String来接收,同时根据接口类型的不同,需要用不同的方法去调用,模拟http请求然后使用RestTemplate发送给其他服务,具体的不在此多做展示,这里我们注意一点,那就是参数的传递
接下来我们看feign
先创建一个接口
import com.lp.common.ResultJson;import org.springframework.cloud.openfeign.FeignClient;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;/** * 微服务调用接口 */@RequestMapping("service")//这里为服务的名字@FeignClient("lp-service")public interface TestService { @PostMapping("test") ResultJson saveTest(@RequestParam String txt);}
再创建接口
import com.lp.common.ResultJson;import com.lp.service.TestService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;/** * 测试controller */@RestController@RequestMapping("/test")public class TestController { @Autowired private TestService testService; @PostMapping public ResultJson saveTest( @RequestParam String txt) { return testService.saveTest(txt); }}
用feign看上去代码好像要多点,但是,从整体上看,代码更加的简洁也更清楚明了,在接口里面,我们只是有保证和生产者暴露的接口保持一致即可,甚至可以直接复制,删掉里面的实现方法即可。至于其他启动配置就不说了,这个都是需要的,更多的可以下载源代码查看
日常求赞、求分享、求点亮在看
以上就是这篇文章的全部内容了。
各位如果这个文章写得还不错, 求点赞? 求关注❤️ 求分享? 点亮在看? , 创作不易,各位的支持和认可,就是我创作的最大动力,我们下篇文章见!
南城 | 文 【原创】【转载请联系本人】 如果本篇博客有任何错误,请批评指教,不胜感激 !!!
文章中出现的广告为系统自动配送,与本人无关,如果您方便的话,还请动动手指,点一下,为我增加一点点点微薄的收入,谢谢
个人微信
个人微信,欢迎一起讨论技术,向往生活