8.Hystrix-Feign配置服务降级

Posted wangfajun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了8.Hystrix-Feign配置服务降级相关的知识,希望对你有一定的参考价值。

项目中用到Feign调用服务端方法并做服务降级处理

1.application.properties配置:

feign.hystrix.enabled=true

2.pom:


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

3.客户端调message-server服务的sendMessage方法时:

package com.odao.consumer.service.message;

import com.odao.consumer.entity.MessageEntity;
import com.odao.consumer.service.message.impl.MessageServiceImpl;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@FeignClient(value = "message-server",fallback = MessageServiceImpl.class)
public interface MessageService {

    @RequestMapping(value = "sendMessage",method = RequestMethod.POST)
    String sendMessage(MessageEntity messageEntity);

}
4.MessageServiceImpl:
package com.odao.consumer.service.message.impl;

import com.odao.consumer.entity.MessageEntity;
import com.odao.consumer.service.message.MessageService;
import com.odao.plat.response.OResponse;
import com.odao.plat.response.RespStatus;
import org.springframework.stereotype.Service;

@Service
public class MessageServiceImpl implements MessageService {

    @Override
    public String sendMessage(MessageEntity messageEntity) {
        return "服务器开小差啦";
    }

}

 



以上是关于8.Hystrix-Feign配置服务降级的主要内容,如果未能解决你的问题,请参考以下文章

错误记录Android Studio 编译报错 ( Could not determine java version from ‘11.0.8‘. | Android Studio 降级 )(代码片段

SpringCloud-2.0:(11. 服务降级 - Hystrix - 解决问题)

dubbo服务降级

dubbo服务降级

Spring Cloud系列之 HystrixZuulGateway

Spring Cloud系列之 HystrixZuulGateway