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);}

当被调用服务接口挂掉之后,进行相应的拦截并返回信息

这里写图片描述

这里写代码片