Spring?Boot?根據(jù)配置決定服務(wù)(集群、單機(jī))是否使用某些主件的操作代碼
比如:在集群模式下,我想用 Nacos 組件,單機(jī)版不想用它。
server:
name: VipSoft Server Dev
port: 8193
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 #注冊(cè)中心地址(集群用,號(hào)分隔)
cluster-name: DEFAULT #可以通過集群名稱區(qū)分不同的項(xiàng)目
server-name: netty-service
group-name: NETTY_GROUP@Component
public class NettyServer {
private static final Logger logger = LoggerFactory.getLogger(LoggerConfig.NETTY_LOGGER);
@Value("${server.cloud.nacos.discovery.server-addr}")
private String nacosServer;
@Value("${server.cloud.nacos.discovery.server-name}")
private String serviceName;
@Value("${server.cloud.nacos.discovery.group-name}")
private String groupName;
}
@Component
public class XXXService {
@Autowired
private NacosUtil nacosUtil;
}解決文案
方案1:使用條件注解 + 配置開關(guān)
- 修改 application.yml 添加啟用開關(guān):
server:
name: Telemetry Cloud Server Dev
port: 8193
cloud:
nacos:
enabled: false # 添加這個(gè)開關(guān)
discovery:
server-addr: 127.0.0.1:8848
cluster-name: DEFAULT
server-name: netty-service
group-name: NETTY_GROUP- 修改 NettyServer 類:
@Component
@ConditionalOnProperty(name = "server.cloud.nacos.enabled", havingValue = "true")
public class NettyServer {
// 原有代碼...
}
@Component
public class XXXService {
// 允許依賴不存在
@Autowired(required = false)
private NacosUtil nacosUtil;
}方案2:使用 Profile 區(qū)分
- 創(chuàng)建不同環(huán)境的配置文件:
- application.yml (公共配置)
- application-nacos.yml (Nacos相關(guān)配置)
- application-standalone.yml (單機(jī)版配置)
- application.yml 中激活不同配置:
spring:
profiles:
active: standalone # 或 nacos- 將 Nacos 相關(guān)配置移到 application-nacos.yml 中
方案3:編程式條件加載(更靈活)
- 添加配置開關(guān):
netty: mode: standalone # 或 cloud
- 創(chuàng)建配置類:
@Configuration
public class NettyConfig {
@Bean
@ConditionalOnProperty(name = "netty.mode", havingValue = "cloud")
public NettyServer nettyServer() {
return new NettyServer();
}
}方案4:使用 @ConfigurationProperties 更優(yōu)雅地管理配置
- 創(chuàng)建配置類:
@ConfigurationProperties(prefix = "server.cloud.nacos.discovery")
public class NacosProperties {
private boolean enabled;
private String serverAddr;
private String clusterName;
private String serverName;
private String groupName;
// getters and setters
}- 修改 NettyServer:
@Component
public class NettyServer {
private final NacosProperties nacosProperties;
public NettyServer(NacosProperties nacosProperties) {
this.nacosProperties = nacosProperties;
if(nacosProperties.isEnabled()) {
// 初始化Nacos相關(guān)邏輯
}
}
}最佳實(shí)踐建議:
推薦方案1或方案4:
- 如果只是簡(jiǎn)單開關(guān),用方案1最簡(jiǎn)單:
server:
cloud:
nacos:
enabled: false@Component
@ConditionalOnProperty(name = "server.cloud.nacos.enabled", matchIfMissing = false)
public class NettyServer {
// ...
}- 如果需要更復(fù)雜的配置管理,用方案4更優(yōu)雅。
這樣你可以通過修改配置文件的 enabled 值來決定是否啟用Nacos相關(guān)功能,無需修改代碼。
到此這篇關(guān)于Spring Boot 根據(jù)配置決定服務(wù)(集群、單機(jī))是否使用某些主件的文章就介紹到這了,更多相關(guān)Spring Boot 根據(jù)配置決定服務(wù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
IDEA2023常用配置指南(JDK/系統(tǒng)設(shè)置等常用配置)
idea很強(qiáng)大,但是初次安裝默認(rèn)的有很多設(shè)置并不是滿足我們開發(fā)的需要,下面這篇文章主要給大家介紹了關(guān)于IDEA2023常用配置(JDK/系統(tǒng)設(shè)置等常用配置)的相關(guān)資料,需要的朋友可以參考下2023-12-12
IDEA創(chuàng)建parent項(xiàng)目(聚合項(xiàng)目)
這篇文章主要介紹了IDEA創(chuàng)建parent項(xiàng)目(聚合項(xiàng)目),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
java?安全?ysoserial?CommonsCollections6?分析
這篇文章主要介紹了java?安全?ysoserial?CommonsCollections6示例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
Java微信公眾平臺(tái)開發(fā)(1) 接入微信公眾平臺(tái)
這篇文章主要為大家詳細(xì)介紹了Java微信公眾平臺(tái)開發(fā)第一步,接入微信公眾平臺(tái),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
Java for循環(huán)常見優(yōu)化方法案例詳解

