本文根据https://blog.csdn.net/forezp/article/details/81040965写出,修正了部分瑕疵,在此对那位博主表示感谢。

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

一、Feign简介

Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单。使用Feign,只需要创建一个接口并注解。它具有可插拔的注解特性。

Feign支持可插拔的编码器和解码器。Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果。

简而言之:

  • Feign 采用的是基于接口的注解
  • Feign 整合了ribbon,具有负载均衡的能力
  • 整合了Hystrix,具有熔断的能力

二、准备工作

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

三、创建一个feign的服务

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


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

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

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

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

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

hi sun ,i am from port:8762

hi sun ,i am from port:8763

聪明如你,一定可以想到传对象的问题,下面来看看feign如何传对象给服务生产者:

1、eureka-client工程新建一个实体类User:

package com.sun;

public class User {
private Long id;
private String username;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
} }

2、新建controller:UserController代码如下:

package com.sun;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
@RequestMapping(value="users")
public class UserController { @RequestMapping(value="/list",method={RequestMethod.GET,RequestMethod.POST,RequestMethod.PUT})
@ResponseBody
public User list(@RequestBody User user) throws InterruptedException{
user.setUsername(user.getUsername().toUpperCase());
user.setId(user.getId()+100l);
return user;
}
}

注意到接收参数是由@RequestBody User user接收。

3、eureka-serice-feign工程新建实体类User,代码和eureka-client一样。

4、新建RemoteService,作为消费的service:

package com.sun;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import feign.Headers;
import feign.Param; @FeignClient(value = "service-hi")
public interface RemoteService {
@Headers({"Content-Type: application/json","Accept: application/json"})
@RequestMapping(value = "/users/list",method = RequestMethod.POST)
String getOwner(@Param(value = "user") User user);
}
最关键的一句是@Headers({"Content-Type: application/json","Accept: application/json"}),提交的是json格式的数据。
接口传了一个User对象。
5、新建UserController,作为controller映射地址:
package com.sun;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; @RestController
public class UserController {
//编译器报错,无视。 因为这个Bean是在程序启动的时候注入的,编译器感知不到,所以报错。
@Autowired
RemoteService remoteService; @RequestMapping(value = "/getOwner", method = RequestMethod.GET)
public String sayHi(@RequestParam String name) {
User user = new User();
user.setUsername(name);
user.setId(100L);
String result = remoteService.getOwner(user);
return result;
}
}

访问http://localhost:8765/getOwner?name=sun,就可以看到返回的json串:

{"id":200,"username":"SUN"}

 

第三篇: 服务消费者(Feign)的更多相关文章

  1. SpringCloud教程 | 第三篇: 服务消费者(Feign)

    上一篇文章,讲述了如何通过RestTemplate+Ribbon去消费服务,这篇文章主要讲述如何通过Feign去消费服务.一.Feign简介 Feign是一个声明式的伪Http客户端,它使得写Http ...

  2. SpringCloud教程 | 第三篇: 服务消费者(Feign)(Finchley版本)

    上一篇文章,讲述了如何通过RestTemplate+Ribbon去消费服务,这篇文章主要讲述如何通过Feign去消费服务. 一.Feign简介 Feign是一个声明式的伪Http客户端,它使得写Htt ...

  3. 【SpringCloud】第三篇: 服务消费者(Feign)

    前言: 必需学会SpringBoot基础知识 简介: spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选. ...

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

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f3-feign/ 本文出自方志朋的博客 上一篇文章,讲述了如 ...

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

    转载请标明出处: https://www.fangzhipeng.com/springcloud/2017/07/12/sc03-feign/ 本文出自方志朋的博客 最新Finchley版本请访问: ...

  6. Spring Cloud学习笔记【三】服务消费者Feign

    Feign 是一个声明式的 Web Service 客户端,它的目的就是让 Web Service 调用更加简单.它整合了 Ribbon 和 Hystrix,从而让我们不再需要显式地使用这两个组件.F ...

  7. SpringCloud学习系列之二 ----- 服务消费者(Feign)和负载均衡(Ribbon)使用详解

    前言 本篇主要介绍的是SpringCloud中的服务消费者(Feign)和负载均衡(Ribbon)功能的实现以及使用Feign结合Ribbon实现负载均衡. SpringCloud Feign Fei ...

  8. SpringCloud学习(三)服务消费者(Feign)(Finchley版本)

    上一篇文章,讲述了如何通过RestTemplate+Ribbon去消费服务,这篇文章主要讲述如何通过Feign去消费服务. Feign简介 Feign是一个声明式的伪Http客户端,它使得写Http客 ...

  9. SpringCloud教程 | 第二篇: 服务消费者(rest+ribbon)

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

随机推荐

  1. 从performance_schema中查看MySQL活动Session的详细执行信息

    本文出处:http://www.cnblogs.com/wy123/p/7851294.html 在做数据库的异常诊断的时候,之前在SQL Server上的时候,最主要的参考信息之一就是去看当前的活动 ...

  2. js中子窗口调用父窗口中的变量、函数

  3. 大数据入门到精通11-spark dataframe 基础操作

    // dataframe is the topic 一.获得基础数据.先通过rdd的方式获得数据 val ny= sc.textFile("data/new_york/")val ...

  4. php面试题五之nginx如何调用php和php-fpm的作用和工作原理

    nginx如何调用php 采用nginx+php作为webserver的架构模式,在现如今运用相当广泛.然而第一步需要实现的是如何让nginx正确的调用php.由于nginx调用php并不是如同调用一 ...

  5. FortiGate部分用户上网慢,丢包严重

    1.现状: 如图,出口internet有2条联通线路分别为liant_218和liant_61,在防火墙上使用WAN LLB,基于源IP: 2.现象: 使用liant_218的用户上网正常,使用lia ...

  6. node.js中express的Router路由的使用

    express中的Router作用就是为了方便我们更好的根据路由去分模块.避免将所有路由都写在入口文件中. 一.简单的使用Router const express = require('express ...

  7. LWIP学习

    转自:https://blog.csdn.net/kzq_qmi/article/details/46900589 数据包pbuf:    LwIP采用数据结构 pbuf 来描述数据包,其结构如下:  ...

  8. Android 获得本地IP地址、外网IP地址、本设备网络状态信息、本地Mac地址

    本地内网IP和外网IP的区别: 根据我的经验一台电脑需要两个ip才可以上网,一个是本地的内网ip 一个是外网的ip 本地的ip 一般是192.168.1.2这种样子  只要在不同的路由器上可以重复 外 ...

  9. 14. pt-kill

    pt-kill h=192.168.100.101,P=3306,u=admin,p=admin \--match-user "user01" \--match-host &quo ...

  10. SPOJ - AMR11B

    题目链接:https://www.spoj.com/problems/AMR11B/en/ 题目大意就是要你求图形覆盖的格点数,标记每个图形里的未标记格点(包括边界),总标记数就是覆盖的总格点数. # ...