Apollo is a distributed configuration center developed by the Framework department of Ctrip. It can centrally manage configurations in different environments and clusters, and push configurations to applications in real time after modification. It has standardized permissions and process governance features, and is suitable for micro-service configuration management scenarios. The server is developed based on Spring Boot and Spring Cloud, and can run directly after packaging, without additional installation of Application containers such as Tomcat. The Java client does not depend on any framework, can run in all Java runtime environments, and has good support for the Spring/Spring Boot environment.

V Architecture Overview

The image above briefly describes Apollo’s overall design:

  • The Config Service provides functions such as reading and pushing configurations to the Apollo client
  • Admin Service provides configuration modification, publishing, and other functions. The Service object is Apollo Portal.
  • The Config Service and Admin Service are both multi-instance, stateless deployments, so you need to register yourself with Eureka and maintain a heartbeat
  • On top of Eureka we build a Meta Server to encapsulate Eureka’s service discovery interface
  • The Client accesses the Meta Server using the domain name to obtain the Config Service list (IP+Port). The Client accesses the Meta Server using the IP+Port and performs load balance and error retry on the Client
  • The Portal accesses the Meta Server using the domain name to obtain the Admin Service Service list (IP+Port), accesses the Meta Server using the IP+Port, and performs load balance and error retry on the Portal
  • To simplify deployment, we will actually deploy the Config Service, Eureka, and Meta Server logical roles in the same JVM process

VDocker deployment of Apollo,

1. Ready to MySQL

git clone https://github.com/ctripcorp/apollo.git

Copy the cloned SQL into the docker instance (docker instance path /opt/ Apollo /script need to create).

docker cp /data/app/apollo/source-code/apollo/scripts/sql mysql3308:/opt/apollo/scripts

Enter the docker instance and execute the SQL script.

docker exec -it mysql3308 bash
Copy the code

2. Mirror pull

docker pull apolloconfig/apollo-configservice
docker pull apolloconfig/apollo-adminservice
docker pull apolloconfig/apollo-portal
Copy the code

3. apollo-configservice

docker run -d \ --name apollo-configservice \ --net=host \ -v /tmp/logs:/opt/logs \ -e SPRING_DATASOURCE_URL="jdbc:mysql://ip.ip.ip.ip:3308/ApolloConfigDB? characterEncoding=utf8" \ -e SPRING_DATASOURCE_USERNAME=root \ -e SPRING_DATASOURCE_PASSWORD=password \ apolloconfig/apollo-configserviceCopy the code

Note that the default apollo-configService port is 8080, which may cause a port conflict. If you need to change the port of Apollo-configService, remember to change the address of Eureka as well. In ApolloConfigDB library ServerConfig table value, default is 8080. Otherwise, other services cannot be registered.

In addition, if the Apollo and mysql is a virtual machine docker, mysql JDBC connection string: mysql: / / IP. The IP. The IP. The IP: 3308 / ApolloConfigDB? CharacterEncoding = ip.ip.ip in UTf8 do not use 127.0.0.1. Do not use 127.0.0.1 anywhere else in docker deployment Apollo where IP is used.

SPRING_DATASOURCE_URL: address of ApolloConfigDB

SPRING_DATASOURCE_USERNAME: user name of ApolloConfigDB

SPRING_DATASOURCE_PASSWORD: specifies the password of ApolloConfigDB

4. apollo-adminservice

docker run -d \ --name apollo-adminservice \ --net=host \ -v /tmp/logs:/opt/logs \ -e SPRING_DATASOURCE_URL="jdbc:mysql://ip.ip.ip.ip:3308/ApolloConfigDB? characterEncoding=utf8" \ -e SPRING_DATASOURCE_USERNAME=root \ -e SPRING_DATASOURCE_PASSWORD=password \ apolloconfig/apollo-adminserviceCopy the code

SPRING_DATASOURCE_URL: address of ApolloConfigDB

SPRING_DATASOURCE_USERNAME: user name of ApolloConfigDB

SPRING_DATASOURCE_PASSWORD: specifies the password of ApolloConfigDB

5. apollo-portal

docker run -d \ --name apollo-portal \ --net=host \ -v /tmp/logs:/opt/logs \ -e SPRING_DATASOURCE_URL="jdbc:mysql://ip.ip.ip.ip:3308/ApolloPortalDB? characterEncoding=utf8" \ -e SPRING_DATASOURCE_USERNAME=root \ -e SPRING_DATASOURCE_PASSWORD=password \ -e APOLLO_PORTAL_ENVS=dev \ -e DEV_META=http://ip.ip.ip.ip:8080 \ apolloconfig/apollo-portalCopy the code

SPRING_DATASOURCE_URL: address of ApolloPortalDB

SPRING_DATASOURCE_USERNAME: user name of ApolloPortalDB

SPRING_DATASOURCE_PASSWORD: password of ApolloPortalDB

APOLLO_PORTAL_ENVS(Optional): Corresponds to the apollo.portal.envs configuration item in ApolloPortalDB, which can be configured using this environment parameter if it is not configured in the database

DEV_META/PRO_META (optional) : Meta Service address configuration corresponding to the environment, in order to ${ENV} _META naming, it is important to note if the configuration of ApolloPortalDB Apollo. Portal. Meta. The configuration of the servers, With Apollo. Portal. Meta. The configuration of the servers

6. Verify the Apollo

You can log in to http://toutou.com:8070 using the system account/password Apollo /admin.

7. Create projects

Click on the home page to create the project, create the submission, and then return to the home page to see the project created.

8. Add the configuration

9. Publish the configuration

After selecting the corresponding environment, click Publish.

Apollo vspringboot integration

1. Add a reference

<dependency> <groupId>com.ctrip.framework.apollo</groupId> <artifactId>apollo-client</artifactId> The < version > 1.7.0 < / version > < / dependency >Copy the code

2. Add the configuration

Application. The properties:

#AppId is the identity information of the application. It is an important information for the configuration center to obtain the configuration. App. id=abc10086def # Inject the configuration information of the managed application.properties file into the Spring container during application startup. Apollo. Bootstrap. enabled = true # Enable your Eureka registry address Apollo. Meta =http://toutou.com:8080 Starting with version 1.2.0, if you want to put logging related configurations (such as parameters in logging.level.root=info or logback-spring.xml) under Apollo management as well, You can additional configuration Apollo. The bootstrap. EagerLoad. Enabled = true to Apollo load order before loading on the logging system. apollo.bootstrap.eagerLoad.enabled=trueCopy the code

3. Interface code

package learn.web.controller; import com.ctrip.framework.apollo.Config; import com.ctrip.framework.apollo.spring.annotation.ApolloConfig; import learn.model.vo.Result; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.beans.factory.annotation.Value; import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig; /** * @author toutou * @date by 2021/2 * @des https://www.cnblogs.com/toutou */ @Slf4j @RestController @EnableApolloConfig public class IndexController { @Value("${demo.default.message}") private String helloMessage; @ApolloConfig private Config config; @GetMapping("apollo/message") public Result apolloMessage() { return Result.setSuccessResult(config.getProperty("demo.default.message", "null")); } @GetMapping("apollo/message2") public Result apolloMessage2() { return Result.setSuccessResult(helloMessage); }}Copy the code

4. Verify the effect

5. Multiple ways to get Apollo configurations

1. Use the @value annotation to obtain the configuration value. For example, @value ("${demo.default.message}") private String message; 2. Inject @configurationProperties into the bean object to get configuration information for example:  @ConfigurationProperties(prefix="test") Public class Myprop{ private Map<String,String> prop = Maps.newLinkedHashMap();  } used in spring-managed classes: @autoWired private Myprop Myprop; prop.getProp().get("name"); Note: Property values obtained in this way are not updated in real time. 3. Use @apolloConfig to get the injected Config object, and then use the config object to get the attribute values. For example, in a spring-managed class: @apolloConfig private config config; config.getProperty("demo.default.message",null); 4. Through Java API access to Config the Config = ConfigService. GetAppConfig (); String someKey = "someKeyFromDefaultNamespace"; String someDefaultValue = "someDefaultValueForTheKey"; String value = config.getProperty(someKey, someDefaultValue); 5. Obtain the attribute value in the configuration file. For example: In the application. The properties of the spring. The datasource. Name = ${JDBC. Name} spring. The datasource. Password = ${JDBC. "}Copy the code

Other reference/learning materials:

  • Github.com/ctripcorp/a…
  • Spring Boot integrates with the Apollo configuration center

V Source code address

Github.com/toutouge/ja…

About the author: Focus on basic platform project development. If you have any questions or suggestions, please feel free to comment! Copyright notice: The copyright of this article belongs to the author and the blog garden, welcome to reprint, but without the consent of the author must retain this statement, and give the original text link in a prominent place on the page of the article. For the record: all comments and messages will be answered as soon as possible. You are welcome to correct your mistakes and make progress together. Or direct private message I support the blogger: if you think the article is helpful to you, you can click on the lower right corner of the article [recommendation]. Your encouragement is the author to adhere to the original and continuous writing of the biggest power! \