springboot2 接口化 http 跨项目调用之 fegin
作者:互联网
1,依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2,启动类添加注解:@EnableFeignClients
3,添加配置启动 fegin
# feign.hystrix.enabled: 启动 fegin 远程调用的熔断机制
feign:
hystrix:
enabled: true
4,添加接口,其中 @FeignClient 注解中的 name 为被调用项目的名称,fallback 指定容错的实现类
package com.hwq.web.server.fegin;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient(name = "global-api", fallback = SnowFlakeFeginImpl.class)
public interface SnowFlakeFegin {
@GetMapping("snow-flake/id")
String getId();
}
5,容错的实现类
package com.hwq.web.server.fegin;
import org.springframework.stereotype.Component;
@Component
public class SnowFlakeFeginImpl implements SnowFlakeFegin {
@Override
public String getId() {
throw new RuntimeException("获取主键失败");
}
}
标签:web,http,fegin,springframework,springboot2,import,org,cloud 来源: https://www.cnblogs.com/lovling/p/13206278.html