1.配置类
pom
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- common -->
<dependency>
<groupId>org.example</groupId>
<artifactId>common</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
yml
server:
port: 8082
spring:
application:
name: cloud-order-service-openfeign
eureka:
client:
register-with-eureka: false
fetch-registry: true
service-url:
#defaultZone: http://localhost:7001/eureka 单机版
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka # 集群
ribbon:
ReadTimeout: 5000 # 超时配置
ConnectTimeout: 5000
logging: #日志配置
level:
com.cloud.openfeign.service.OpenFeignService: debug
主启动类
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableFeignClients
public class Application8082 {
public static void main(String[] args) {
SpringApplication.run(Application8082.class, args);
}
}
2.业务层
controller : 调用服务端接口
@RestController
public class OpenFeignController {
@Autowired
private OpenFeignService openFeignService;
@GetMapping("/consumer/openfeign/{id}")
public CommonResult selectPaymentById(@PathVariable(value = "id") Long id){
//http://localhost:8082/consumer/openfeign/1
CommonResult result = openFeignService.selectPaymentById(id);
return CommonResult.success().setData(result);
}
@GetMapping("/consumer/openfeign/timeout")
public void timeout(){
//http://localhost:8082/consumer/openfeign/timeout
openFeignService.timeout();
}
}
接口层
@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")
public interface OpenFeignService {
@GetMapping(value = "/payment/{id}")
CommonResult selectPaymentById(@PathVariable(value = "id") Long id);
@GetMapping("/payment/timeout")
void timeout();
}
配置类
@Configuration
public class FeignLogConfig {
// NONE, 不记录
// BASIC, 仅记录请求方法、URL以及响应状态码和执行时间
// HEADERS, BASIC + 记录请求和响应的头信息
// FULL; 记录所有请求与响应的明细, 包括头信息、 请求体、 元数据等
@Bean
Logger.Level feignLog() {
return Logger.Level.FULL;
}
}