分享

spring cloud-使用feign来消费Restful服务同时加入Ribbon来实现负载均衡

 WindySky 2017-07-24

前言

在前面的示例中,我们消费spring boot提供的Restful服务的时候,使用的是RestTemplate来实现的,实现起来还是比较复杂的,尤其是在消费复杂的Restful服务的时候,还需要进行一系列的转换,编解码等,使用Feign就完全不用考虑这个问题了。

一、Feign简介

Feign是一种声明式、模板化的HTTP客户端。在Spring Cloud中使用Feign, 我们可以做到使用HTTP请求远程服务时能与调用本地方法一样的编码体验,开发者完全感知不到这是远程方法,更感知不到这是个HTTP请求,这整个调用过程和Dubbo的RPC非常类似。开发起来非常的优雅。

二、在spring cloud中添加Feign支持

  1. <dependency>  
  2.     <groupId>org.springframework.cloud</groupId>  
  3.     <artifactId>spring-cloud-starter-feign</artifactId>  
  4. </dependency>  
三、新建Spring Starter Project

建工程的过程在前面的示例中已经重复过很多次了,这里不做冗余的介绍,目录结构如下:

该示例是从前面的

使用RestTemplate消费spring boot的Restful服务

示例改造而来,调用的都是springboot-h2服务

四、新建接口,并实现Feign client

  1. package com.chhliu.springboot.restful.feignclient;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.springframework.cloud.netflix.feign.FeignClient;  
  6. import org.springframework.web.bind.annotation.PathVariable;  
  7. import org.springframework.web.bind.annotation.RequestMapping;  
  8. import org.springframework.web.bind.annotation.RequestMethod;  
  9.   
  10. import com.chhliu.springboot.restful.vo.User;  
  11.   
  12. /** 
  13.  * 描述:调用springboot-h2服务对应的feign 
  14.  *  
  15.  * @author chhliu 创建时间:2017年1月25日 上午9:09:58 
  16.  * @version 1.2.0 
  17.  */  
  18. @FeignClient("springboot-h2")  
  19. public interface UserFeignClient {  
  20.     @RequestMapping(value = "/user/{id}", method = RequestMethod.GET)  
  21.     User findById(@PathVariable("id") Long id);  
  22.       
  23.     @RequestMapping(value="/users", method=RequestMethod.GET)  
  24.     List<User> findAll();  
  25. }  
注意,此处的RequestMapping对应的value是springboot-h2服务提供的Restful服务,下面将springboot-h2对应的Restful服务也列出来:

  1. package com.chhliu.springboot.h2.controller;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.springframework.beans.factory.annotation.Autowired;  
  6. import org.springframework.web.bind.annotation.GetMapping;  
  7. import org.springframework.web.bind.annotation.PathVariable;  
  8. import org.springframework.web.bind.annotation.RestController;  
  9.   
  10. import com.chhliu.springboot.h2.entity.User;  
  11. import com.chhliu.springboot.h2.repository.UserRepository;  
  12.   
  13. @RestController  
  14. public class UserController {  
  15.   
  16.     @Autowired  
  17.     private UserRepository userRepository;  
  18.   
  19.     @GetMapping("/user/{id}")  
  20.     public User findById(@PathVariable Long id) {  
  21.         return this.userRepository.findOne(id);  
  22.     }  
  23.       
  24.     @GetMapping("/users")  
  25.     public List<User> findUsers(){  
  26.         return this.userRepository.findAll();  
  27.     }  
  28. }  
也就是说,当我们想使用Feign来消费上面的这个Restful服务的时候,我们只需要在自定义的接口上加上@FeignClient(注册到Eureka上的服务应用名),然后在接口定义的方法上加上对应的@RequestMapping即可

五、配置文件

  1. server.port:7904  
  2. # spring boot服务注册到Eureka Server上的应用名称  
  3. spring.application.name=springboot-rest-template-feign  
  4. eureka.instance.prefer-ip-address=true  
  5. # Eureka Server注册服务的地址  
  6. eureka.client.service-url.defaultZone=http://localhost:8761/eureka  
  7. springboot-h2.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule // Ribbon的负载均衡策略  

六、开启Feign支持

  1. package com.chhliu.springboot.restful;  
  2.   
  3. import org.springframework.boot.SpringApplication;  
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  5. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;  
  6. import org.springframework.cloud.netflix.feign.EnableFeignClients;  
  7.   
  8. @SpringBootApplication  
  9. @EnableEurekaClient  
  10. @EnableFeignClients // Feign支持  
  11. public class SpringbootRestTemplateApplication {  
  12.       
  13.     public static void main(String[] args) {  
  14.         SpringApplication.run(SpringbootRestTemplateApplication.class, args);  
  15.     }  
  16. }  
五、测试

依次启动Eureka Server和springboot-h2服务(由于该服务是被调用服务,为了同时演示负载均衡的效果,同时启动该服务的多个实例),然后启动工程,在浏览器中多次输入http://localhost:7904/template/3

测试结果如下:

  1. {"id":3,"username":"user3","name":"王五","age":20,"balance":100.00}  

springboot-h2:7901服务调用结果如下:

  1. Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=?  
  2. Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=?  
  3. Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=?  
  4. Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=?  
  5. Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=?  
  6. Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=?  
  7. Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=?  
  8. Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=?  
springboot-h2:7902服务调用结果如下:

  1. Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=?  
  2. Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=?  
  3. Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=?  
  4. Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=?  
  5. Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=?  
从测试结果可以看出,我们使用Feign消费了springboot-h2的Restful服务。


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多