这里以service项目调用xxl-job为例进行说明
一、nacos配置
server:
port: 32031
spring:
application:
name: t1
profiles:
active: local
cloud:
nacos:
config:
prefix: t1
group: test
namespace: YKF
file-extension: properties
server-addr: nacos1.test:8848,nacos2.test:8848,nacos3.test:8848
loadbalancer:
ribbon:
enabled: false
二、XxlJobConfig
@Slf4j
@ComponentScan(basePackages = "*.xxljob")
@Configuration
@RefreshScope //动态刷新
public class XxlJobConfig {
@Value("${xxl.job.admin.addresses}")
private String adminAddresses;
@Value("${xxl.job.executor.appname}")
private String appName;
@Value("${xxl.job.executor.ip}")
private String ip;
@Value("${xxl.job.executor.port}")
private int port;
@Value("${xxl.job.accessToken}")
private String accessToken;
@Value("${xxl.job.executor.logpath}")
private String logPath;
@Value("${xxl.job.executor.logretentiondays}")
private int logRetentionDays;
@Bean
public XxlJobSpringExecutor xxlJobExecutor() {
log.info(">>>>>>>>>>> xxl-job config init.");
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
xxlJobSpringExecutor.setAppName(appName);
xxlJobSpringExecutor.setIp(ip);
xxlJobSpringExecutor.setPort(port);
xxlJobSpringExecutor.setAccessToken(accessToken);
xxlJobSpringExecutor.setLogPath(logPath);
xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
return xxlJobSpringExecutor;
}
}
1.@Value给静态变量赋值,不能直接写在变量上,应该放在变量的set()方法上,且该方法不能被static修饰。其中需要在类上加入@Component注解。
2.通过@Value(“${xxxx}”)
可以获取属性文件中对应的值,但是如果属性文件中没有这个属性,则会报错。可以通过赋予默认值解决这个问题,如@Value("${xxxx:yyyy}")
比如
@Value("${mth.common.thread.corePoolSize:16}")
private int corePoolSize;
@Value("${mth.common.thread.maxPoolSize:100}")
private int maxPoolSize;
3.@RefreshScope注解能帮助我们做局部的参数刷新,但侵入性较强,需要开发阶段提前预知可能的刷新点,并且该注解底层是依赖于cglib进行代理的,所以不要掉入cglib的坑,出现刷了也不更新情况;
参考文章
https://blog.csdn.net/chuixue24/article/details/107386045
https://www.cnblogs.com/weihuang6620/p/13723219.html
https://www.jianshu.com/p/dbf3933aecb3