spring cloud的Netflix中提供了两个组件实现软负载均衡调用,分别是Ribbon和Feign。上一篇和大家一起学习了Ribbon。

Ribbon :Spring Cloud Ribbon是基于HTTP和TCP的客户端负载工具,它是基于Netflix Ribbon实现的,它可以在客户端配置 ribbonServerList(服务端列表),然后轮询请求以实现均衡负载。

Feign :spring cloud feign 是一个使用起来更加方便的 HTTP 客戶端。 在使用ribbon时,通常会使用RestTemplate实现对http请求的封装,形成了模板化的调用方法。spring cloud feign在此基础上做了进一步的封装,Feign是一种声明式、模板化的HTTP客户端。在Spring Cloud中使用Feign, 可以做到使用HTTP请求远程服务时能与调用本地方法一样的编码体验,完全感知不到这是远程方法,更感知不到这是个HTTP请求。

1、 新建项目sc-eureka-client-consumer-feign,对应的pom.xml文件如下

<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>spring-cloud</groupId>

<artifactId>sc-eureka-client-consumer-feign</artifactId>

<version>0.0.1-SNAPSHOT</version>

<packaging>jar</packaging>

<name>sc-eureka-client-consumer-feign</name>

<url>http://maven.apache.org</url>

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>2.0.4.RELEASE</version>

</parent>

<dependencyManagement>

<dependencies>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-dependencies</artifactId>

<version>Finchley.RELEASE</version>

<type>pom</type>

<scope>import</scope>

</dependency>

</dependencies>

</dependencyManagement>

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<maven.compiler.source>1.8</maven.compiler.source>

<maven.compiler.target>1.8</maven.compiler.target>

</properties>

<dependencies>

<!-- 说明是一个 eureka client -->

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<!-- <dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-feign</artifactId>

<version>1.4.5.RELEASE</version>

</dependency> -->

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-openfeign</artifactId>

</dependency>

</dependencies>

</project>

备注:spring cloud 2.x后spring-cloud-starter-feign已经被标识为过期,推荐使用spring-cloud-starter-openfeign

2、 新建spring boot启动类ConsumerFeignApplication.java

package sc.consumer;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication

@EnableEurekaClient

@EnableFeignClients

public class ConsumerFeignApplication {

public static void main(String[] args) {

SpringApplication.run(ConsumerFeignApplication.class, args);

}

}

3、 创建配置文件bootstrap.yml和application.yml,对应的内容如下

bootstrap.yml

server:

port: 5800

application.yml

spring:

application:

name: sc-eureka-client-consumer-feign

eureka:

client:

registerWithEureka: true #是否将自己注册到Eureka服务中,默认为true

fetchRegistry: true #是否从Eureka中获取注册信息,默认为true

serviceUrl:

defaultZone: http://localhost:5001/eureka/

4、 编写feign客户端

package sc.consumer.service;

import java.util.Map;

import org.springframework.cloud.openfeign.FeignClient;

import org.springframework.web.bind.annotation.DeleteMapping;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.PutMapping;

import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RequestMapping;

import sc.consumer.model.User;

@FeignClient(value="sc-eureka-client-provider")

public interface UserService {

@GetMapping("/user/getUser/{id}")

Map<String, Object> getUser(@PathVariable(value ="id") Long id);

@RequestMapping("/user/listUser")

Map<String, Object> listUser();

@PostMapping("/user/addUser")

Map<String, Object> addUser(@RequestBody User user);

@PutMapping("/user/updateUser")

Map<String, Object> updateUser(@RequestBody User user);

@DeleteMapping("/user/deleteUser/{id}")

Map<String, Object> deleteUser(@PathVariable(value ="id") Long id);

}

5、 分别启动注册中心项目sc-eureka-server和服务提供者sc-eureka-client-provider

6、 启动项目sc-eureka-client-consumer-feign,并验证是否启动成功

方法一

方法二

7、 使用postman验证

查询:

http://127.0.0.1:5800/feign/user/getUser/4

列表:

http://127.0.0.1:5800/feign/user/listUser

添加:

http://127.0.0.1:5800/feign/user/addUser

更新:

http://127.0.0.1:5800/feign/user/updateUser

删除:

http://127.0.0.1:5800/feign/user/deleteUser/6

备注:

sc-eureka-client-provider项目的UserController.java 需要修正

https://gitee.com/hjj520/spring-cloud-2.x/tree/master/sc-eureka-client-consumer-feign

8、服务发现&服务消费者Feign的更多相关文章

  1. 在Windows环境中使用Nginx, Consul, Consul Template搭建负载均衡和服务发现服务

    搭建负载均衡和服务发现服务的目的 随着网站业务的不断提升,单个服务器的性能越来越难满足客户的业务需求,所以很多情况下,需要使用多服务器实例和负载均衡器来满足业务需要. Nginx 什么是Nginx N ...

  2. 7、服务发现&服务消费者Ribbon

    公众号: java乐园 在<服务注册&服务提供者>这一篇可能学习了这么开发一个服务提供者,在生成上服务提供者通常是部署在内网上,即是服务提供者所在的服务器是与互联网完全隔离的.这篇 ...

  3. .net core consul 服务配置 服务发现 服务健康检测 服务变更加载

    准备环境 安装consul之后 1. 创建一个.net core webapi 举例为UsercenterService 2. nuget引用Consul组件  https://github.com/ ...

  4. SpringCloud2.0 Feign 服务发现 基础教程(五)

    1.启动[服务中心]集群,即 Eureka Server 参考 SpringCloud2.0 Eureka Server 服务中心 基础教程(二) 2.启动[服务提供者]集群,即 Eureka Cli ...

  5. 鹅长微服务发现与治理巨作PolarisMesh实践-上

    @ 目录 概述 定义 核心功能 组件和生态 特色亮点 解决哪些问题 官方性能数据 架构原理 资源模型 服务治理 基本原理 服务注册 服务发现 安装 部署架构 集群安装 SpringCloud应用接入 ...

  6. 服务发现Eureka、zookeeper、consul

    Spring Cloud为开发人员提供了工具,以快速构建分布式系统中的某些常见模式(例如,配置管理,服务发现,断路器,智能路由,微代理,控制总线,一次性令牌,全局锁,领导选举,分布式会话,群集状态). ...

  7. Consul 服务注册与服务发现

    上一篇:Mac OS.Ubuntu 安装及使用 Consul 1. 服务注册 对 Consul 进行服务注册之前,需要先部署一个服务站点,我们可以使用 ASP.NET Core 创建 Web 应用程序 ...

  8. Presto服务发现(Discovery Service)

    Presto 集群配置不管是coordinator还是worker配置项中都有一项discovery.uri,这个是一个比较核心的东西,简单来说就是服务发现的地址. coordinator和worke ...

  9. k8s服务发现和负载均衡(转)

    原文 http://m635674608.iteye.com/blog/2360095 kubernetes中如何发现服务 如何发现pod提供的服务 如何使用kube-dns发现服务   servic ...

随机推荐

  1. docker的入门简介

    可能写的不是很完美,需要大家指正修改和意见(谢谢合作) docker的入门: docker的好处: 1.更快交付你的应用(Faster delivery of your applications) 2 ...

  2. vue 实现active点击图片切换

    循环条件下: 1.点击函数@click="active(index)" 获取点击的位置 2.讲索引值传给class,点击哪一个则显示哪一个的样式 3.在data添加ins的初始值 ...

  3. jackson的readTree有坑

    {"}] readTree认为上面的字符是json,坑啊 alibaba的fastjson 无论JSONObject.parseObject还是JSONObject.parseObject ...

  4. systemctl 相关命令

    systemctl 相关命令:service,chkconfig systemd 是 Linux 下的一款系统和服务管理器,兼容 SysV 和 LSB 的启动脚本.systemd 的特性有:支持并行化 ...

  5. 中州韵输入法(rime)导入搜狗词库

    rime是一个非常优秀的输入法,linux平台下的反应速度远超搜狗,也没有隐私风险.2012年开始接触它,到后来抛弃了它,因为rime自带的词库真的太弱了,也懒得折腾.最近发现一个词库转换软件叫ime ...

  6. vue学习笔记(五)— 组件通信

    关于vue父子组件通信 作者:狐狸家的鱼 本文链接:vue组件通信 GitHub:sueRimn 如果组件是一个单页面,组件之间存在父子关系,数据传递就需要根据父子不同的地位使用不同的办法. 借助新建 ...

  7. PHP浮点精度问题

    使用php+ - * /计算浮点数的时候,可能会遇到一些计算结果错误的问题,如下: <?php echo intval(0.58 * 100); //输出57 解决办法 <?php ech ...

  8. openwrt配置内核,加载air720 4G模块的USB串口设备

    1,进入openwrt源码包,键入 make menuconfig 2,配置如下 kernel modules  ---> USB Support---> <*> kmod-u ...

  9. font-size-adjust属性定义及用法

    font-size-adjust属性定义及用法 在css中,font-size-adjust属性是使用来更好的控制字体大小,当第一个选择的字体不可用时,浏览器使用第二个指定的字体,这可能会导致改变字体 ...

  10. hdu 4705 Y (树形dp)

    Description Input 4 1 2 1 3 1 4 题目的意思是给你一棵树,让你找到所有不在一条路径上的三个点的情况个数.乍一看正向处理比较麻烦,我们从反方向考虑,如果是取在一条路径上的3 ...