项目中用到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 "服务器开小差啦";
}
}