添加了 openfeign 包 默认集成了 hystrix。只需要开启开关
server:
port: 9080
spring:
application:
name: member-service-openfeign
eureka:
client:
service-url:
defaultZone:
http://localhost:8761/eureka/
feign:
hystrix:
enabled: true
package com.itlaoqi.springcloud.memberserviceopenfeign.controller;
import com.itlaoqi.springcloud.memberserviceopenfeign.client.BookService;
import com.netflix.discovery.converters.Auto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
@Controller
@RequestMapping(value="/book",produces = {"application/json;charset=UTF-8"})
public class MemberController {
@Autowired
private BookService bookService;
@GetMapping("/sendMess")
@ResponseBody
public Map<String,Object> sendMess(@RequestParam("phone")String phone){
Map<String,Object> paramMap = new HashMap<>();
paramMap.put("phone",phone);
Map<String,Object> res = bookService.sendMess(paramMap);
return res;
}
}
远程调用接口
package com.itlaoqi.springcloud.memberserviceopenfeign.client;
import com.itlaoqi.springcloud.memberserviceopenfeign.client.impl.BookServiceFallback;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.Map;
//http://book-service/test?XXX
@FeignClient(name = "book-service",fallback = BookServiceFallback.class)
public interface BookService {
// 注意: RequestBody map 通过请求体发送
@PostMapping("/sendMess")
public Map<String,Object> sendMess(@RequestBody Map<String,Object> paramMap);
}
package com.itlaoqi.springcloud.memberserviceopenfeign.client.impl;
import com.itlaoqi.springcloud.memberserviceopenfeign.client.BookService;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
// 降级实现类
@Component
public class BookServiceFallback implements BookService {
public Map<String, Object> sendMess(Map<String, Object> paramMap) {
Map<String,Object> resultMap = new HashMap<>();
resultMap.put("code",-1);
resultMap.put("message","发送短信失败");
return resultMap;
}
}
springCloud(十) hystrix 和 openFeign 整合
原文:https://www.cnblogs.com/guhualin/p/13122195.html