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
- spring cloud-使用feign来消费Restful服务同时加入Ribbon来实现负载均衡
- Spring Cloud-使用feign来消费Restful服务
- spring cloud使用ribbon实现负载均衡
- 使用feign作为客户端来消费服务,提供负载均衡和断路器支持
- spring cloud之服务调用及使用ribbon实现负载均衡(三)
- sping cloud feign ribbon均衡负载
- Spring Cloud的负载均衡Spring Cloud Ribbon和Spring Cloud Feign
- spring cloud学习(二)使用Ribbon实现客户端负载均衡
- Spring Cloud -- Ribbon负载均衡
- Spring-Cloud 负载均衡 Ribbon
- 【Spring Cloud】Ribbon负载均衡
- 微服务框架Spring Cloud介绍 Part4: 使用Eureka, Ribbon, Feign实现REST服务客户端
- Spring Cloud+Eureka+Ribbon实现客户端负载均衡
- Spring Cloud(三):服务消费Ribbon
- spring cloud中使用Ribbon实现客户端的软负载均衡
- spring cloud ribbon自定义负载均衡策略
- Spring Cloud 客服端负载均衡 Ribbon
- Spring Cloud 客服端负载均衡 Ribbon
- 17 - 05 - 09 大学第一学期给理想、尊严与未来交的答卷
- 读《黑客与画家》感想(4)
- synx的使用及出现的问题
- 海康 rtsp 主流 和 辅流的一个特点
- Array238ProductOfArrayExceptSelf
- spring cloud-使用feign来消费Restful服务同时加入Ribbon来实现负载均衡
- 2016年工作总结
- Python爬虫包 BeautifulSoup 学习(十一) CSS 选择器
- POJ3253 Fence Repair (HDU 1263 搬水果)
- PHP Study
- IS_REACHABLE
- android-webView拉起其他应用程序
- Eclipse 使用Maven
- mybatis详解(二)