As the graphical interface supported by Apollo was more friendly to us, we used Apollo to do the configuration center.
This article implements the configuration of attributes in both dev and fat environments using Apollo.
Apollo Official Document Https://github.com/ctripcorp/apollo/wiki
1. Download Dependencies
- Download the latest version of Apollo-configservice-x.x.x-github.zip from the Https://github.com/ctripcorp/apollo/releases page, Apollo-adminservice-x.x.x-github.zip and Apollo-portal-x.x.x-github.zip dependent packages (requires FQ. Students who cannot FQ recommend using the second method).
- Built locally after downloading the source code from Https://github.com/ctripcorp/apollo. The build steps are:
- dependencies required to download the project
- Build with BUILD.bat or build.sh under the Scripts folder
- Copy the Target/apollo-xxx-x.x.x-github.zip files from the Apollo-adminservice, Apollo-configservice, and apollo-portal three folders respectively
2. Create a database
- Download the Apolloconfigdb.sql and Apolloportaldb.sql database files from Https://github.com/ctripcorp/apollo/tree/master/scripts/sql.
- Use the Apolloportaldb.sql file to create the APOLLOPORTALDB database, which is a common database for managing various environments.
- Use the Apolloconfigdb.sql file to create the Apolloconfigdb_dev and APOLLOCONFIGDB_FAT databases separately as data stores for our two environments.
3. Configure Database connection Information
- Unzip the three compressed files downloaded in the first step
- Apollo-portal-1.0.0-github
- Configure the connection information for the APOLLOPORTALDB database in the Application-github.properties file under Apollo-portal-1.0.0-github/config.
- Open the Apollo-env.properties file to modify the Eureka address of the Dev.mate and Fat.mate property values for the different environments. For example here my fat environment is used locally, and Dev uses the server address
- Copies a copy of the Apollo-adminservice-1.0.0-github file, renamed to Apollo-adminservice-dev and Apollo-adminservice-fat, respectively.
- Configure the Apolloconfigdb in the application-github.properties file in the Config folder of Apollo-adminservice-dev and Apollo-adminservice-fat respectively Connection information for the _dev and Apolloconfigdb_fat databases.
- Follow 3.4 steps to copy the Apollo-configservice-1.0.0-github and configure the data connection address separately
Now the database connection information is as follows:
4. Start the service
- When using Apollo, the portal only needs to start one to manage it, and here we temporarily put it on the local boot. To start, use a small script
1 2 3 4
|
#!/bin/bash SH apollo-portal-1.0.0-github/scripts/startup.sh SH apollo-configservice-fat/scripts/startup.sh SH apollo-adminservice-fat/scripts/startup.sh
|
- Upload Apollo-configservice-dev and Apollo-adminservice-dev to the server and start with the following command
1 2
|
Sh./apollo-configservice-dev/scripts/startup. Sh./apollo-adminservice-dev/scripts/startup. SH
|
- Now we have access to http://localhost:8080/and http://10.10.10.10:8080/can see the following information is no problem
- Modify the data in the Serverconfig table in the database Apolloconfigdb_dev and Apolloconfigdb_fat to the value of the Eureka.service.url, respectively, by 10.10.10.10:8080/eureka/and http://localhost:8080/eureka/
5. Testing
- Create a MAVEN project that introduces the related dependencies of Apollo
1 2 3 4 5 6
|
<apollo.version>1.0.0</APOLLO.VERSION> < DEPENDENCY> < groupid>com.ctrip.framework.apollo</groupId> <artifactid>apollo-client< span class= "tag" ></ARTIFACTID> <version>${apollo.version} </VERSION> </DEPENDENCY> |
- Specify the ID of the app in Application.yml, and the address of the Apollo Configuration Center
1 2 3 4
|
APP: Id:demo Apollo http://10.10.10.10:8080 #指定dev环境
|
- Creating the Configrefresher Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
@Service PublicClassConfigrefresherImplementsApplicationcontextaware { Private ApplicationContext ApplicationContext;
@ApolloConfig Private config config;
@PostConstruct PrivatevoidInitialize() { Refresher (Config.getpropertynames ()); }
@ApolloConfigChangeListener PrivatevoidOnChange(Configchangeevent changeevent) { Refresher (Changeevent.changedkeys ()); }
PrivatevoidRefresher(set<string> Changedkeys) {
for (String Changedkey:changedkeys) { System.out.println ("This key is changed:" +changedkey); } this.applicationContext.publishEvent (new Environmentchangeevent (Changedkeys));
}
@Override Public void setapplicationcontext(ApplicationContext applicationcontext) throws beansexception { this.applicationcontext = ApplicationContext; } }
|
- Create a startup class and start
1 2 3 4 5 6 7 8 |
< span class= "variable" > @EnableApolloConfig public class Application { public static void main (string[] args) { springapplication.run (Application.class, args); } /span> |
- Modify the Apollo in the configuration file. Meta for localhost:8080 start again
- Open browser Access http://localhost:8070 Apollo The default user name is Apollo, password is admin. After landing click Create Project, the project's app ID and name fill in the App.id in our configuration file.
- Entry can be tested separately in the dev and fat environments by publishing different configurations
This article is from http://zhixiang.org.cn, reproduced please retain.
Learn a little springcloud every day (eight): Use Apollo to do configuration center