当前位置:网站首页>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.com

And 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.properties

The contents are as follows :

application-config.properties

TEST_KEY=It's a test value

application.properties

spring.profile.active = config
test.key=${TEST_KEY:default_value}

If in apollo The configuration item in is

TEST_KEY=new value

Then 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=true

3) 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

原网站

版权声明
本文为[Fenglibin]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221835236069.html