前言

本文采用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. Kubernetes 系列(四):使用Traefik访问.net core api

    一. 准备 本篇的要求是在前三篇的基础上已经搭建好的本地k8s以及部署了Traefik,我们将会使用Traefik Ingress来访问.net core api,比较简单,做个记录,如果还没有搭建k ...

  2. 利用Code::Blocks搭建64位C++开发平台

    0.前言 随着64位计算机的普及,编写64位程序成为程序员基本的要求.我在<体验Code::Blocks下的C++编程>中描述了利用Code::Blocks官方提供的封装了编译器的安装包( ...

  3. 记一次linux Docker网络故障排除经历

    背景: 之前做了一个项目,需要在容器内访问宿主机提供的Redis 服务(这是一个比较常见的应用场景哈), 常规方案: ①   主机网络(docker run --network=host): 完全应用 ...

  4. ueditor的初始化赋值

    ue.ready(function () {ue.setContent('初始内容'); //赋值给UEditor });

  5. 即学即用的 30 段 Python 实用代码

    [☞ 分享:最全最新的Python学习大礼包 ☜ 点击查看](https://mp.weixin.qq.com/s?__biz=MzU2MzgyODA4OA==&mid=100000592&a ...

  6. docker镜像制作必备技能

    正文 使用过docker的都知道dockerfile,其用于定义制作镜像的流程,由一系列命令和参数构成的脚本,这些命令应用于基础镜像并最终创建一个新的镜像.可参考往期文章学习:docker基础知识整理 ...

  7. python模块常用用法

    1.time模块(※※※※) import time #导入时间模块 print(time.time()) #返回当前时间的时间戳,可用于计算程序运行时间 print(time.localtime() ...

  8. python编程基础之二

    交互式: 此处以windows为例:开始->运行->cmd,输入python 交互式界面 优点:即时,所见即所得 缺点:代码不可复用,根本无法进行维护 退出:exit() 代码是顺序执行: ...

  9. 部署主从dns

    主机部署:yum安装DNS服务和依赖 [admin@haifly-bj-dns1 ~]$ sudo yum install bind-chroot启动named-chroot服务 [admin@hai ...

  10. 《深入理解Java虚拟机》-----第12章 Java内存模型与线程

    概述 多任务处理在现代计算机操作系统中几乎已是一项必备的功能了.在许多情况下,让计算机同时去做几件事情,不仅是因为计算机的运算能力强大了,还有一个很重要的原因是计算机的运算速度与它的存储和通信子系统速 ...