Feign 是一个声明式的 Web Service 客户端,它的目的就是让 Web Service 调用更加简单。它整合了 Ribbon 和 Hystrix,从而让我们不再需要显式地使用这两个组件。Feign 还提供了 HTTP 请求的模板,通过编写简单的接口和插入注解,我们就可以定义好 HTTP 请求的参数、格式、地址等信息。接下来,Feign 会完全代理 HTTP 的请求,我们只需要像调用方法一样调用它就可以完成服务请求。

Feign 具有如下特性:

  • 可插拔的注解支持,包括 Feign 注解和 JAX-RS 注解
  • 支持可插拔的 HTTP 编码器和解码器
  • 支持 Hystrix 和它的 Fallback
  • 支持 Ribbon 的负载均衡
  • 支持 HTTP 请求和响应的压缩

上一篇中已经有服务提供者了,这次我们继续延用不再另写。

创建一个spring start project,添加以下依赖

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

配置属性(application.yml)

server:
port: 8083
spring:
application:
name: service-consumer-feign
eureka:
client:
serviceUrl:
defaultZone: http://admin:123456@localhost:8761/eureka/

启动类开启FeignClients

 package com.carry.springcloud;

 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
@EnableEurekaClient
@SpringBootApplication
public class ServiceConsumerFeignApplication { public static void main(String[] args) {
SpringApplication.run(ServiceConsumerFeignApplication.class, args);
}
}

创建一个FeignClient接口

 package com.carry.springcloud.api;

 import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping; @FeignClient(name = "service-producer")
public interface ConsumerFeignClient { @GetMapping("/getPortInfo")
public String produce();
}

解释:

  • @FeignClient(name="service-producer") 标明feign调用的微服务名称
  • @GetMapping("/getPortInfo") 对应service-producer微服务中的URL

创建一个Controller注入FeignClient并调用服务

 package com.carry.springcloud.controller;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import com.carry.springcloud.api.ConsumerFeignClient; @RestController
public class ConsumerController { @Autowired
private ConsumerFeignClient feignClient; @GetMapping("/getPoducerInfoByFeign")
public String getPoducerInfoByFeign() {
return feignClient.produce();
} }

测试

上图Eureka中服务提供者以及消费者feign已经注册成功

访问localhost:8083/getPoducerInfoByFeign,出现如下结果

再次访问

结果显示轮询访问8080和8081,访问正常。

Spring Cloud学习笔记【三】服务消费者Feign的更多相关文章

  1. spring cloud学习笔记三 Feign与Ribbon负载均衡的区别

    一.Feign的介绍 Feign一般比较书面的解释是:Feign是一个声明式的WebService客户端,使用Feign编写的WebService客户端更加简单,他的使用方法是定义一个接口,然后在上线 ...

  2. Spring Cloud 入门教程(二): 服务消费者(rest+ribbon)

    在上一篇文章,讲了服务的注册和发现.在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的.Spring cloud有两种服务调用方式,一种是ribbon+r ...

  3. Spring Cloud学习笔记-005

    服务消费者 之前已经搭建好了微服务中的核心组件——服务注册中心(包括单节点模式和高可用模式).也有了服务提供者,接下来搭建一个服务消费者,它主要完成两个目标,发现服务以及消费服务.其中,服务发现的任务 ...

  4. spring cloud (四、服务消费者demo_consumer)

    spring cloud (一.服务注册demo_eureka) spring cloud (二.服务注册安全demo_eureka) spring cloud (三.服务提供者demo_provid ...

  5. Spring Cloud 学习笔记(一)——入门、特征、配置

    [TOC] 0 放在前面 0.1 参考文档 http://cloud.spring.io/spring-cloud-static/Brixton.SR7/ https://springcloud.cc ...

  6. Spring Cloud学习笔记【二】Eureka 服务提供者/服务消费者(ribbon)

    Ribbon 是 Netflix 发布的开源项目,主要功能是为 REST 客户端实现负载均衡.它主要包括六个组件: ServerList,负载均衡使用的服务器列表.这个列表会缓存在负载均衡器中,并定期 ...

  7. Spring Cloud学习笔记【七】服务网关 Zuul(路由)

    Spring Cloud Zuul 路由是微服务架构的不可或缺的一部分,提供动态路由.监控.弹性.安全等的边缘服务.Zuul 是 Netflix 出品的一个基于 JVM 路由和服务端的负载均衡器. 准 ...

  8. Spring Cloud学习笔记【一】Eureka服务注册与发现

    Spring Cloud Eureka 是 Spring Cloud Netflix 微服务套件的一部分,基于 Netflix Eureka 做了二次封装,主要负责完成微服务架构中的服务治理功能,服务 ...

  9. Spring Cloud学习笔记之微服务架构

    目录 什么是微服务 架构优点 架构的挑战 设计原则 什么是微服务     微服务构架方法是以开发一种小型服务的方式,来开发一个独立的应用系统的.     其中每个小型服务都运行在自己的进程中,并经常采 ...

随机推荐

  1. Invalid property 'sentinels' of bean class redis spring 错误修改

    /* * Copyright 2014-2015 the original author or authors. * * Licensed under the Apache License, Vers ...

  2. QT跟VC++结合来进行插件的验证机制(遍历vtable,保证虚函数的个数一致,也可使用Q_INVOKABLE宏定义)

    由于最近公司要开发一个以C++插件机制为主的,主要有一个问题就是C++的二进制兼容性的问题.一旦类使用虚函数,只要随便改动下增删查改下头文件的虚函数,就会导致程序在跑的时候进行乱跳,因为这个时候exe ...

  3. codeforces#281 A

    脑子有点秀逗 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm ...

  4. Pascal Script

    MsgBox http://www.jrsoftware.org/ishelp/index.php?topic=isxfunc_msgbox ExpandConstant http://www.jrs ...

  5. poj--2385--Apple Catching(状态dp)

    Apple Catching Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submi ...

  6. [BZOJ4289] [PA2012] Tax 解题报告 (最短路+差分建图)

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=4289 4289: PA2012 Tax Time Limit: 10 Sec  Memo ...

  7. Pycharm-连接服务器

  8. 6.deque

    #include <iostream> #include <deque> #include <algorithm> using namespace std; //序 ...

  9. js确认框confirm()用法实例详解

    先为大家介绍javascript确认框的三种使用方法,具体内容如下 第一种方法:挺好用的,确认以后才能打开下载地址页面.原理也比较清晰.主要用于删除单条信息确认. ? 1 2 3 4 5 6 7 8 ...

  10. GoldenGate 异常处理预案

    异常处理一般步骤 如果GoldenGate复制出现异常,可以通过以下步骤尝试解决问题: 1)        通过ggsci>view report命令查找ERROR字样,确定错误原因并根据其信息 ...