springcloud系列八 整合Hystrix

feign本身是支持Hystrix的,所以不需要引入其他依赖:

我们可以看看feign这个项目的依赖,就是引入这个依赖的pom.xml

要想看这个很简单,点击那个依赖进去就可以了

点进去就可以看到

<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>
    <parent>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-openfeign</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath>..</relativePath>
    </parent>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
    <name>Spring Cloud Starter OpenFeign</name>
    <description>Spring Cloud Starter OpenFeign</description>
    <url>https://projects.spring.io/spring-cloud</url>
    <organization>
        <name>Pivotal Software, Inc.</name>
        <url>https://www.spring.io</url>
    </organization>
    <properties>
        <main.basedir>${basedir}/../..</main.basedir>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-openfeign-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-commons</artifactId>
        </dependency>
        <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-core</artifactId>
        </dependency>
        <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-slf4j</artifactId>
        </dependency>
        <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-java8</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-archaius</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>
</project>

是本身就支持的,所以直接使用就可以了:

 

使用很简单:

在yml文件中开启:

feign:
  hystrix:
    enabled: true

看之前写的调用接口:

package com.cxy.service;

import com.cxy.dataObject.PersonDo;
import com.cxy.service.impl.PersonServiceImpl;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient(value = "cxy-person-service",fallback=PersonServiceImpl.class)
public interface IPersonService {
    @RequestMapping("/person/{id}")
    PersonDo getPersonDoById(@PathVariable("id")Integer id);
}

那个如果服务出现问题了就进入实现类的方法中

 然后填写一个实体类就可以了:

package com.cxy.service.impl;

import com.cxy.dataObject.PersonDo;
import com.cxy.service.IPersonService;
import org.springframework.stereotype.Component;

/***
 * @ClassName: PersonServiceImpl
 * @Description:
 * @Auther: 陈绪友
 * @Date: 2019/1/2821:09
 * @version : V1.0
 */
@Component
public class PersonServiceImpl implements IPersonService {
    @Override
    public PersonDo getPersonDoById(Integer id) {
        return new PersonDo(1,"1",1,"1");
    }
}

 不启动person服务看结果:

是在实现类中的数据,代表生效了

posted @ 2019-01-29 11:28  菩提树下的丁春秋  阅读(708)  评论(0编辑  收藏  举报