当前位置:网站首页>Precautions for Apollo use
Precautions for Apollo use
2022-06-22 20:21:00 【Fenglibin】
1. Introduce client dependency
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>1.5.1</version>
</dependency>notes :
If there are multiple internal applications to be used at the same time Apollo, Can be Apollo Encapsulated in a single Jar in , For these applications . And the environment can be (DEV、UAT、TEST、PRD)Meta Server Written under the name apollo-env.properties In profile , As shown below :
dev.meta=http://1.1.1.1:8080
fat.meta=http://apollo.fat.xxx.com
uat.meta=http://apollo.uat.xxx.com
pro.meta=http://apollo.xxx.comAnd put it in classpath Next , these Meta Server Is the default value for each environment , You can also follow up by java Start parameter of -Dapollo.meta=metaServerHost, perhaps spring boot Configuration file for application.properties By designation apollo.meta=metaServerHost Methods such as , Make changes .
And then in java The startup parameters only need to pass -Denv=DEV Equal parameter ,apollo Will choose a specific environment Meta Server.
See :apollo meta server Configuration of
2、 The parameter hot update configuration that introduces other variables in the configuration
Apollo It has the function of hot updating parameters , however Apollo Middle configuration Key Must be application.properties or application.yml Medium key, Instead of introducing... In other configuration files key, Otherwise, the hot update will not work , Here are some examples .
The project includes two configuration files :
application-config.properties
application.propertiesThe contents are as follows :
application-config.properties
TEST_KEY=It's a test valueapplication.properties
spring.profile.active = config
test.key=${TEST_KEY:default_value}If in apollo The configuration item in is
TEST_KEY=new valueThen modify the configuration item TEST_KEY, Although the application can receive the notification of configuration item modification , But introduce test.key The value of the attribute in the object does not change , Only restart the application will take effect .
In order for the configuration to take effect immediately ,Apollo The configuration item in should be application.properties Medium test.key, Modify configuration item test.key Value , Then introduce test.key The value of the attribute in the object will take effect immediately .
3、Apollo About @ConfigurationProperties Dynamic refresh of
Comments are used for @ConfigurationProperties Configuration injection mode , The following example class :
@ConfigurationProperties(prefix = "redis.cache")
public class SampleRedisConfig {
private int expireSeconds;
private int commandTimeout;
public void setExpireSeconds(int expireSeconds) {
this.expireSeconds = expireSeconds;
}
public void setCommandTimeout(int commandTimeout) {
this.commandTimeout = commandTimeout;
}
}In view of this Bean, Need to use spring cloud Of EnvironmentChangeEvent and RefreshScope, The implementation is as follows :
1) rely on spring cloud context, Such as :
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
<version>2.0.4.RELEASE</version><!-- The version depends on the actual scenario -->
</dependency>2) stay spring boot Add the following configuration items to the configuration file of :
spring.boot.enableautoconfiguration=true3) Realization ApplicationContextAware
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
import org.springframework.cloud.context.scope.refresh.RefreshScope;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**
* apollo Automatically refresh
*
* @author qubianzhong
* @Date 20:32 2019/11/11
*/
@Component
@Slf4j
public class ApolloRefreshConfig implements ApplicationContextAware {
ApplicationContext applicationContext;
@Autowired
RefreshScope refreshScope;
// Designated here Apollo Of namespace, It's very important , If you don't specify , By default, only application
@ApolloConfigChangeListener(value = {ConfigConsts.NAMESPACE_APPLICATION,"business","everything"})
public void onChange(ConfigChangeEvent changeEvent) {
for (String changedKey : changeEvent.changedKeys()) {
log.info("apollo changed namespace:{} Key:{} value:{}", changeEvent.getNamespace(), changedKey, changeEvent.getChange(changedKey));
}
refreshProperties(changeEvent);
}
public void refreshProperties(ConfigChangeEvent changeEvent) {
this.applicationContext.publishEvent(new EnvironmentChangeEvent(changeEvent.changedKeys()));
refreshScope.refreshAll();
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
4、 The project startup script on the server boot.sh Add startup parameters
java \
-Dapollo.autoUpdateInjectedSpringProperties=true \
-Dapp.id=apollo-demo \
-Denv=DEV \
-Dapollo.meta=http://192.168.10.235:8071 \
-Dapollo.bootstrap.enabled=true \
-Dapollo.bootstrap.eagerLoad.enabled=true \
-Dapollo.bootstrap.namespaces=application \
-Dapollo.cacheDir=/opt/data/some-cache-dir \
-jar apollo-demo.jar apollo.autoUpdateInjectedSpringProperties: Whether to update the configuration automatically , If you do not want to update automatically, set this value to false, The default value is true
app.id: Project AppId
env: Environment settings , Test environment DEV, Pre release environment UAT, Production environment PRO, There are three ways ( Launch parameters 、 Operating system environment variables and configuration files ) Appoint , See documentation
apollo.meta:apollo Service address , For example, the test environment is http://192.168.10.235:8071
apollo.bootstrap.enabled: Whether the configuration is injected in the startup phase
apollo.bootstrap.eagerLoad.enabled: Whether or not to apollo Before the loading of the log system , So you can get through apollo To manage log related configurations , Such as log level settings , If true, At this stage apollo There will be no log output
apollo.bootstrap.namespaces: Configuration item , This is the same as the Apollo configuration properties The documents correspond to each other , For example, Apollo configured application.properties、db.properties and dubbo.properties Three configuration files , Here we set the value to application,db,dubbo
apollo.cacheDir: Custom cache path
Script namespaces The default value is application, So the default reading is Apollo application.properties The configuration file .
If there are too many parameters , Or other needs , If you need to add multiple configuration files , It is necessary to contact the operation and maintenance students to cooperate in modifying the project jenkins To configure , modify namespaces Parameters . Under normal circumstances, it is recommended to use only one application.properties The configuration file
Details see Apollo Java Official documents used :Apollo Java Client user guide
边栏推荐
- 【深入理解TcaplusDB技术】TcaplusDB 表管理——新建表
- Simple integration of client go gin 11 delete
- [deeply understand tcapulusdb knowledge base] common problems in deploying tcapulusdb local
- Mysql database knowledge points (III)
- Peking University - robust task representation for off-line meta reinforcement learning through contrastive learning
- 【深入理解TcaplusDB技术】TcaplusDB运维
- [in depth understanding of tcapulusdb technology] tcapulusdb model
- 软件上线前为什么要做性能测试?软件性能测试机构怎么找
- [in depth understanding of tcapulusdb technology] business guide for creating doc acceptance
- Using span method to realize row merging of multi-layer table data
猜你喜欢
2019 年总结:31岁,不过是另一个开始

【深入理解TcaplusDB技术】入门MySQL Driver

芯和半导体“射频EDA/滤波器设计平台”闪耀IMS2022

经典面试题:一个页面从输入url到呈现过程
![[deeply understand tcapulusdb knowledge base] common problems in deploying tcapulusdb local](/img/2b/3ab5e247ac103728b4d3579c3c5468.png)
[deeply understand tcapulusdb knowledge base] common problems in deploying tcapulusdb local

【深入理解TcaplusDB技术】入门TcaplusDB 问题汇总

Classic interview question: a page from entering URL to rendering process

元宇宙中的云计算,提升你的数字体验
![[deeply understand tcapulusdb technology] view the online operation of tcapulusdb](/img/6f/2d62030e631e3085acf72951f2416f.png)
[deeply understand tcapulusdb technology] view the online operation of tcapulusdb

Matplotlib set axis scale interval
随机推荐
科技云报道:东数西算不止于“算”,更需“新存储”
Traversal of trees and forests
mysql filesort要小心
数字经济加速落地,能为中小企业带来什么?
Redis持久化的几种方式——深入解析RDB
Shell Sort
[deeply understand tcapulusdb technology] tcapulusdb table management - modify table
[deeply understand tcapulusdb technology] tcapulusdb model management
Pit of undefined reference
An error is reported when idea writes JSP code, but it is solved by normal operation
Definitions and terms of drawings
【深入理解TcaplusDB技术】入门Tcaplus SQL Driver
【Proteus仿真】74LS138译码器流水灯
[in depth understanding of tcapulusdb technology] new models of tcapulusdb
【Proteus仿真】8x8Led点阵数字循环显示
critical path
程序员应该怎么查日期
树莓派环境设置
【深入理解TcaplusDB技术】TcaplusDB 表管理——清理表
What can the accelerated implementation of digital economy bring to SMEs?