spring cloud Apollo apollo local development environment setup process

  • 2021-01-02 21:51:59
  • OfStack

Open source configuration center - Apollo

Apollo (Apollo) is a configuration management platform developed by ctrip framework Department. It can centralize the configuration of different application environments and clusters. After configuration modification, Apollo can be pushed to the application end in real time, and it has standardized permissions, process governance and other characteristics. The server is developed based on Spring Boot and Spring Cloud. It can be packaged and run directly without additional installation of application containers such as Tomcat.

Check out the code

apollo github

fork and then open it locally using idea

Database script

Execute the following script to create ApolloConifgDB, ApolloPortalDB

apollo.scripts.sql.apolloconfigdb.sql apollo.scripts.sql.apolloportaldb.sql

Start the configservice adminservice

Main class configuration


com.ctrip.framework.apollo.assembly.ApolloApplication

VM opions


-Dapollo_profile=github 
-Dspring.datasource.url=jdbc:mysql://localhost:3306/ApolloConfigDB?characterEncoding=utf8 
-Dspring.datasource.username=root 
-Dspring.datasource.password=
Program arguments
--configservice --adminservice

After startup, open http://localhost:8080 to see that both ES44en-ES45en and ES46en-ES47en have been started and registered to Eureka

Start the Apollo - Portal

Main class configuration


com.ctrip.framework.apollo.portal.PortalApplication
-Dapollo_profile=github,auth 
-Ddev_meta=http://localhost:8080/ 
-Dserver.port=8070 
-Dspring.datasource.url=jdbc:mysql://localhost:3306/ApolloPortalDB?characterEncoding=utf8 
-Dspring.datasource.username=root 
-Dspring.datasource.password=

If auth profile is enabled, the default user name is apollo and the password is admin

Application in SIT, UAT, production environment machines

1. Add directory /opt/data/ directory with read-write permission;

2. The new file: / / opt settings/server properties and join the configuration:


env=DEV
sit: env=FAT
uat: env=UAT
 Production: env=PRO

Client-side example


@Component  Set the component name 
@RefreshScope  Specifies that configuration changes can be refreshed 
@ConfigurationProperties(prefix = "redis.cache")
@Component("sampleRedisConfig")
@RefreshScope
public class SampleRedisConfig {
 private static final Logger logger = LoggerFactory.getLogger(SampleRedisConfig.class);
 private int expireSeconds;
 private String clusterNodes;
 private int commandTimeout;
 private Map<String, String> someMap = Maps.newLinkedHashMap();
 private List<String> someList = Lists.newLinkedList();
 @PostConstruct
 private void initialize() {
 logger.info(
  "SampleRedisConfig initialized - expireSeconds: {}, clusterNodes: {}, commandTimeout: {}, someMap: {}, someList: {}",
  expireSeconds, clusterNodes, commandTimeout, someMap, someList);
 }
 public void setExpireSeconds(int expireSeconds) {
 this.expireSeconds = expireSeconds;
 }
 public void setClusterNodes(String clusterNodes) {
 this.clusterNodes = clusterNodes;
 }
 public void setCommandTimeout(int commandTimeout) {
 this.commandTimeout = commandTimeout;
 }
 public Map<String, String> getSomeMap() {
 return someMap;
 }
 public List<String> getSomeList() {
 return someList;
 }
 @Override
 public String toString() {
 return String.format(
  "[SampleRedisConfig] expireSeconds: %d, clusterNodes: %s, commandTimeout: %d, someMap: %s, someList: %s",
   expireSeconds, clusterNodes, commandTimeout, someMap, someList);
 }
}

Set up to monitor


@Component
public class SpringBootApolloRefreshConfig {
 private static final Logger logger = LoggerFactory.getLogger(SpringBootApolloRefreshConfig.class);
 @Autowired
 private ApolloRefreshConfig apolloRefreshConfig;
 @Autowired
 private SampleRedisConfig sampleRedisConfig;
 @Autowired
 private RefreshScope refreshScope;
 @ApolloConfigChangeListener
 public void onChange(ConfigChangeEvent changeEvent) {
 logger.info("before refresh {}", sampleRedisConfig.toString());
 refreshScope.refresh("sampleRedisConfig");
 logger.info("after refresh {}", sampleRedisConfig.toString());
 }
}

conclusion


Related articles: