spring-cloud-consumer-hystrix(六)
来源:互联网 发布:linux cdn服务器搭建 编辑:程序博客网 时间:2024/05/16 17:40
Hystrix是一个有关延迟和失败容错的开源库包,用来设计隔离访问远程系统端点或微服务等,防止级联爆炸式的失败,也就是由一个小问题引起接二连三扩大的疯狂的错误爆炸直至整个系统瘫痪,能够让复杂的分布式系统更加灵活具有弹性。
1、结构图
2、配置跨域访问,开发环境使用,生产环境可取消
package com.wei.qin;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.CorsRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;/** * 配置跨域访问,开发环境使用,生产环境可取消 * * @author Davis * @date 2017年6月8日 下午5:28:59 */@Configuration public class CorsConfig extends WebMvcConfigurerAdapter { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOrigins("*") .allowCredentials(true) .allowedMethods("GET", "POST", "DELETE", "PUT") .maxAge(3600); } }
3、application启动配置
package com.wei.qin;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.feign.EnableFeignClients;@SpringBootApplication@EnableDiscoveryClient@EnableFeignClientspublic class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); }}
4、propertiest配置、
spring.application.name=spring-cloud-consumer-hystrixserver.port=9001feign.hystrix.enabled=trueeureka.client.serviceUrl.defaultZone=http://localhost:8001/eureka/
5、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.wei.qin</groupId> <artifactId>spring-cloud-consumer-hystrix</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>spring-cloud-consumer-hystrix</name> <description>Demo project for Spring cloud consumer hystrix</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Dalston.RELEASE</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
6、HelloHystrixController
package com.wei.qin.consumer;import org.springframework.stereotype.Component;import org.springframework.web.bind.annotation.RequestParam;/** * * @auther dwx * * 2017年8月15日 */@Componentpublic class HelloControllerHystrix implements HelloController{ @Override public String hello(@RequestParam(value = "name") String name) { return "this messge send failed "; }}
7、HelloController
package com.wei.qin.consumer;import org.springframework.cloud.netflix.feign.FeignClient;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;/** * 消费者 * * @auther dwx * * 2017年8月15日 */@FeignClient(name= "spring-cloud-producer", fallback = HelloControllerHystrix.class)public interface HelloController { @RequestMapping(value = "/hello") public String hello(@RequestParam(value = "name") String name);}
当被调用服务接口挂掉之后,进行相应的拦截并返回信息
阅读全文
0 0
- spring-cloud-consumer-hystrix(六)
- Spring Cloud Hystrix--熔断器
- spring cloud-hystrix
- spring cloud Hystrix
- spring-cloud-hystrix(一)
- Spring Cloud Hystrix Dashboard
- spring cloud hystrix
- spring cloud hystrix dashboard
- Spring Cloud Hystrix
- Spring-Cloud 熔断器 Hystrix
- spring-cloud【Hystrix】
- spring cloud: Hystrix断路器(熔断器)
- spring cloud: Hystrix断路器(熔断器)
- Spring Cloud 断路器Hystrix实战
- Spring Cloud 中的断路器 hystrix
- spring cloud: Hystrix断路器(熔断器)
- Spring Cloud Eureka+Ribbon+Hystrix
- spring cloud: Hystrix断路器(熔断器)
- Unix哲学
- TP5整合 WorkerMan 以及 GatewayWorker
- 栈应用——平衡符号
- [POJ](3268)Silver Cow Party ---最短路径(图)
- 2017网易前端笔试题总结
- spring-cloud-consumer-hystrix(六)
- Maven打包命令
- Unable to add window -- token null is not valid; is your activity running?
- java 学习笔记之AtomicInteger类的使用
- 无需XML的hibernate
- C语言读取配置文件
- bzoj 4916: 神犇和蒟蒻
- 最近在头痛的协作问题
- Spring与mybatis三种整合方法