一个很简单的SpringCloud项目,集成Feign、Hystrix
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的更多相关文章
- 从零搭建一个SpringCloud项目之Feign搭建
从零搭建一个SpringCloud项目之Feign搭建 工程简述 目的:实现trade服务通过feign调用user服务的功能.因为trade服务会用到user里的一些类和接口,所以抽出了其他服务需要 ...
- 搭建简单的SpringCloud项目三:问题及解决
GitHub:https://github.com/ownzyuan/test-cloud 前篇:搭建简单的SpringCloud项目一:注册中心和公共层 搭建简单的SpringCloud项目二:服务 ...
- [.NET] 打造一个很简单的文档转换器 - 使用组件 Spire.Office
打造一个很简单的文档转换器 - 使用组件 Spire.Office [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/6024827.html 序 之前,& ...
- 搭建简单的SpringCloud项目一:注册中心和公共层
注:笔者在搭建途中其实遇见不少问题,统一放在后面的文章说明,现在的搭建是测试OK的. GitHub:https://github.com/ownzyuan/test-cloud 后续:搭建简单的Spr ...
- 一起来学Spring Cloud | 第一章 :如何搭建一个多模块的springcloud项目
在spring cloud系列章节中,本来已经写了几个章节了,但是自己看起来有些东西写得比较杂,所以重构了一下springcloud的章节内容,新写了本章节,先教大家在工作中如何搭建一个多模块的spr ...
- vs2015制作一个超级简单的MVC项目
使用vs2015制作一个超级简单的MVC项目 本文链接:https://blog.csdn.net/qq_40919762/article/details/100705314 直奔主题一,创建一个 ...
- 搭建简单的SpringCloud项目二:服务层和消费层
GitHub:https://github.com/ownzyuan/test-cloud 前篇:搭建简单的SpringCloud项目一:注册中心和公共层 后篇:搭建简单的SpringCloud项目三 ...
- java(itext) 一个很简单的PDF表格生成工具
先上个效果图 因为做的项目涉及到数据预测,其中有大量打印业务来支撑实体店的运营,因为注重的是数据,要求简洁,清晰,所以写了个很简单也很实用的工具类. 如果需要编写样式或者插入背景,都可以查阅itex官 ...
- 一个很简单的jQuery插件实例教程(菜鸟级)
很多公司的前端设计开发人员都是女孩子,而这些女孩子很多JavaScript技能都不是很好.而前端开发过程中,JavaScript技能又是必不可少的.所以,如果前端小MM正在为某个JavaScript效 ...
随机推荐
- 简单python爬虫实例
先放上url,https://music.douban.com/chart 这是豆瓣的一个音乐排行榜,这里爬取了左边部分的歌曲排行榜部分,爬虫很简单,就用到了beautifulsoup和request ...
- 【Oracle】Windows启动
cd D:\app\Administrator\product\\dbhome_1\BIN D: sqlplus /nolog conn sys/system as sysdba startup pf ...
- 水晶报表报错:log4net初始值问题
运行水晶报表的程序一直出错,报错如下: System.TypeInitializationException: “CrystalDecisions.ReportSource.ReportSourceF ...
- Chrome 禁止 http 自动转化为https
Chrome 浏览器 地址栏中输入 chrome://net-internals/#hsts 在 Delete domain security policies 中输入项目的域名,并 Delete 删 ...
- string方法介绍
#_*_coding:utf-8_*_#作者:王佃元#日期:2019/12/9#string操作print('hello'*2) #乘法操作,输出对应次数print('helloworld'[2:]) ...
- [LeetCode] 642. Design Search Autocomplete System 设计搜索自动补全系统
Design a search autocomplete system for a search engine. Users may input a sentence (at least one wo ...
- [LeetCode] 174. Dungeon Game 地牢游戏
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
- vue 图片放大镜效果
插件名称:vue-photo-zoom-pro https://github.com/Mater1996/vue-photo-zoom-pro 效果图 使用: <template> &l ...
- Oracle 10G RAC集群安装
一,基本环境配置 01,hosts cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.loc ...
- tomcat 指定(自定义)JDK路径的两种方式
1.情景展示 tomcat7使用jdk1.7:tomcat8使用jdk1.8.两个tomcat在一台机器下同时启动,你会发现这两个tomcat使用的是一个版本的jdk, 那就是你配置过的JAVA_ ...