Learn a little springcloud every day (eight): Use Apollo to do configuration center

Source: Internet
Author: User

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
    1. 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).
    2. Built locally after downloading the source code from Https://github.com/ctripcorp/apollo. The build steps are:
    1. dependencies required to download the project
    2. Build with BUILD.bat or build.sh under the Scripts folder
    3. 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
    1. Download the Apolloconfigdb.sql and Apolloportaldb.sql database files from Https://github.com/ctripcorp/apollo/tree/master/scripts/sql.
    2. Use the Apolloportaldb.sql file to create the APOLLOPORTALDB database, which is a common database for managing various environments.
    3. 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
    1. Unzip the three compressed files downloaded in the first step
    2. Apollo-portal-1.0.0-github
    1. Configure the connection information for the APOLLOPORTALDB database in the Application-github.properties file under Apollo-portal-1.0.0-github/config.
    2. 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
    3. Copies a copy of the Apollo-adminservice-1.0.0-github file, renamed to Apollo-adminservice-dev and Apollo-adminservice-fat, respectively.
    4. 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.
    5. 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
    1. 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
    1. 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
    1. Now we have access to http://localhost:8080/and http://10.10.10.10:8080/can see the following information is no problem
    2. 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
    1. 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>
    1. 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环境
    1. 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;
}
}
    1. 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>
    1. Modify the Apollo in the configuration file. Meta for localhost:8080 start again
    2. 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.
    3. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.