0
点赞
收藏
分享

微信扫一扫

Spring Cloud Feign


声明式服务调用:Spring Cloud Feign

Feign整合了Spring Cloud Ribbon 与Spring Cloud Hystrix,同时提供了一种声明式的Web服务客户端定义方式。
所有通过Feign我们可以通过注解的方式 快速的调用服务,并且自动回开启客户端负载均衡。
简单地说 Feign能够对RestTemplate进行进步封装。

#######传统的方式###############
restTemplate.getForEntity("http://springcloud-eureka-feign-user/hello?name={1}",String.class,"luo");

##########Feign的方式###########
@FeignClient("springcloud-eureka-feign-user")
public interface IUserService {
@RequestMapping("/hello")
public String helloService(String id);
}

Feign参数的绑定

  • @Request 携带request参数
  • @RequestHead 携带Header信息
  • @RequestBody 包含对象的请求

接收端

@RequestMapping(value ="/hello/{id}",method = RequestMethod.GET)
public String helloservie(@PathVariable String id)
{
return "helloword"+id;
}


@RequestMapping(value ="/hello1",method = RequestMethod.GET)
public String helloservie1(@RequestParam String id)
{
return "helloword"+id;
}


@RequestMapping(value ="/hello2",method = RequestMethod.GET)
public String helloservie2(@RequestHeader String id)
{
return "helloword"+id;
}


@RequestMapping(value ="/hello3",method = RequestMethod.POST)
public String helloservie3(@RequestBody UserDto userDto)
{
return "helloword"+userDto.getName();
}

发送端

@FeignClient(value = "springcloud-eureka-feign-user",configuratin=类名.class)的方式实现对Hystrix的自定义配置

/**
* 通过指定服务名来绑定服务
*用其实现类作为其降级服务类
*/
@FeignClient(value = "springcloud-eureka-feign-user",fallback= UserServiceImpl.class)
public interface IUserService {

@RequestMapping( value = "/hello/luo" ,method = RequestMethod.GET)
public String helloService();

@RequestMapping(value = "/hello1",method = RequestMethod.GET)
public String helloService1(@RequestParam("id") String id);

@RequestMapping(value = "/hello2",method = RequestMethod.GET)
public String helloService2(@RequestHeader("id") String id);

@RequestMapping(value = "/hello3",method = RequestMethod.POST)
public String helloService3(@RequestBody UserDto userDto);
}

请求压缩
Spring Cloud Feign支持对请求与响应进行GZIP压缩,以减少通信过程中的性能损耗。

##############Feign配置#####################
#请求压缩
feign.compression.request.enable=true
feign.compression.response.enable=true
feign.compression.request.mime-types=text/xml,application/xml,application/json
feign.compression.request.min-request-size=2048

Spring Cloud Feign_Spring Cloud Feign


举报

相关推荐

0 条评论