Feign的功能:这是个消费者,根据服务注册在Eureka的ID去找到该服务,并调用接口
Hystrix的功能:熔断器,假如A服务需要调用B服务的/cities接口获取数据,那就在A服务的controller里声明@HystrixCommand,如果B服务的/cities接口挂了,就返回一个自定义的值

项目结构

[root@node01 cloud]# tree weather/
weather/
├── Application.java #主程序启动入口
├── controller
│   ├── CityController.java #控制路由,比如访问127.0.0.1:8080/cities, 这里调用cityClient方法
└── service
└── CityClient.java #具体方法的实现,这里就具体实现cityClient方法(interface)

Application.java

package com.waylau.spring.cloud.weather;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; #引入熔断器
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; #让eureka能发现自己
import org.springframework.cloud.netflix.feign.EnableFeignClients; #引入feign @SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients #声明这个程序引入FeignClient
@EnableCircuitBreaker #声明这个程序引入熔断器
public class Application { public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

controller/CityController.java

package com.waylau.spring.cloud.weather.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.waylau.spring.cloud.weather.service.CityClient; #引用service/CityClient.java @RestController
public class CityController {
@Autowired
private CityClient cityClient; @GetMapping("/cities") #控制路由,比如访问127.0.0.1:8080/cities
@HystrixCommand(fallbackMethod="defaultCities") #给另外一个服务设置hystrix熔断器,并声明fallbackMethod(回调方法)假如另外一个服务挂了,就调用defaultCities方法
public String listCity() {
// 通过Feign客户端来查找
String body = cityClient.listCity(); #调用service里CityClient.java的cityClient方法,获取城市数据
return body; #返回,访问127.0.0.1:8080/cities就能返回城市数据
} public String defaultCities() {
return "City Data Server is down!";
}
}

service/CityClient.java(消费者)

用Feign去另一个接口获取数据

package com.waylau.spring.cloud.weather.service;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping; @FeignClient("msa-weather-city") #msa-weather-city是msa-weather-city服务注册到eureka的ID,通过这个ID就能访问到msa-weather-city微服务
public interface CityClient { @GetMapping("/cities") #调用msa-weather-city微服务里的/cities接口
String listCity(); #调用msa-weather-city微服务里的/cities接口里的listCity()方法
}

application.properties

spring.application.name: micro-weather-eureka-client-feign-hystrix

eureka.client.serviceUrl.defaultZone: http://localhost:8761/eureka/

feign.client.config.feignName.connectTimeout: 5000
feign.client.config.feignName.readTimeout: 5000

测试,条件为另一个服务可用时,我们是能调用的

测试.条件为另一个服务不可以时,我们是不能调用的,所以fallback

一个很简单的SpringCloud项目,集成Feign、Hystrix的更多相关文章

  1. 从零搭建一个SpringCloud项目之Feign搭建

    从零搭建一个SpringCloud项目之Feign搭建 工程简述 目的:实现trade服务通过feign调用user服务的功能.因为trade服务会用到user里的一些类和接口,所以抽出了其他服务需要 ...

  2. 搭建简单的SpringCloud项目三:问题及解决

    GitHub:https://github.com/ownzyuan/test-cloud 前篇:搭建简单的SpringCloud项目一:注册中心和公共层 搭建简单的SpringCloud项目二:服务 ...

  3. [.NET] 打造一个很简单的文档转换器 - 使用组件 Spire.Office

    打造一个很简单的文档转换器 - 使用组件 Spire.Office [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/6024827.html 序 之前,& ...

  4. 搭建简单的SpringCloud项目一:注册中心和公共层

    注:笔者在搭建途中其实遇见不少问题,统一放在后面的文章说明,现在的搭建是测试OK的. GitHub:https://github.com/ownzyuan/test-cloud 后续:搭建简单的Spr ...

  5. 一起来学Spring Cloud | 第一章 :如何搭建一个多模块的springcloud项目

    在spring cloud系列章节中,本来已经写了几个章节了,但是自己看起来有些东西写得比较杂,所以重构了一下springcloud的章节内容,新写了本章节,先教大家在工作中如何搭建一个多模块的spr ...

  6. vs2015制作一个超级简单的MVC项目

    使用vs2015制作一个超级简单的MVC项目   本文链接:https://blog.csdn.net/qq_40919762/article/details/100705314 直奔主题一,创建一个 ...

  7. 搭建简单的SpringCloud项目二:服务层和消费层

    GitHub:https://github.com/ownzyuan/test-cloud 前篇:搭建简单的SpringCloud项目一:注册中心和公共层 后篇:搭建简单的SpringCloud项目三 ...

  8. java(itext) 一个很简单的PDF表格生成工具

    先上个效果图 因为做的项目涉及到数据预测,其中有大量打印业务来支撑实体店的运营,因为注重的是数据,要求简洁,清晰,所以写了个很简单也很实用的工具类. 如果需要编写样式或者插入背景,都可以查阅itex官 ...

  9. 一个很简单的jQuery插件实例教程(菜鸟级)

    很多公司的前端设计开发人员都是女孩子,而这些女孩子很多JavaScript技能都不是很好.而前端开发过程中,JavaScript技能又是必不可少的.所以,如果前端小MM正在为某个JavaScript效 ...

随机推荐

  1. mysql版本引起的驱动问题

    mysql版本引起的驱动问题 com.mysql.jdbc.Driver 是 mysql-connector-java 5中的, com.mysql.cj.jdbc.Driver 是 mysql-co ...

  2. React组件简单介绍

    组件是 React 的核心,因此了解如何利用它们对于创建优秀的设计结构至关重要. 组件之间传递信息方式: 1.(父组件)向(子组件)传递信息 2.(子组件)向(父组件)传递信息 3.没有任何嵌套关系的 ...

  3. 前端Vue项目——课程详情页面实现

    一.详情页面路由跳转 应用 Vue Router 编程式导航通过 this.$router.push() 来实现路由跳转. 1.绑定查看详情事件 修改 src/components/Course/Co ...

  4. BBS 03day

    目录 BBS_03 day: 自定义标签 过滤器: 文章的点赞,点彩功能: 文章的评论功能 transaction用法: 自定义 标签代码展示: BBS_03 day: 自定义标签 过滤器: --&g ...

  5. [LeetCode] 164. Maximum Gap 求最大间距

    Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...

  6. 原创|强大!Nginx 配置在线一键生成“神器”

    Nginx作为一个轻量级的HTTP服务器,相比Apache优势也是比较明显的,在性能上它占用资源少,能支持更高更多的并发连接,从而达到提高访问效率;在功能上它是一款非常优秀的代理服务器与负载均衡服务器 ...

  7. Laravel本地环境搭建:Homestead开发环境的部署

    Laravel框架在php开发过程是不断进行优化的,当然也包括了本地环境的开发,下面我们就来具体看看laravel框架中的Homestead 开发环境的部署内容. 首先白以下几个概念 VirtualB ...

  8. ROS向节点传递参数

    ROS的节点有很多中调用方式,包括rosrun,launch,直接运行等,向节点内传递参数的方式也有很多. 1. rosrun + 参数服务器传递 ros::init(argc, argv, &quo ...

  9. 折腾linux随笔 之 关闭Budgie默认自动隐藏应用的菜单栏 与 Gnome系桌面应用菜单无内容解决

    关闭Budgie默认自动隐藏应用菜单栏 首选项 -> 设置 -> 通用辅助功能 -> 打开 始终显示通用辅助菜单 后的开关 -> 注销桌面重新登录. done. 解决Gnome ...

  10. 关于eclipse SE版本不支持建立web工程的问题

    关于eclipse SE版本不支持建立web工程的问题 我们会发现 JAVA eclipse SE版本无法建立 Web 程序的问题...... 最好的解决方法就是下载一个myeclipse 或 Jav ...