史上最简单的SpringCloud教程 | 第三篇: 服务消费者(Feign)

史上最简单的SpringCloud教程 | 第三篇: 服务消费者(Feign)原文地址:https://blog.csdn.net/forezp/article/details/69808079上一篇文章,讲述了如何通过RestTemplate+Ribbon去消费服务,这篇文章主要讲述如何通过Feign去消费服务。一、Feign简介Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单。使用Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,可…

大家好,又见面了,我是你们的朋友全栈君。

原文地址:https://blog.csdn.net/forezp/article/details/69808079


上一篇文章,讲述了如何通过RestTemplate+Ribbon去消费服务,这篇文章主要讲述如何通过Feign去消费服务。

一、Feign简介

Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单。使用Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解。Feign支持可插拔的编码器和解码器。Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果。

简而言之:

  • Feign 采用的是基于接口的注解
  • Feign 整合了ribbon

二、准备工作

继续用上一节的工程, 启动eureka-server,端口为8761; 启动service-hi 两次,端口分别为8762 、8773.

三、创建一个feign的服务

新建一个spring-boot工程,取名为serice-feign,在它的pom文件引入Feign的起步依赖spring-cloud-starter-feign、Eureka的起步依赖spring-cloud-starter-eureka、Web的起步依赖spring-boot-starter-web,代码如下:

<?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>

    <groupId>com.forezp</groupId>
    <artifactId>service-feign</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>service-feign</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.RC1</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>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>


</project>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82

在工程的配置文件application.yml文件,指定程序名为service-feign,端口号为8765,服务注册地址为http://localhost:8761/eureka/ ,代码如下:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8765
spring:
  application:
    name: service-feign
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

在程序的启动类ServiceFeignApplication ,加上@EnableFeignClients注解开启Feign的功能:

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ServiceFeignApplication { 
   

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

定义一个feign接口,通过@ FeignClient(“服务名”),来指定调用哪个服务。比如在代码中调用了service-hi服务的“/hi”接口,代码如下:


/** * Created by fangzhipeng on 2017/4/6. */
@FeignClient(value = "service-hi")
public interface SchedualServiceHi { 
   
    @RequestMapping(value = "/hi",method = RequestMethod.GET)
    String sayHiFromClientOne(@RequestParam(value = "name") String name);
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

在Web层的controller层,对外暴露一个”/hi”的API接口,通过上面定义的Feign客户端SchedualServiceHi 来消费服务。代码如下:

@RestController
public class HiController { 
   

    @Autowired
    SchedualServiceHi schedualServiceHi;
    @RequestMapping(value = "/hi",method = RequestMethod.GET)
    public String sayHi(@RequestParam String name){
        return schedualServiceHi.sayHiFromClientOne(name);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

启动程序,多次访问http://localhost:8765/hi?name=forezp,浏览器交替显示:

hi forezp,i am from port:8762

hi forezp,i am from port:8763

Feign源码解析:http://blog.csdn.net/forezp/article/details/73480304

本文源码下载: 
https://github.com/forezp/SpringCloudLearning/tree/master/chapter3

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/152807.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 2021必看!java电子书合集,值得收藏![通俗易懂]

    2021必看!java电子书合集,值得收藏![通俗易懂]正文作为后端开发,日常操作数据库最常用的是写操作和读操作。读操作我们下边会讲,这个分类里我们主要来看看写操作时为什么会导致SQL变慢。刷脏页脏页的定义是这样的:内存数据页和磁盘数据页不一致时,那么称这个内存数据页为脏页。那为什么会出现脏页,刷脏页又怎么会导致SQL变慢呢?那就需要我们来看看写操作时的流程是什么样的。对于一条写操作的SQL来说,执行的过程中涉及到写日志,内存及同步磁盘这几种情况。这里要提到一个日志文件,那就是redolog,位于存储引擎层,用来存储物理日志。在写操

    2022年7月9日
    25
  • python的image读取的图片是什么类型的_python读取图片数据

    python的image读取的图片是什么类型的_python读取图片数据Python读取图片尺寸、图片格式需要用到PIL模块,使用pip安装Pillow.Pillow是从PILfork过来的Python图片库。fromPILimportImageim=Image.open(filename)#返回一个Image对象print(‘宽:%d,高:%d’%(im.size[0],im.size[1]))Image类的属性##PIL.Image.format图片…

    2025年10月29日
    3
  • 40 道基础Dubbo 面试题及答案

    40 道基础Dubbo 面试题及答案转载自史上最全40道Dubbo面试题及答案,看完碾压面试官想往高处走,怎么能不懂Dubbo?Dubbo是国内最出名的分布式服务框架,也是Java程序员必备的必会的框架之一。Dubbo更是中高级面试过程中经常会问的技术,无论你是否用过,你都必须熟悉。下面我为大家准备了一些Dubbo常见的的面试题,一些是我经常问别人的,一些是我过去面试遇到的一些问题,总结给大家,希…

    2022年5月10日
    48
  • 「从零单排canal 07」 parser模块源码解析

    「从零单排canal 07」 parser模块源码解析

    2020年11月19日
    212
  • win10系统用激活码激活,用dos命令来激活,只能激活半年时间,暂时没找到永久的。

    win10系统用激活码激活,用dos命令来激活,只能激活半年时间,暂时没找到永久的。自己之前用激活工具激活的,激活时间好像是180天,即将到期了,于是在网上百度各种永久激活方法,参照别人的方法,发现把自己的激活状态由激活即将到期变为通知模式,瞬间一脸懵逼,于是找啊找,终于找到了这种方式,亲测可行。(1)首先查看自己系统的激活状态;快捷键win+r输入命令:slmgr.vbs-xpr; 随后弹出窗口显示2018/9/8过期。 (2)然后以管理员模式打开命令提示符窗…

    2022年5月30日
    116
  • String、StringBuilder和StringBuffer

    String、StringBuilder和StringBuffer这三个类之间的区别主要是在两个方面,即运行速度和线程安全这两方面。首先说运行速度,或者说是执行速度,在这方面运行速度快慢为:StringBuilder &gt; StringBuffer &gt; String  String最慢的原因:  String为字符串常量,而StringBuilder和StringBuffer均为字符串变量,即String对象一旦创建之后该对象是不可更改的,但…

    2022年6月13日
    28

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号