2019独角兽企业重金招聘Python工程师标准>>>

SpringCloud中fegin的使用是非常优雅的,被大多数面向接口编程的程序员喜欢,而且熔断(hystrix)则能在服务不可达时快速给客户端一个默认响应。可以说是很不错的机制,今天我们就来了解一下这两个。

1,准备

①,用idea建立一个空工程

②,添加一个pom类型的父模块

③,给父工程添加子模块即可

④,结构如下

⑤,eclipse就更简单了,直接建立一个pom类型的父工程,右键父工程给其添加子模块即可

2,父工程配置

①,pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.example</groupId><artifactId>spc-parent</artifactId><version>0.0.1-SNAPSHOT</version><!--这里一定要是pom,不然公共的api模块会无法install--><packaging>pom</packaging><name>spc-parent</name><description>Demo project for Spring Boot</description><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target></properties><!-- 父类工程管理机制 --><dependencyManagement><dependencies><!--springcloud 依赖--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Dalston.SR1</version><type>pom</type><scope>import</scope></dependency><!--springboot依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>1.5.12.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><resources><resource><directory>src/main/resources</directory><!-- 这个路径下的文件运行访问 --><filtering>true</filtering></resource></resources><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-resources-plugin</artifactId><configuration><delimiters><delimit>$</delimit><!-- 访问被$包围的值 --></delimiters></configuration></plugin></plugins></build></project>

3,api模块

①,pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><!--保留名字--><artifactId>api</artifactId><!--父工程--><parent><groupId>com.example</groupId><artifactId>spc-parent</artifactId><version>0.0.1-SNAPSHOT</version></parent><dependencies><!-- 依赖fegin --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-feign</artifactId><version>1.3.1.RELEASE</version></dependency></dependencies>
</project>

②,用到的pojo

import java.util.Date;public class Ticket {private Integer id;private String name;private Date buyDate;
}

③,定义接口

import com.example.api.bean.Ticket;
import com.example.api.factory.TickClientServiceFallbackFactory;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;import java.util.List;//必须指定服务名,指定熔断统一处理类
@FeignClient(value = "ticket-provider",fallbackFactory =TickClientServiceFallbackFactory.class)
public interface TicketService {//  以下为暴露的服务,与提供者的映射路径一致@RequestMapping(value = "/ticket/add", method = RequestMethod.POST)public boolean add(Ticket ticket);@RequestMapping(value = "/ticket/get/{id}", method = RequestMethod.GET)public Ticket get(@PathVariable("id") Long id);@RequestMapping(value = "/ticket/list", method = RequestMethod.GET)public List<Ticket> list();}

④,熔断处理类

import com.example.api.bean.Ticket;
import com.example.api.service.TicketService;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;import java.util.Date;
import java.util.List;//加入到ioc容器
@Component
public class TickClientServiceFallbackFactory implements FallbackFactory<TicketService> {@Overridepublic TicketService create(Throwable cause) {
//        通过匿名内部类的方式,如果出错默认结果return new TicketService() {@Overridepublic boolean add(Ticket ticket) {System.out.println("添加失败^_^");return false;}@Overridepublic Ticket get(Long id) {Ticket ticket = new Ticket();ticket.setId(1);ticket.setBuyDate(new Date());ticket.setName("请稍后重试");return ticket;}@Overridepublic List<Ticket> list() {return null;}};}
}

4,注册模块

①,pom.xml

 <artifactId>eureka</artifactId><parent><groupId>com.example</groupId><artifactId>spc-parent</artifactId><version>0.0.1-SNAPSHOT</version></parent><dependencies><!--eureka-server服务端 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka-server</artifactId></dependency></dependencies>

②,application.properties

#eueka 主机名
eureka.instance.hostname=eureka-service
#不注册自己
eureka.client.register-with-eureka=false
#获取服务
eureka.client.fetch-registry=false
#提供者和消费者的注册地址
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/server.port=8761

③,主配置类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
//开启eureka
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {public static void main(String[] args) {SpringApplication.run(EurekaApplication.class, args);}
}

5,消费者配置

①,pom.xml

 <artifactId>customer</artifactId><parent><groupId>com.example</groupId><artifactId>spc-parent</artifactId><version>0.0.1-SNAPSHOT</version></parent><dependencies><!-- hystrix --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-hystrix</artifactId></dependency><dependency><groupId>com.springcloud</groupId><artifactId>microservicecloud-api</artifactId><version>${project.version}</version></dependency><!-- Feign相关 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-feign</artifactId></dependency><!-- eureka相关 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency></dependencies>

②,application.properties

server.port=8201spring.application.name=ticket-customer-fegin
eureka.instance.prefer-ip-address=true
#注册地址
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
#开启熔断
feign.hystrix.enabled=true

③,主配置类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
//注册到eureka
@EnableEurekaClient
@SpringBootApplication
//指定fegin扫描的包,需要即扫描api工程中熔断配置类
@EnableFeignClients(basePackages= {"com.example.api"})
//需要扫描api工程及本工程注册的bean
@ComponentScan("com.example")
public class CustomerApplication {public static void main(String[] args) {SpringApplication.run(CustomerApplication.class, args);}
}

④,controller层代码编写

import com.example.api.bean.Ticket;
import com.example.api.service.TicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.List;@RestController
public class CustomerController {@Autowiredprivate TicketService ticketService;@RequestMapping(value = "/fegin/add")public boolean add(Ticket ticket) {return ticketService.add(ticket);}@RequestMapping(value = "/fegin/get/{id}")public Ticket get(@PathVariable("id") Long id) {return ticketService.get(id);}@RequestMapping(value = "/fegin/list")public List<Ticket> list() {return ticketService.list();}
}

6,提供者配置

①,pom.xml

<artifactId>provider</artifactId><parent><groupId>com.example</groupId><artifactId>spc-parent</artifactId><version>0.0.1-SNAPSHOT</version></parent><dependencies><!-- 引入自己定义的api通用包,可以使用Dept部门Entity --><dependency><groupId>com.example</groupId><artifactId>api</artifactId><version>${project.version}</version></dependency><!-- 将微服务provider侧注册进eureka --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency></dependencies>

②,application.properties

server.port=8002
#服务名
spring.application.name=ticket-provider
#使用ip进行注册
eureka.instance.prefer-ip-address=true
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

③,主配置类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@EnableEurekaClient //启动后注册到eureka
@SpringBootApplication
public class ProviderApplication {public static void main(String[] args) {SpringApplication.run(ProviderApplication.class, args);}
}

④,controller层代码


import com.example.api.bean.Ticket;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;import java.util.Collections;
import java.util.Date;
import java.util.List;@RestController
public class TicketController {@RequestMapping(value = "/ticket/add", method = RequestMethod.POST)public boolean add(Ticket ticket) {System.out.println("添加成功");return true;}@RequestMapping(value = "/ticket/get/{id}", method = RequestMethod.GET)public Ticket get(@PathVariable("id")Long id) {Ticket ticket = new Ticket();ticket.setId(1);ticket.setBuyDate(new Date());ticket.setName("《男儿当自强》");return ticket;}@RequestMapping(value = "/ticket/list", method = RequestMethod.GET)public List<Ticket> list() {System.out.println("找到了好多数据。。。。");return Collections.emptyList();}
}

7,测试

①,启动注册工程(eureka)

②,启动消费者工程,此时已经可以看到效果

③,访问消费者接口,看到如下信息,说明熔断起效果了

④,启动提供者工程,等几秒后,便能正常访问提供者

转载于:https://my.oschina.net/u/3574106/blog/1815646

简单使用SpringCloud的fegin和熔断hystrix相关推荐

  1. 史上最简单的SpringCloud教程 | 第四篇:断路器(Hystrix)

    转:https://blog.csdn.net/forezp/article/details/69934399 最新版本: 史上最简单的SpringCloud教程 | 第四篇:断路器(Hystrix) ...

  2. 史上最简单的SpringCloud教程 | 第四篇:断路器(Hystrix)--有BUG,注意看我的备注

    转载请标明出处:  http://blog.csdn.net/forezp/article/details/69934399  本文出自方志朋的博客 在微服务架构中,根据业务来拆分成一个个的服务,服务 ...

  3. 史上最简单的SpringCloud教程 | 第四篇:断路器(Hystrix)--里面有BUG,所以我转载改一下

    017年04月09日 21:14:05 阅读数:271535 转载请标明出处:  http://blog.csdn.net/forezp/article/details/69934399  本文出自方 ...

  4. 史上最简单的SpringCloud教程 | 第四篇:断路器(Hystrix)(Finchley版本)

    转载请标明出处: http://blog.csdn.net/forezp/article/details/81040990 本文出自方志朋的博客 个人博客纯净版:https://www.fangzhi ...

  5. 手写springcloud|熔断 hystrix

    文章目录 手写springcloud|熔断hystrix github示例代码 服务调用和熔断的方式 熔断的流程 Spring Cloud Hystrix 熔断细节 基本步骤 原版使用核心代码 实现过 ...

  6. springcloud+springboot+Eureka+Feign+Ribbon+Hystrix+Zuul

    Springcloud集成Eureka Eureka服务端和客户端 本实例采用springboot,eureka和feign/ribbon,hystrix,zuul,mybatis,redis 1. ...

  7. SpringCloud(3)---最简单的 SpringCloud 教程

    史上最简单的 SpringCloud 教程 案例全部采用Spring Boot 1.5.x ,Spring Cloud版本为Dalston.RELEASE 码农下载:https://git.oschi ...

  8. 史上最简单的 SpringCloud 教程

    关注公众号"风色年代"订阅更多精彩文章,本博大部分文章为转载并已标明原文出处,如有再转敬请保留,请自觉尊重原创作者的劳动成果! https://blog.csdn.net/fore ...

  9. Spring Cloud---服务熔断Hystrix

    哈喽大家好我是yangerkong!今天跟大家探讨下微服务中的熔断机制. 本文中部分介绍和部分图片摘自官网,官网地址:Home · Netflix/Hystrix Wiki · GitHub Spri ...

最新文章

  1. Python之路--Django--form组件与model form组件
  2. 《大道至简》阅读笔记二
  3. 批量创建同义词并授权
  4. pipe row的用法, Oracle split 函数写法.
  5. COM 组件设计与应用(七)
  6. Python 进程互斥锁 Lock - Python零基础入门教程
  7. SpringBoot2.1.5 (5)---快速构建SpringBoot 项目的两种方式
  8. 安装CentOS7虚拟机
  9. java获取键盘输入
  10. 95后妈妈8成是全职,爸爸去哪了?
  11. 京东线报接口 全网一手线报全网(京东,淘宝,天猫)最全优惠信息
  12. 南京大学计算机考研信息汇总
  13. 智慧工厂智能制造供应链解决方案
  14. Windows下完成SCTP简单抓包
  15. 百度磁盘搜索和git、ssh的试用
  16. 关于获取安卓手机MAC地址的问题
  17. 如何才能学好UI设计 Logo设计有哪几个要素
  18. 浅谈 MVC、MVP 和 MVVM 架构模式
  19. 抖音视频真的能赚到钱吗?抖音真的有带货力吗?国仁网络资讯
  20. Vue.js中$refs{}获取DOM元素

热门文章

  1. eclipse链接mysql数据池配置_Tomcat+mysql+eclipse数据库连接池配置
  2. SAP WM高阶Storage Location Control
  3. 放心,GPT-3 不会“杀死”编程
  4. SAP ECM的相关设定(ECN)
  5. 到2030年AI会变成怎样?专家给出10大预测
  6. ICDM 2019最佳论文:从图片、文本到网络结构数据翻译,一种新型的多属性图翻译模型
  7. 苏黎世大学 AI 新研究:仅用少量样本生成高质量光声图像
  8. 曾经百度大佬吹过的牛实现了,看完这个,带你搞定AI前沿技术
  9. 人工智能如何彻底改变全球物流和供应链管理
  10. 心得丨程序员们,AI来了,机会来了,危机也来了,我们该咋办?