博主
258
258
258
258
专辑

第十一节 Feign与Hystrix

亮子 2021-06-09 02:05:33 6252 0 0 0

1、添加Hystrix依赖

        <!-- hystrix -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

注意:这个依赖可以不加,因为Feign默认已经包含了hystrix功能。

2、修改属性文件启用Hystrix

## feign启用hysrix
feign.hystrix.enabled=true

3、定义Feign接口的实现类

package com.mazong.serverbloguser.service.impl;

import com.mazong.serverbloguser.service.IUserService;

@Component
public class IUserServiceImpl implements IUserService {

    //
    public String hello() {

        return "feign(hello):降级了";
    }
}

4、在Feign接口上增加降级注解属性

package com.mazong.serverbloguser.service;

import com.mazong.serverbloguser.service.impl.IUserServiceImpl;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(value = "server-blog-article", fallback = IUserServiceImpl.class)
@Component(value = "iUserService")
public interface IUserService {

    @RequestMapping(value = "/hello")
    public String hello();
}

5、运行效果

图片alt