服务调用Feign入门

服务调用Feign入门
A12X
1 前言
前面我们使用的RestTemplate实现REST API的调用,代码如下:
package com.xuguoguo.order.controller;

import com.xuguoguo.order.domain.Product;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping(value = “/order”)
public class OrderController {
@Autowired
private RestTemplate restTemplate;

@GetMapping(value = "/buy/{id}")
public Product buy(@PathVariable(value = "id") Long id) {
    Product product = restTemplate.getForObject("http://localhost:9003/product/findById/" + id, Product.class);
    return product;
}

}
由上面的代码可知,我们是使用字符串拼接的方式构造URL的,该URL只有一个参数。但是,在现实中,URL中往往含有多个参数,这个时候,我们如果还需要字符串拼接的方式构造URL,将会非常痛苦。

2 Feign简介

Feign是Netflix公司开发的声明式、模板化的HTTP客户端,其灵感来自Retrofit、JAXRS-2.0以及WebSocket。
Feign可以帮助我们更加便捷、优雅的调用HTTP的API。
在SpringCloud中,使用Feign非常简单–创建一个接口,并在接口上添加一些注解,代码就完成了。
Feign支持多种注解,例如Feign自带的注解或者JAX-RS注解等。
SpringCloud对Feign进行了增强,使得Feign支持了SpringMVC注解,并整合了Ribbon和Eureka,从而让Feign的使用更加简单。

3 基于Feign的服务调用

3.1 导入Feign的相关jar包的Maven坐标

修改部分:

org.springframework.cloud spring-cloud-starter-openfeign 完整部分: <?xml version="1.0" encoding="UTF-8"?> spring_cloud_demo com.xuguoguo 1.0 4.0.0
<artifactId>order-service-feign9005</artifactId>

<dependencies>
    <!-- 导入openFeign的依赖 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
</dependencies>
3.2 在启动类上添加Feign的支持

启动类:
package com.xuguoguo.order;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients //添加对Feign的支持,@EnableFeignClients注解开启SpringCloudFeign的支持功能
public class Order9005Application {
public static void main(String[] args) {
SpringApplication.run(Order9005Application.class, args);
}
}
3.3 配置调用接口

ProductClientFeign.java
package com.xuguoguo.order.feign;

import com.xuguoguo.order.domain.Product;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**

  • 声明需要调用的微服务名称

  • @FeignClient name: 服务提供者的名称
    */
    @FeignClient(name = “service-product”)
    public interface ProductFeignClient {

    /**

    • 配置需要调用的微服务的接口
    • @param id
    • @return
      */
      @GetMapping(value = “/product/findById/{id}”)
      Product findById(@PathVariable(value = “id”) Long id);
      }
      3.4 通过配置调用的接口调用远程微服务

OrderController.java
package com.xuguoguo.order.controller;

import com.xuguoguo.order.domain.Product;
import com.xuguoguo.order.feign.ProductFeignClient;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = “/order”)
public class OrderController {

@Autowired
private ProductFeignClient productFeignClient;


@GetMapping(value = "/buy/{id}")
public Product buy(@PathVariable(value = "id") Long id) {

    Product product = productFeignClient.findById(id);

    return product;
}

}
3.5 测试

4 Feign和Ribbon的联系

Ribbon是一个基于HTTP和TCP客户端的负载均衡工具。它可以在客户端配置RibbonServerList(服务端列表),使用HttpClient或者RestTemplate模拟HTTP请求,步骤相当繁琐。
Feign是在Ribbon的基础上进行了一次改进,是一个使用起来更加方便的HTTP客户端。采用接口的方式,只需要创建一个接口,然后在上面添加注解,将需要调用的其他服务的方法定义成抽象方法即可,不需要自己构建HTTP请求。然后就像调用自身工程的方法调用个,而感觉不到调用远程方法,使得编写客户端变得非常容易。

5 负载均衡

Feign本身已经集成了Ribbon的依赖和自动配置,因此我们不需要额外的引入依赖,也不需要再注册RestTemplate对象。我们可以通过
ribbon.xx来进行全局配置,或者通过
服务名.ribbon.xx来对指定的服务配置。
启动两个商品微服务,并将其注册到Eureka中,重新测试可以返现使用Ribbon的轮询策略进行负载均衡。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值