前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringCloud详细教程 | 第三篇: 声明性REST客户端Feign(Greenwich版本)

SpringCloud详细教程 | 第三篇: 声明性REST客户端Feign(Greenwich版本)

作者头像
小东啊
发布2019-06-26 15:24:18
9210
发布2019-06-26 15:24:18
举报
文章被收录于专栏:李浩东的博客李浩东的博客

Spring Cloud有两种服务调用方式,一种是Ribbon+RestTemplate,另一种是Feign,上一篇文章,讲述了如何通过Ribbon+RestTemplate去调用服务,这篇文章主要讲述如何通过Feign去调用服务

一. Feign简介

Feign是一个声明式的Web服务客户端。这使得Web服务客户端的写入更加方便 要使用Feign创建一个界面并对其进行注释。它具有可插入注释支持,包括Feign注释和JAX-RS注释。Feign还支持可插拔编码器和解码器。Spring Cloud增加了对Spring MVC注释的支持,并使用Spring Web中默认使用的HttpMessageConverters。Spring Cloud集成Ribbon和Eureka以在使用Feign时提供负载均衡的http客户端。

二. Feign能做什么?

Feign能使我们远程调用服务客户端变得更加容易,不再使用Ribbon+RestTemplate模板式的调用,在一套系统中, 不止一个接口需要调用,往往遇到这样的情况总合有方法解决那就是Feign,Fegin进行了封装,由他来帮助我们定义和实现依赖服务接口的定义.在Feign的实现下,我们只需要创建一个接口并使用注解的方式来配置它(以前是Dao接口上标注Mapper注解,现在是一个微服务接口上面标注一个Feign注解即可),即可完成对服务提供方的接口绑定,简化了使用Spring cloud Ribbon时,自动封装服务调用客户端的开发量

三. Fegin实战

1.按照上篇文章一样启动两个实例

SpringCloud详细教程 | 第二篇: 客户端负载平衡器Ribbon(Greenwich版本)

192.168.1.105:eureka-client:8764

192.168.1.105:eureka-client:8762

2.创建Feign服务

开始创建feign-server服务 创建时勾选feign依赖

代码语言:javascript
复制
      <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>

改造后的pom.xml文件 继承父pom文件 和 父pom添加feign-server模块

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <parent>        <groupId>com.li</groupId>        <artifactId>SpringCloudLearn</artifactId>        <version>1.0-SNAPSHOT</version>        <relativePath>../</relativePath>    </parent>    <artifactId>feign-server</artifactId>    <version>0.0.1-SNAPSHOT</version>    <name>feign-server</name>    <description>Demo project for Spring Boot</description>
    <dependencies>        <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>
    <dependencyManagement>        <dependencies>            <dependency>                <groupId>org.springframework.cloud</groupId>                <artifactId>spring-cloud-dependencies</artifactId>                <version>${spring-cloud.version}</version>                <type>pom</type>                <scope>import</scope>            </dependency>        </dependencies>    </dependencyManagement>
    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>        </plugins>    </build>
</project>

配置application.properties文件

代码语言:javascript
复制
# 端口号server.port=8766# 需要指明spring.application.name 这个很重要# 这在以后的服务与服务之间相互调用一般都是根据这个namespring.application.name=feign-server#服务注册中心实例的主机名eureka.instance.hostname=localhost#服务注册中心端口号eureka.port=8761#在此指定服务注册中心地址eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${eureka.port}/eureka/

在程序的启动类加上@EnableEurekaClient向注册中心注册和@EnableFeignClients注解开启Feign的功能

代码语言:javascript
复制
package com.li.feignserver;
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableFeignClients  // 开启Feign功能@EnableEurekaClient@SpringBootApplicationpublic class FeignServerApplication {
    public static void main(String[] args) {        SpringApplication.run(FeignServerApplication.class, args);    }
}

创建一个服务接口请求服务提供者eureka-client

代码语言:javascript
复制
package com.li.feignserver.server;

import org.springframework.cloud.openfeign.FeignClient;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;
/** * eureka-client 是调用的服务名 */@FeignClient(value = "eureka-client")public interface EurekaCclientService {
    @GetMapping(value = "/hello")    String hello(@RequestParam(value = "name") String name);}

Feign配置重试机制和超时等

Request.Options 第一个参数建立socket超时时间 第二个读取响应socket超时时间

Retryer 第一个参数period是请求重试的间隔算法参数,第二个参数maxPeriod 是请求间隔最大时间,第三个参数是重试的次数

参考:https://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-feign.html

代码语言:javascript
复制
package com.li.feignserver.server;
import feign.Request;import feign.Retryer;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;
import java.util.concurrent.TimeUnit;
/** * @Classname FeignConfig * @Description TODO * @Author 李号东 lihaodongmail@163.com * @Date 2019-03-26 23:14 * @Version 1.0 */@Configurationpublic class FeignConfig {
    /**     * 建立socket超时时间 读取响应socket超时时间     * @return     */    @Bean    public Request.Options options() {        return new Request.Options(12000, 12000);    }
    /**     * 自定义重试次数     * @return     */    @Bean    public Retryer feignRetried(){        return new Retryer.Default(100, TimeUnit.SECONDS.toMillis(1L),5);    }}

在启动类添加请求

代码语言:javascript
复制
package com.li.feignserver;
import com.li.feignserver.server.EurekaCclientService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.cloud.openfeign.EnableFeignClients;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;
@RestController@EnableFeignClients // 开启Feign功能@EnableEurekaClient@SpringBootApplicationpublic class FeignServerApplication {
    public static void main(String[] args) {        SpringApplication.run(FeignServerApplication.class, args);    }

    @Autowired    private EurekaCclientService eurekaCclientService;
    @GetMapping("/hello")    public String hello(@RequestParam(value = "name") String name) {        return eurekaCclientService.hello(name);    }
}

启动项目,打开浏览器访问: http://localhost:8766/hello?name=lhd

第一次访问

第二次访问

是不是比Ribbon简单很多 赶快自己实现吧

个人是比较建议使用Feign 因为本身就包含Ribbon,数据的丢失率很低

源码下载: https://github.com/LiHaodong888/SpringCloudLearn

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

本文分享自 李浩东的博客 微信公众号,前往查看

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一. Feign简介
  • 二. Feign能做什么?
  • 三. Fegin实战
    • 1.按照上篇文章一样启动两个实例
    相关产品与服务
    微服务引擎 TSE
    微服务引擎(Tencent Cloud Service Engine)提供开箱即用的云上全场景微服务解决方案。支持开源增强的云原生注册配置中心(Zookeeper、Nacos 和 Apollo),北极星网格(腾讯自研并开源的 PolarisMesh)、云原生 API 网关(Kong)以及微服务应用托管的弹性微服务平台。微服务引擎完全兼容开源版本的使用方式,在功能、可用性和可运维性等多个方面进行增强。
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档