feat(openFeign): 配置请求拦截器和云端的属性内容
This commit is contained in:
parent
f4fc71e738
commit
1b4c938c38
|
@ -1,11 +1,18 @@
|
|||
package cn.bunny.config;
|
||||
|
||||
import com.alibaba.cloud.nacos.NacosConfigManager;
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
import com.alibaba.nacos.api.config.listener.Listener;
|
||||
import feign.Logger;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
@Configuration
|
||||
public class CloudConfiguration {
|
||||
|
||||
|
@ -22,4 +29,26 @@ public class CloudConfiguration {
|
|||
public Logger.Level feignLoggerLevel() {
|
||||
return Logger.Level.FULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* 当配置文件更新时的操作
|
||||
*/
|
||||
@Bean
|
||||
public ApplicationRunner applicationRunner(NacosConfigManager nacosConfigManager) {
|
||||
return args -> {
|
||||
ConfigService configService = nacosConfigManager.getConfigService();
|
||||
configService.addListener("service-order.yml", "DEFAULT_GROUP", new Listener() {
|
||||
@Override
|
||||
public Executor getExecutor() {
|
||||
return Executors.newFixedThreadPool(4);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void receiveConfigInfo(String s) {
|
||||
System.out.println("变化的信息:" + s);
|
||||
System.out.println("====================发送邮件。。。====================");
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
package cn.bunny.controller;
|
||||
|
||||
import cn.bunny.properties.OrderProperties;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api")
|
||||
@Tag(name = "获取配置属性名称", description = "获取配置属性名称")
|
||||
public class PropertiesController {
|
||||
|
||||
@Autowired
|
||||
private OrderProperties orderProperties;
|
||||
|
||||
@GetMapping("config")
|
||||
@Operation(summary = "获取云端配置属性", description = "获取云端配置属性")
|
||||
public String getConfig() {
|
||||
return "timeout:" + orderProperties.getTimeout() + ";autoConfirm:" + orderProperties.getConfirm() + ";dbUrl:" + orderProperties.getDbUrl();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package cn.bunny.intercept;
|
||||
|
||||
import feign.RequestInterceptor;
|
||||
import feign.RequestTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@Component
|
||||
public class TokenRequestInterceptor implements RequestInterceptor {
|
||||
/**
|
||||
* 请求拦截器
|
||||
*
|
||||
* @param requestTemplate 请求拦截器
|
||||
*/
|
||||
@Override
|
||||
public void apply(RequestTemplate requestTemplate) {
|
||||
System.out.println("===============请求拦截器===============");
|
||||
requestTemplate.header("X-Token", UUID.randomUUID().toString());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package cn.bunny.properties;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "order")
|
||||
@Data
|
||||
public class OrderProperties {
|
||||
String timeout;
|
||||
|
||||
String confirm;
|
||||
|
||||
String dbUrl;
|
||||
}
|
|
@ -6,6 +6,9 @@ spring:
|
|||
profiles:
|
||||
active: dev
|
||||
cloud:
|
||||
nacos:
|
||||
config:
|
||||
namespace: ${spring.profiles.active:public} # 后面是默认 namespace
|
||||
openfeign:
|
||||
client:
|
||||
config:
|
||||
|
@ -25,5 +28,3 @@ spring:
|
|||
# 压缩响应请求
|
||||
response:
|
||||
enabled: true
|
||||
# main:
|
||||
# allow-bean-definition-overriding: true
|
Loading…
Reference in New Issue