feat(openFeign): 配置请求和响应压缩,加入错误回调内容
This commit is contained in:
parent
9d49cea720
commit
1a18153ed7
|
@ -1,5 +1,6 @@
|
|||
package cn.bunny.config;
|
||||
|
||||
import feign.Logger;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
@ -13,4 +14,12 @@ public class CloudConfiguration {
|
|||
public RestTemplate restTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置openFeign 日志级别
|
||||
*/
|
||||
@Bean
|
||||
public Logger.Level feignLoggerLevel() {
|
||||
return Logger.Level.FULL;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package cn.bunny.controller;
|
||||
|
||||
import cn.bunny.feign.CloudFeignClient;
|
||||
import cn.bunny.feign.CloudFeignClient1;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -16,12 +17,21 @@ public class CloudController {
|
|||
@Autowired
|
||||
private CloudFeignClient cloudFeignClient;
|
||||
|
||||
@Autowired
|
||||
private CloudFeignClient1 cloudFeignClient1;
|
||||
|
||||
@Operation(summary = "本体接口调用", description = "feignCloud2")
|
||||
@GetMapping("feignCloud1")
|
||||
public String getFeignCloud1(Long id) {
|
||||
return "feignCloud1:" + id;
|
||||
}
|
||||
|
||||
@Operation(summary = "调用cloud2中的接口")
|
||||
@GetMapping("feignAccept1")
|
||||
public String feignAccept1() {
|
||||
return cloudFeignClient1.feignAccept1();
|
||||
}
|
||||
|
||||
@Operation(summary = "调用服务2中接口", description = "feignByCloud2")
|
||||
@GetMapping("feignByCloud2")
|
||||
public String feignByCloud2(Long id) {
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
package cn.bunny.feign;
|
||||
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
@FeignClient(value = "service-cloud2", path = "/api")
|
||||
public interface CloudFeignClient1 {
|
||||
@GetMapping("feignAccept1")
|
||||
String feignAccept1();
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package cn.bunny.feign.fallback;
|
||||
|
||||
import cn.bunny.feign.CloudFeignClient1;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class CloudFeignClientFallBack implements CloudFeignClient1 {
|
||||
|
||||
@Override
|
||||
public String feignAccept1() {
|
||||
return "出错了的回调";
|
||||
}
|
||||
}
|
|
@ -6,3 +6,23 @@ spring:
|
|||
name: service-cloud1
|
||||
profiles:
|
||||
active: dev
|
||||
cloud:
|
||||
openfeign:
|
||||
client:
|
||||
config:
|
||||
service-cloud1: # 针对自己的这个配置
|
||||
logger-level: full
|
||||
connect-timeout: 3000
|
||||
read-timeout: 5000
|
||||
default: # 针对默认配置
|
||||
logger-level: full
|
||||
connect-timeout: 3000
|
||||
read-timeout: 5000
|
||||
compression:
|
||||
# 压缩请求
|
||||
request:
|
||||
enabled: true
|
||||
min-request-size: 4096 # 压缩最小值 为4096 = 4KB
|
||||
# 压缩响应请求
|
||||
response:
|
||||
enabled: true
|
|
@ -22,6 +22,12 @@ public class CloudController {
|
|||
return "feign:" + id;
|
||||
}
|
||||
|
||||
@Operation(summary = "接受其他接口调用1", description = "接受其他接口调用1")
|
||||
@GetMapping("feignAccept1")
|
||||
public String feignAccept1() {
|
||||
return "feignAccept1---成功";
|
||||
}
|
||||
|
||||
@Operation(summary = "调用服务1中接口", description = "feignByCloud1")
|
||||
@GetMapping("feignByCloud1")
|
||||
public String feignByCloud1(Long id) {
|
||||
|
|
|
@ -2,10 +2,12 @@ package cn.bunny.feign;
|
|||
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
@FeignClient(value = "service-cloud1")
|
||||
// 如果公共路径相同需要加上 path = "/api"
|
||||
@FeignClient(value = "service-cloud1", path = "/api")
|
||||
public interface CloudFeignClient {
|
||||
|
||||
@GetMapping("feignCloud1")
|
||||
String getFeignCloud1(Long id);
|
||||
String getFeignCloud1(@RequestParam Long id);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue