JAVA development (several ways to inject configuration information through Apollo)

foreword

An important component in springCloud is the configuration center, config:server, which is used to configure various configuration items that need to be injected in springboot. But now it is found that more and more enterprises use Apollo for integration. Bloggers also use Apollo for configuration during development. This article summarizes the use of Apollo, integration into springboot, and injection methods.

 

1. Introduction to Apollo

Apollo is an open source configuration management center developed by Ctrip's framework department. It can centrally manage the configuration of applications in different environments and different clusters. After the configuration is modified, it can be pushed to the application side in real time, and it has functions such as authority management and process governance.

(1) Apollo supports four dimensions to manage the configuration of the key-value format

  • application (application level, for the current application)
  • environment (environmental level, different configurations can be used in different environments. For example, dev and prod environments)
  • cluster (cluster. It can be divided into different configurations according to the cluster)
  • namespace (namespace. Different configuration files, such as db configuration, rpc configuration, etc.) 

(2) Features of Apollo

  • The configuration of different environments and different clusters can be managed in a unified manner.
    (1) The same code is deployed in different clusters and can have different configurations.
    (2) Different applications can share the same configuration (public configuration) by configuring the namespace, or they can override the public configuration according to their own needs by associating with the public namespace. (Equivalent to the subclass can inherit the parent class, for the members of the parent class, if it does not meet the needs, you can override it yourself.)
  • Hot release is supported. After the configuration is modified on the management interface, the client can receive it in real time. (According to its description, the time is 1s)
  • Every time the configuration is changed and released, there will be a corresponding version concept, which is convenient for subsequent rollback.
  • Grayscale release. (It refers to changing the configuration and clicking publish, which will only take effect for some instances. After observation, if there is no problem, it will be pushed to all instances.)
  • Permissions management (Different permissions can be assigned to different operations. And in the release of configuration changes, it is divided into two independent operations: editing and publishing.)
  • Supports Java and .Net clients.
  • Provide open platform API.
  • Deployment is simple and convenient (currently the only external dependency is MySQL, so it can run after installing JDK and MySQL.)

2. The structure and working principle of Apollo

(1) The core concept of Apollo

  • application: The application that actually uses the configuration.
  • environment (environment): The environment corresponding to the configuration. For example, environments such as dev and prd can have different configurations in different environments. The default is to read the configuration on the machine (env property in server.properties)
  • cluster: A grouping of different instances of an application. For example, the application instances in the Beijing computer room are divided into one cluster, and the application instances in the Yanjiao computer room are divided into another cluster. The same configuration can have different values ​​under different clusters. The default is to read on the machine (idc property in server.properties)
  • namespace (namespace): A group of different configurations under an application, which can be understood as a configuration file. Such as database configuration files, rpc configuration files, and application configuration files.

(2) Implementation principle of Apollo client

  • The client and server maintain a long connection through http long polling, so that they can get the push of configuration updates at the first time.
  • The client will regularly pull the latest configuration of the application from the Apollo configuration center server
  • After the client obtains the latest configuration, it will be saved in memory.
  • The client will cache a copy of the obtained configuration locally (in order to ensure that the service is restored from the local)
  • Applications can fetch the latest configuration through the client.
  • Note: If you want to learn more, you can study the source code, and the source code analysis link of Apollo is also given on github)

 3. Installation of Apollo (here is the introduction of installation through docker)

1. Preparation

The deployment of Apollo requires a Mysql database, version 5.6.5 or above.

2. Create a database

The Apollo server needs two databases: ApolloPortalDB and ApolloConfigDB. We have prepared sql files for the database, table creation and sample data, and only need to import the database.

Execute two sql files (downloaded through Ctrip Apollo community)

sql/apolloportaldb.sql

sql/apolloconfigdb.sql

will create two databases

 The sql file can be downloaded from the official website: Quick Start · apolloconfig/apollo Wiki · GitHub

 3. Create a shell script file on the server

Create the "apollo" folder under the /home/docker/script path

 Create the "apollo-portal.sh" script file under the /home/docker/script/apollo path, the content is:

docker run -d \
--name apollo-portal \
--net=host \
-v /tmp/logs:/opt/logs \
-e SPRING_DATASOURCE_URL=
"jdbc:mysql://数据库地址/apollo_portal?characterEncoding=utf8" \
-e SPRING_DATASOURCE_USERNAME=数据库账号\
-e SPRING_DATASOURCE_PASSWORD=数据库密码\
-e APOLLO_PORTAL_ENVS=test \
-e TEST_META=http://服务器ip地址:8080 \
apolloconfig/apollo-portal

Create the "apollo-config.sh" script file under the /home/docker/script/apollo path, the content is:

docker run -d \
--name apollo-configservice \
--net=host \
-v /tmp/logs:/opt/logs \
-e SPRING_DATASOURCE_URL=
"jdbc:mysql://数据库地址/apollo_config?characterEncoding=utf8" \
-e SPRING_DATASOURCE_USERNAME=数据库账号\
-e SPRING_DATASOURCE_PASSWORD=数据库密码\
apolloconfig/apollo-configservice

Create the "apollo-admin.sh" script file under the /home/docker/script/apollo path, the content is:

docker run -d \
--name apollo-adminservice \
--net=host \
-v /tmp/logs:/opt/logs \
-e SPRING_DATASOURCE_URL=
"jdbc:mysql://数据库地址/apollo_config?characterEncoding=utf8" \
-e SPRING_DATASOURCE_USERNAME=数据库账号\
-e SPRING_DATASOURCE_PASSWORD=数据库密码\
apolloconfig/apollo-adminservice

4. Pull the Apollo image through docker

docker pull apolloconfig/apollo-configservice:latest
docker pull apolloconfig/apollo-adminservice:latest
docker pull apolloconfig/apollo-portal:latest

5. Run Apollo

1. ./apollo-portal.sh
2. ./apollo-config.sh
3. ./apollo-admin.sh

6. Visit Apollo

 

The access server IP: 8070 can be accessed, the default user name is apollo, and the password is admin.

4. Apollo is integrated into springboot

1. Introduce maven

		<!-- apollo -->
		<dependency>
			<groupId>com.ctrip.framework.apollo</groupId>
			<artifactId>apollo-client</artifactId>
			<version>${apollo.version}</version>
		</dependency>

		<dependency>
			<groupId>com.ctrip.framework.apollo</groupId>
			<artifactId>apollo-core</artifactId>
			<version>${apollo.version}</version>
		</dependency>

2. Start class annotation @EnableApolloConfig

After the Apollo system configuration completes the configuration

Injection method one:

example:

	/**
	 * 微信主体appId
	 */
	@Value("${weixin.appid:-1}")
	private String appid;

Injection method two:

Using the annotation @ConfigurationProperties to use entities, I personally think this way is better.

example:

The format configured in Apollo is consistent with the format of yml

oss:
  tencent:
    domain:
    region: mhyr-api
    bucketName: mhyr-13015545476
    secretId:  
    secretKey: 
    styleRule:
    thumbnailStyleRule: "!Photo_Compression"
    fileTypes: ## 允许上传的文件类型
        - png
        - jpg
        - jpeg
        - gif
        - bmp
        - svg

The code is mapped through the entity class 

@ConfigurationProperties(prefix = "oss.tencent")
public class TencentProperties {
	
    /**域名*/
    private String domain;
    /**地域节点*/
    private String region;
    /**存储桶名称*/
    private String bucketName;
    /**secretId*/
    private String secretId;
    /**secretKey*/
    private String secretKey;
    /**图片策略*/
    private String styleRule;
    /**缩略图策略*/
    private String thumbnailStyleRule;
    /**文件类型*/
    private List<String> fileTypes;
}

Guess you like

Origin blog.csdn.net/dongjing991/article/details/131230338