前言

本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3

本文基于前两篇文章eureka-server和eureka-client的实现。

参考

创建Feign工程

1.1 创建sping boot工程:eureka-feign

1.2 添加pom.xml相关依赖

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

1.3 application添加配置信息

spring:
application:
name: eureka-feign server:
port: 8601 eureka:
instance:
hostname: localhost
lease-renewal-interval-in-seconds: 5
lease-expiration-duration-in-seconds: 10
client:
service-url:
defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/

1.4 启动类EurekaFeignApplication增加注解

package spring.cloud.demo.eurekafeign;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients; @EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication
public class EurekaFeignApplication { public static void main(String[] args) {
SpringApplication.run(EurekaFeignApplication.class, args);
} }

@EnableDiscoveryClient:这里使用EnableDiscoveryClient注解,在eureka-client已说明,这边就不在阐述

@EnableFeignClients:启用Feign客户端

1.5 创建服务接口EurekaFeignService

package spring.cloud.demo.eurekafeign.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping; /**
* @auther: maomao
* @DateT: 2019-09-17
*/
@FeignClient(value = "eureka-client")
public interface EurekaFeignService { @RequestMapping(value = "/info")
String syaHello();
}

@FeignClient:定义Feign客户端,调用远程client。eureka-client代表client的spring.application.name,调用远程地址示例:http://eureka-client/info

1.6 创建EurekaFeignController控制类

package spring.cloud.demo.eurekafeign.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import spring.cloud.demo.eurekafeign.service.EurekaFeignService; import javax.annotation.Resource; /**
* @auther: maomao
* @DateT: 2019-09-17
*/
@RestController
@RequestMapping("/feign")
public class EurekaFeignController { @Resource
private EurekaFeignService eurekaFeignService; @RequestMapping("/sayHello")
public String sayHello() {
return "feign result: " + eurekaFeignService.syaHello();
} }

1.7 启动eureka-feign服务

可以在eureka-server服务注册中心看到eureka-feign是否注册成功。

红框中内容代表eureka-feign已经正常启动并成功注册到eureka-server服务注册中心。

访问http://localhost:8601/feign/sayHello,显示如下:



多刷新几次可以在浏览器中看到不通的结果,端口是变化的。

Feign默认的负载均衡策略是轮询方式。如果想修改Feign的负载均衡策略请参考eureka-ribbon中的配置

至此,一个简单的单机Feign服务消费者工程就搭建完成了。

彩蛋

Feign集成Hystrix熔断机制

pom.xml增加依赖

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

增加application.yml配置

feign:
hystrix:
enabled: true

修改EurekaFeignService

在@FeignClient注解中增加fallback

package spring.cloud.demo.eurekafeign.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping; /**
* @auther: maomao
* @DateT: 2019-09-17
*/
@FeignClient(value = "eureka-client", fallback = EurekaFeignServiceFailure.class)
public interface EurekaFeignService { @RequestMapping(value = "/info")
String syaHello();
}

增加EurekaFeignServiceFailure类:

package spring.cloud.demo.eurekafeign.service;

import org.springframework.stereotype.Service;

/**
* @auther: maomao
* @DateT: 2019-09-17
*/
@Service
public class EurekaFeignServiceFailure implements EurekaFeignService {
@Override
public String syaHello() {
return "网络繁忙,请稍后在试";
}
}

启动eureka-feign服务

首先在eureka-client全不启动的情况,访问http://localhost:8601/feign/sayHello看是否全不正常,然后停掉其中一个eureka-client,在多次刷新http://localhost:8601/feign/sayHello,显示如下:



说明增加的Hystrix已经生效。

至此,Feign集成Hystrix熔断机制全部完成。

总结

本文主要简单实现了feign作为服务消费的简单应用和Hystrix的集成。

代码地址

gitHub地址


《Srping Cloud 2.X小白教程》目录


转载请注明出处,

  • 联系方式:4272231@163.com

spring cloud 2.x版本 Feign服务发现教程(内含集成Hystrix熔断机制)的更多相关文章

  1. spring cloud 2.x版本 Ribbon服务发现教程(内含集成Hystrix熔断机制)

    本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 前言 本文基于前两篇文章eureka-server和eureka-client的实现. 参考 ...

  2. spring cloud 2.x版本 Eureka Client服务提供者教程

    本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 1 创建eureka client 1.1 新建Srping boot工程:eureka-c ...

  3. spring cloud 2.x版本 Zuul路由网关教程

    前言 本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 本文基于前两篇文章eureka-server.eureka-client.eureka ...

  4. spring cloud 2.x版本 Gateway动态路由教程

    摘要 本文采用的Spring cloud为2.1.8RELEASE,version=Greenwich.SR3 本文基于前面的几篇Spring cloud Gateway文章的实现. 参考 Gatew ...

  5. spring cloud 2.x版本 Gateway路由网关教程

    前言 本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 本文基于前两篇文章eureka-server.eureka-client.eureka ...

  6. spring cloud 2.x版本 Config配置中心教程

    前言 本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 本文基于前面的文章eureka-server的实现. 参考 eureka-server ...

  7. spring cloud 2.x版本 Gateway自定义过滤器教程

    前言 本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 本文基于前两篇文章eureka-server.eureka-client.eureka ...

  8. Spring Cloud官方文档中文版-服务发现:Eureka客户端

    官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#_spring_cloud_netflix 文中例子我做了一些测试在:h ...

  9. Spring Cloud官方文档中文版-服务发现:Eureka服务端

    官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR3/#spring-cloud-eureka-server 文中例子我做了一些 ...

随机推荐

  1. windows如何利用计划任务自动关机?

    第一步打开控制面板,然后选择计划任务,打开它 选择创建基本任务 输入任务名称,描述,选择下一步 根据需要选择,我这里选择的是每天,然后选择下一步 选择任务开始时间,然后选择下一步 选择启动程序,然后选 ...

  2. Event Hub小白入门指南

    Event Hub事件中心 本文的目的在于用最白的大白话,让你从“完全不懂”开始,理解什么是分布式大数据流平台Event Hub,并且理解它的关键概念,并且初步理解其收发数据API. 定义,Event ...

  3. 浅谈MVC&MTV设计模式

    在目前基于Python语言的几十个Web开发框架中,几乎所有的全栈框架都强制或引导开发者使用MVC设计模式.所谓全栈框架,是指除了封装网络和线程操作,还提供HTTP.数据库读写管理.HTML模板引擎等 ...

  4. centos7 scrapy安装

    1.anaconda3安装 wget https://repo.anaconda.com/archive/Anaconda3-2019.03-Linux-x86_64.sh 安装报错,可能是源的问题 ...

  5. Python调用 Openstack 主要服务(keystone,nova,glance,neutron,heat)

    由于Openstack更新很快,现在准备搭建基于Queen版本的Openstack,Queen版本要求keystone版本为V3,所以之前大多数接口都不能用了,百度了一下都没有比较新的实例,官方文档又 ...

  6. MySQLdb操作数据库

    堡垒机前戏 开发堡垒机之前,先来学习Python的paramiko模块,该模块机遇SSH用于连接远程服务器并执行相关操作 SSHClient 用于连接远程服务器并执行基本命令 基于用户名密码连接: + ...

  7. 2.linux系统基础笔记(延时操作、实时系统中的定时器、事件)

    延时操作 延时操作是操作系统中经常遇到的一种情形.延时的原因很多,有的时候是为了等待外设芯片处理结束,有的时候是为了暂时释放cpu的使用权,有的就是为了希望在一段时间获取资源,如果没法在单位时间内获取 ...

  8. T4 模板

    T4模板入门 T4,即4个T开头的英文字母组合:Text Template Transformation Toolkit.T4(Text Template Transformation Toolkit ...

  9. SQL Server换版本卸载问题

    好久没更博客了,今天随性的更一篇.. 你是否也在问这个问题. 给你答案: 1.运行:输入regedit 进入注册表编辑器,进入之后执行下列操作: 2.在注册表,删除如下项:HKEY_CURRENT_U ...

  10. 帝国CMS 6.5功能解密:网站安全防火墙使用说明

    有关帝国CMS新版防火墙介绍可以查看:http://bbs.phome.net/showthread-13-136169-0.html 本文为大家讲解如何使用网站防火墙:一.配置“网站防火墙”有下面两 ...