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. day23_7.29 多态和类的内置方法

    一.多态 在现实生活中,多态也会体现.如对于水这种物质,有固态:冰,液态:常态,气态:水蒸气, 在程序中,其官方定义是:多个不同对象可以相应同一方法,产生不同的结果. 而在python中,多态不是一个 ...

  2. python -m pip install --upgrade pip

    升级pip后报错 TypeError: 'module' object is not callable 原因 存在两个版本的pip 先把原先版本的卸载了: python -m pip uninstal ...

  3. zz2019年主动学习有哪些进展?答案在这三篇论文里

    2019年主动学习有哪些进展?答案在这三篇论文里 目前推广应用的机器学习方法或模型主要解决分类问题,即给定一组数据(文本.图像.视频等),判断数据类别或将同类数据归类等,训练过程依赖于已标注类别的训练 ...

  4. hdu1495-非常可乐-(倒水问题bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1495 题解: 1.最少次数?江湖套路,bfs.2.怎么倒?从一个杯子倒到另一个杯子.3.倒多少?因为没有刻度,所 ...

  5. 【java】isEmpty VS isBlank 的区别

  6. [LeetCode] 198. House Robber 打家劫舍

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  7. 解决VMware虚拟机中centos 7无法上网的问题

    在WMware中安装centos 7后发现无法安装软件,开始以为是镜像服务器的问题,后来通过ping之后发现根本没办法连接到网络.由于很多设置都是默认的,并且虚拟机也是NAT模式,和电脑主机共享网络, ...

  8. Python爬取豆瓣电影top

    Python爬取豆瓣电影top250 下面以四种方法去解析数据,前面三种以插件库来解析,第四种以正则表达式去解析. xpath pyquery beaufifulsoup re 爬取信息:名称  评分 ...

  9. 标签一致项(LC-KSVD)-全文解读

    Learning A Discriminative Dictionary for Sparse Coding via Label Consistent K-SVD 1,同步学习判决字典和线性分类器 2 ...

  10. Mybatis成为Java互联网时代首选持久框架的原因

    持久层可以将业务数据存储到磁盘,具备长期存储能力,只要磁盘不损坏(大部分的重要数据都会有相关的备份机制),在断电或者其他情况下,重新开启系统仍然可以读取这些数据.一般执行持久任务的都是数据库系统.持久 ...