当前位置:网站首页>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
边栏推荐
- B tree code (C language)
- 【深入理解TcaplusDB技术】单据受理之创建业务指南
- critical path
- 【深入理解TcaplusDB技术】TcaplusDB进程
- 自己写了一个telnet命令
- 【深入理解TcaplusDB知识库】部署TcaplusDB Local版常见问题
- [deeply understand tcapulusdb technology] tcapulusdb model management
- Interpolation lookup and half (bisection) lookup
- Summer Challenge [FFH] Hongmeng machine learning journey from scratch NLP emotion analysis
- Web technology sharing | [Gaode map] to realize customized track playback
猜你喜欢

Comment le sac à dos complet considère - t - il la disposition?

一张图解码 OpenCloudOS 社区开放日

Using span method to realize row merging of multi-layer table data

#夏日挑战赛# 【FFH】从零开始的鸿蒙机器学习之旅-NLP情感分析

三维天地助力实验室夯实完整质量体系管理

Traversal of trees and forests

手把手教你IDEA创建SSM项目结构

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

IDEA写jsp代码报错,但是正常运行解决

【深入理解TcaplusDB技术】TcaplusDB 表管理——清理表
随机推荐
B tree code (C language)
Hash table (hash table)
【深入理解TcaplusDB技术】TcaplusDB 新增业务集群cluster
Heap sort (principle plus code)
What can the accelerated implementation of digital economy bring to SMEs?
【深入理解TcaplusDB技术】单据受理之创建业务指南
漫话Redis源码之一百一二十
I wrote a telnet command myself
IDEA写jsp代码报错,但是正常运行解决
数字经济加速落地,能为中小企业带来什么?
【深入理解TcaplusDB技术】TcaplusDB 表管理——新建表
金鱼哥RHCA回忆录:DO447管理用户和团队的访问--创建和管理Ansible Tower用户
支持在 Kubernetes 运行,添加多种连接器,SeaTunnel 2.1.2 版本正式发布!
【深入理解TcaplusDB技术】入门Tcaplus SQL Driver
【深入理解TcaplusDB技术】创建游戏区
【深入理解TcaplusDB技术】入门TcaplusDB 问题汇总
Dynamicdatabasesource, which supports the master-slave database on the application side
Possible security vulnerabilities in NFT
程序员应该怎么查日期
How to consider the arrangement of complete knapsack