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

来源:互联网 发布:支付宝与淘宝的关系 编辑:程序博客网 时间:2024/05/20 13:37

前言

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

一、Feign简介

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

二、在spring cloud中添加Feign支持

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

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

该示例是从前面的

使用RestTemplate消费spring boot的Restful服务

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

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

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

package com.chhliu.springboot.h2.controller;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RestController;import com.chhliu.springboot.h2.entity.User;import com.chhliu.springboot.h2.repository.UserRepository;@RestControllerpublic class UserController {@Autowiredprivate UserRepository userRepository;@GetMapping("/user/{id}")public User findById(@PathVariable Long id) {return this.userRepository.findOne(id);}@GetMapping("/users")public List<User> findUsers(){return this.userRepository.findAll();}}
也就是说,当我们想使用Feign来消费上面的这个Restful服务的时候,我们只需要在自定义的接口上加上@FeignClient(注册到Eureka上的服务应用名),然后在接口定义的方法上加上对应的@RequestMapping即可

五、配置文件

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

六、开启Feign支持

package com.chhliu.springboot.restful;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.cloud.netflix.feign.EnableFeignClients;@SpringBootApplication@EnableEurekaClient@EnableFeignClients // Feign支持public class SpringbootRestTemplateApplication {public static void main(String[] args) {SpringApplication.run(SpringbootRestTemplateApplication.class, args);}}
五、测试

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

测试结果如下:

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

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

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=?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=?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=?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=?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=?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=?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=?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服务调用结果如下:

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=?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=?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=?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=?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服务。


1 0
原创粉丝点击