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. 学习:逆向PUSH越界/INT 68/反调试导致的程序

    自己根据shark恒老师的分析,总结一下: 一般反调试自动关闭程序利用的函数有: 1.CreateToolhelp32Snapshot 2.FindWindow 3.ExitProcess 4.Pos ...

  2. elasticsearch 简单demo RestHighLevelClient LowLeveClient

    参考: https://www.elastic.co/guide/en/elasticsearch/client/java-rest/6.7/java-rest-low.html www.elasti ...

  3. The 13th Chinese Northeast Collegiate Programming Contest

    题解: solution Code: A. Apple Business #include<cstdio> #include<algorithm> #include<ve ...

  4. postgres 字符操作补位,字符切割

    补位: ,'); -- 字符切割 并取值: )

  5. Thread笔记

    Thread笔记 Thread——fork:https://www.cnblogs.com/noonjuan/diary/2019/08/03/11296217.html Thread——multip ...

  6. vue 图片放大镜效果

    插件名称:vue-photo-zoom-pro https://github.com/Mater1996/vue-photo-zoom-pro 效果图  使用: <template> &l ...

  7. 这些个适合oier的网站丫太有趣了吧(不定期更新中)(update.2019年11月1日)

    //部分来源于:Sophon 的博客 .Smoggy 的博客.lahlah 的空间. //大佬大佬%%%↗↗↗ oier必备!!!(你值得拥有d=====( ̄▽ ̄*)b) 骗分导论 - Vijos V ...

  8. it's over | 2019 CSP-S 第一轮认证

    不知道自己有没有凉,毕竟我们省这么弱(据说有的省80都悬... 其实这几天对初赛基本没什么感觉,可能是没给自己多大压力吧,倒是班上的一群同学似乎比我们还着急,我们的数学课代表兼数竞大佬特意给我画了吉祥 ...

  9. Scala字符串插值 - StringContext

    翻译自:STRING INTERPOLATION 简介 自2.10.0版本开始,Scala提供了一种新的机制来根据数据生成字符串:字符串插值.字符串插值允许使用者将变量引用直接插入处理过的字面字符中. ...

  10. mqtt数据采集器

    MQTT是一种发布(publish)/订阅(subscribe)协议,MQTT协议采用发布/订阅模式,所有的物联网终端都通过TCP连接到云端,云端通过主题的方式管理各个设备关注的通讯内容,负责将设备与 ...