前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >一起来学Spring Cloud(F版) | 第三篇:注解式HTTP请求Feign

一起来学Spring Cloud(F版) | 第三篇:注解式HTTP请求Feign

作者头像
battcn
发布2018-10-18 11:41:35
5830
发布2018-10-18 11:41:35
举报
文章被收录于专栏:battcnbattcn

文章第一时间送达到您的手中!

Spring Cloud 为开发者提供了在分布式系统中的一些常用的组件(例如配置管理,服务发现,断路器,智能路由,微代理,控制总线,一次性令牌,全局锁定,决策竞选,分布式会话集群状态)。使用Spring Cloud开发人员可以快速地完成实现这些模式的服务和应用程序。它们在任何分布式环境中都能很好地工作

Feign

注解式的 Feign 使得 Java HTTP 客户端编写更方便。Feign 灵感来源于安卓网络编程框架 RetrofitJAXRS-2.0WebSocket,支持可插拔编码器和解码器,降低 HTTP API 的复杂度,通过最少的资源和代码来实现和 HTTP API 的连接。通过可定制的解码器和错误处理,可以编写任意的HTTP API。Spring Cloud Feign封装了 Ribbon 这一组件,所以在使用 Feign 同时还能提供负载均衡的功能,这一切只需要一个 @FeignClient即可完成。

早期版本的 FeignSpring Cloud 团队集成在 spring-cloud-netflix 子项目下,但如今 Spring Cloud团队将 Spring Cloud Feign 独立成一个单独的 spring-cloud-openfeign 项目

Try

准备三个工程,分别是 eureka-serverorder-serverproduct-server

Eureka Server

详情参考第一章,或从文末的 GITHUB 链接获取对应篇幅的完整代码

Product Server

一个普通的 Eureka Client 即可,详情参考上一章,或从文末的 GITHUB 链接获取对应篇幅的完整代码

Order Server

这个例子也是在上一章的基础之上做了扩展

依赖

对比上一章,此处多了一个 spring-cloud-starter-openfeign 的依赖

代码语言:javascript
复制
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
</dependencies>

配置文件

src/main/resources 目录下创建一个 bootstrap.yml 的文件,写上 eureka 相关配置信息

代码语言:javascript
复制
server:
  port: 7072
spring:
  application:
    name: order-server
eureka:
  instance:
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ip-address}:${spring.application.instance_id:${server.port}}
  client:
    service-url:
      defaultZone: http://localhost:7071/eureka/

ProductClient 接口

创建一个 ProductClient ,是不是感觉和 XxxxService 看起来类似(用法都类似),都是接口文件只不过在这个文件的上方多了一个 @FeignClient 注解,多种写法,总有一款适合你

  • name:指定 FeignClient 的名称,该属性会作为微服务的名称,用于服务发现
  • value:同 name 字段互通
  • serviceId:指定服务ID,每个注册到注册中心上的客户端都会有对应的 serviceId 一般是 spring.application.name,与 namevalue 互通
  • url: 一般用于调试,可以指定一个详细地址(http://localhost:8080/products)
  • path: 请求统一路径,可以看成 @RequestMapping("/products")
  • decode404:404 错误时,调用 decoder 进行解码,否则抛出 FeignException
  • fallback:发生错误时,回调 hystrix 类/方法(后面会详细介绍)
代码语言:javascript
复制
package com.battcn.api;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * @author Levin
 * @since 2018/9/26 0026
 */
@FeignClient(name = "product-server/products", decode404 = true)
//@FeignClient(name = "products", url = "http://localhost:7073/products")
//@FeignClient(value = "product", serviceId = "product-server", path = "/products", decode404 = true)
public interface ProductClient {

    /**
     * 根据产品ID查询产品信息
     *
     * @param productId ID
     * @return 查询结果
     */
    @GetMapping("/{product_id}")
    String selectProductById(@PathVariable("product_id") Long productId);
}

OrderController

直接使用 @Autowired 注入进去即可,然后调用就好了,对比较 Ribbon 这里我们看不到 RestTemplate 的代码了,也无需自己做解码映射,Spring Cloud Feign 默认都替我们实现好了,我们只需要遵循既定的标准即可

代码语言:javascript
复制
package com.battcn.controller;

import com.battcn.api.ProductClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Levin
 * @since 2018/9/26 0026
 */
@RestController
@RequestMapping("/orders")
public class OrderController {

    @Autowired
    private ProductClient productClient;


    @GetMapping
    public String query() {
        return this.productClient.selectProductById(10L);
    }
}

主函数

通过 @EnableFeignClients 注解开启对 Feign 的支持,用习惯 Dubbo 的朋友喜欢将 API 打包成独立的 JAR ,这个时候需要指定 basePackage 属性。

代码语言:javascript
复制
package com.battcn;

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

/**
 * @author Levin
 */
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class OrderApplication {

    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class, args);
    }

}

总结

目前很多大佬都写过关于 Spring Cloud 的教程了,如有雷同,请多多包涵,本教程基于最新的 spring-cloud:Finchley.SR1 编写…

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-10-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 battcn 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Feign
  • Try
    • Eureka Server
      • Product Server
        • Order Server
        • 总结
        相关产品与服务
        负载均衡
        负载均衡(Cloud Load Balancer,CLB)提供安全快捷的流量分发服务,访问流量经由 CLB 可以自动分配到云中的多台后端服务器上,扩展系统的服务能力并消除单点故障。负载均衡支持亿级连接和千万级并发,可轻松应对大流量访问,满足业务需求。
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档