转载自:https://www.cnblogs.com/chxuyuan/p/8358998.html

SpringCloud系列研究---Eureka服务消费Feign

一、Feign简介

Feign是一种声明式、模板化的HTTP客户端。这使得Web服务客户端的写入更加方便 要使用Feign创建一个界面并对其进行注释。它具有可插入注释支持,包括Feign注释和JAX-RS注释。Feign还支持可插拔编码器和解码器。Spring Cloud增加了对Spring MVC注释的支持,并使用Spring Web中默认使用的HttpMessageConverters。Spring Cloud集成Ribbon和Eureka以在使用Feign时提供负载均衡的http客户端。这段话来源于官方文档,说白了就是通过Feign来调用Rest接口,而无需使用其他HTTP访问组件,并且同时还提供了负载均衡、编解码等功能,使用起来很方便。

二、环境介绍

首先在A服务器上启动Eureka服务,然后在B、C两台服务器上分别启动ms-demo-provider服务,这里也可以部署在一台服务器上采用不同的端口。访问Eureka界面查看服务注册状态,之后在本地新建客户端调用工程进行测试。此时A为注册中心,B、C分别为服务提供者(提供相同的接口),本地工程为服务消费者。

三、项目代码

在Idea中创建maven工程,ms-eurekaclient-demo工程代码结构如下:

1:pom.xml中的依赖如下:

<?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.cloud.microservice</groupId><artifactId>ms-eurekaclient-demo</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>ms-eurekaclient-demo</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.9.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><spring-cloud.version>Edgware.SR1</spring-cloud.version></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-feign</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-ribbon</artifactId></dependency><!-- 加入断路器依赖 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-hystrix</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>${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>

2:application.properties中的配置信息如下:

spring.application.name=ms-eurekaclient-demo
server.port=9800# 注册中心地址
eureka.client.serviceUrl.defaultZone=http://xx.xx.xx.xx:9000/eureka/# Indicates whether this client should fetch eureka registry information from eureka server
# 客户端是否要从eureka server获取注册信息,默认为true
eureka.client.fetchRegistry=true# Indicates how often(in seconds) to fetch the registry information from the eureka server
# 从eureka server获取注册信息的频率,默认为30秒,缩短配置时间可以缓解服务上线时间过长的问题
eureka.client.registryFetchIntervalSeconds=10

3:在入口类Application中增加@EnableEurekaClient和@EnableFeignClients的注解

  • @EnableEurekaClient:注解用来标识开启服务发现功能,据说也可使用@EnableDiscoveryClient,网上有人说@EnableEurekaClient本身就是用@EnableDiscoveryClient来实现的,这点没仔细研究过
  • @EnableFeignClients:注解用来开启Feign功能

package com.cloud.microservice.eurekaclientdemo;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class FeignDemoApplication {public static void main(String[] args) {SpringApplication.run(FeignDemoApplication.class, args);}
}

4:创建IUserFeignServiceClient接口类,代码如下:

package com.cloud.microservice.eurekaclientdemo.FeignClient;import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;@FeignClient("ms-demo-provider")
public interface IUserFeignServiceClient {//Feign定义服务提供者接口@RequestMapping(value = "/demo/user/1.0/findAll", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})String findAll();
}

5:创建IUserService接口类和UserServiceImp实现类,代码如下:

IUserService接口类如下:

package com.cloud.microservice.eurekaclientdemo.FeignClient;public interface  IUserService {String findAll();
}

UserServiceImp实现类如下:

package com.cloud.microservice.eurekaclientdemo.FeignClient;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class UserServiceImp implements  IUserService{@Autowiredprivate IUserFeignServiceClient userFeignServiceClient;public String findAll() {return "Feign: " + userFeignServiceClient.findAll();}
}

6:Rest接口定义,代码如下:

package com.cloud.microservice.eurekaclientdemo.FeignClient;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.RestController;@RestController
public class FeignDemoController {@Autowiredprivate IUserService userService;@RequestMapping(value = "/feigndemo/findAll", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})public String feignDemo() {return userService.findAll();}
}

启动工程,然后刷新Eureka界面,可以看到Feign已经注册到服务中心

四、运行测试

打开浏览器,访问ms-eurekaclient-demo中的接口,地址:http://localhost:9800/feigndemo/findAll,返回结果如下:

以上返回结果说明接口调用成功。同时我们也可以登陆到服务提供者的服务器上查看log。

通过多次访问http://localhost:9800/feigndemo/findAll这个接口,可以看到B、C两台服务器上的provider中都有接口被调用的记录。

分类: 微服务

SpringCloud系列研究---Eureka服务消费Feign相关推荐

  1. SpringCloud系列之Eureka服务注册及发现

    Eureka 1. Eureka简介 1.1 什么是Eureka 2. Eureka实例 2.1 编写Eureka Server 2.2 编写Eureka Client 3. Eureka源码解析 3 ...

  2. Spring Cloud(四)服务提供者 Eureka + 服务消费者 Feign

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

  3. J360-cloud SpringCloud系列二:服务发现Discovery Service

    2019独角兽企业重金招聘Python工程师标准>>> j360开源博客之 ----------------------------------------------------- ...

  4. 《微服务系列:Eureka服务注册发现中心》

    说在前头:本人为大二在读学生,书写文章的目的是为了对自己掌握的知识和技术进行一定的记录,同时乐于与大家一起分享,因本人资历尚浅,能力有限,文章难免存在一些错漏之处,还请阅读此文章的大牛们见谅与斧正.若 ...

  5. SpringCloud笔记(1)—Eureka服务注册中心

    项目开发练习涉及的模块 Eureka:负责服务发现相关.Eureka Server/Client.高可用.服务发现机制. Config:负责统一配置中心.Config Server/Client.Sp ...

  6. SpringCloud之声明式服务调用 Feign(三)

    一 Feign简介 Feign是一种声明式.模板化的HTTP客户端,也是netflix公司组件.使用feign可以在远程调用另外服务的API,如果调用本地API一样. 我们知道,阿里巴巴的doubbo ...

  7. springcloud系列三 搭建服务模块

    搭建服务模块为了模拟正式开发环境,只是少写了service层直接在controller里面直接引用,直接上图和代码:更为方便: 创建完成之后加入配置: pom.xml文件: <?xml vers ...

  8. SpringCloud系列:分布式服务调用链跟踪整合Zipkin、RabbitMQ、Elasticsearch(二)

    2019独角兽企业重金招聘Python工程师标准>>> 一.概述 RabbitMQ用于的服务和Zipkin服务端的通信,代替服务和Zipkin服务端通过http协议的通信,实现了微服 ...

  9. springcloud系列四 搭建服务模块重点讲解

    首先这个服务地址:一定不要写错,是自己注册中心开启的地址 如果注意到这些了,可以简单的进行操作,也可以不需要mybatis与数据库连接,在controller里直接返回相应的数据可以了,不用这么幸苦的 ...

最新文章

  1. js实现随机生成小方块
  2. python怎么启动mne_MNE-Python专辑 | MNE-Python详细安装与使用(更新)
  3. SAP HANA要改变什么?
  4. 超详解析Flutter渲染引擎|业务想创新,不了解底层原理怎么行?
  5. [蓝桥杯][2015年第六届真题]生命之树(树形dp)
  6. C# this关键字的3种用法
  7. 使用Spring Data REST将Spring Data JPA存储库导出为REST服务
  8. python调用dll函数_关于从加载的DLL调用函数的Python基本问题
  9. element动态form实现
  10. sqlserver 如何将exec的结果保存到一个变量_SQL Server之SQL Trace选项
  11. java httpurlconnection 设置编码_java – 通过HttpURLConnection发送UTF-8字符失败
  12. Python内置函数之-struct
  13. 克隆虚拟主机后的主机如何联网!!!!
  14. 滴滴程序员年薪80万却被亲戚鄙视:钱多有啥用,我儿子二本大学教师
  15. 当 webpack 遇上 symlink
  16. 怎么修复linux受损文件,Linux文件数据损坏的快速修复办法-文件或目录损坏且无法读取...
  17. 冯诺依曼体系结构、哈佛体系结构与改进型哈佛结构之间的区别
  18. Tomcat启动报错 More than one fragment with the name [spring_web] was found. This is not legal with relat
  19. 计算机组成原理包健百度云,计算机组成原理包健版答案及解析.doc
  20. https.protocols的检测与设置

热门文章

  1. server sql 统计信息 过时_sql-server – 何时更新统计信息?
  2. OpenShift 4 - 使用 Trivy Operator 对项目中的镜像进行安全扫描
  3. WPF实现Win10汉堡菜单
  4. (一)在GKE上创建MLOps Kubernetes集群
  5. Redis 6 将采用全新协议 RESP3,以提供客户端缓存功能
  6. 在存储过程中构建动态SQL
  7. ASP.NET Core 2.0和Angular 4:从头开始构建用于车辆管理的Web应用程序
  8. 大工18春计算机文化基础在线测试1,大工18春《计算机文化基础》在线测试1(满分答案)...
  9. 各种水龙头拆卸图解_各种水龙头拆卸图解法
  10. python人脸检测与微信小程序_python+requests对app和微信小程序进行接口测试