上一篇是分享了springcloud-框架搭建(一)(基于Ribbon+RestTemplate),这一篇分享下 springcloud框架另一种服务间通信方式,基于Feign
开始前解释什么是feign?
Feign是一个声明式WebService客户端.使用Feign能让编写WebService客户端更加简单,它的使用方法是定义一个接口,然后在上面添加注解,同时也支持JAX-RS标准的注解.Feign也支持可拔插式的编码器和解码器.Spring Cloud对Feign进行了封装,使其支持了Spring MVC标准注解和HttpMessageConverters.Feign可以与Eureka和Ribbon组合使用以支持负载均衡
本篇是代码是基于上篇基础上写的
一、在消费端增加feign模块
1、pom.xml引入feign包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2、在 ConsumerApplication 中加入注释 @EnableFeignClients
3、增加feign服务
package com.test.consumer.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Component
@FeignClient("PRODUCT-SERVICE")
public interface HelloFeignService {
/**
* @param name 参数一定要加注释, 不然不识别
* @return
*/
@GetMapping(value = "/hello/say")
String sayHello(@RequestParam("name") String name);
/**
* @param name 参数一定要加注释, 不然不识别
* @return
*/
@GetMapping(value = "/feign/test")
String feignTest(@RequestParam("name") String name);
}
4、增加feign测试的controller
package com.test.consumer.feign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
private HelloFeignService helloFeignService;
/**
*
* @param name 参数一定要加注释, 不然不识别
* @return
*/
@GetMapping(value = "/hello")
public String doHello(@RequestParam("name") String name){
return helloFeignService.sayHello(name);
}
/***
*
* @param name 参数一定要加注释, 不然不识别
* @return
*/
@GetMapping("/test")
public String feignTest(@RequestParam("name") String name){
return helloFeignService.feignTest(name);
}
}
二、生产者端增加feign专用的controller
package com.test.service.feign;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloFeignController
{
/**
*
* @param name 参数一定要加注释, 不然不识别
* @return
*/
@GetMapping("/feign/test")
public String HelloFeignSay(@RequestParam("name") String name){
return " this is feign test,name is " + name ;
}
}
三、启动eureka、生产者、消费端测试
访问 http://localhost:8020/test?name=2323 返回结果如下
符合预期
总结:
springcloud 两种跨服务调用都分享完了, 这两种调用方式的区别是:
在预订微服务中,有一个同步呼叫票价(Fare)。RestTemplate
是用来制作的同步呼叫。使用RestTemplate时
,URL参数是以编程方式构造的,数据被发送到其他服务。在更复杂的情况下,我们将不得不RestTemplate
深入到更低级别的API提供的甚至是API 的细节。
Feign是Spring Cloud Netflix库,用于在基于REST的服务调用上提供更高级别的抽象。Spring Cloud Feign在声明性原则上工作。使用Feign时,我们在客户端编写声明式REST服务接口,并使用这些接口来编写客户端程序。开发人员不用担心这个接口的实现。这将在运行时由Spring动态配置。通过这种声明性的方法,开发人员不需要深入了解由HTTP提供的HTTP级别API的细节的RestTemplate
。
总之 FeignClient简化了请求的编写,且通过动态负载进行选择要使用哪个服务进行消费,而这一切都由Spring动态配置实现,我们不用关心这些,只管使用方法即可。再说,就是简化了编写,
RestTemplate还需要写上服务器IP这些信息等等,而FeignClient则不用
码源如下
https://gitee.com/xing_xin/springclouddemo-1.git