1. Build the Spring Cloud Config Configuration center (see previous blog post)
2. Create a micro-service project Bounter-simon-app,pom file as follows:
<?XML version= "1.0" encoding= "UTF-8"?><Projectxmlns= "http://maven.apache.org/POM/4.0.0"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <groupId>Cn.bounter</groupId> <Artifactid>Bounter-simon-app</Artifactid> <version>0.0.1-snapshot</version> <Packaging>Jar</Packaging> <name>Bounter-simon-app</name> <Parent> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-starter-parent</Artifactid> <version>2.0.4.RELEASE</version> </Parent> <Properties> <java.version>1.8</java.version> <spring-cloud.version>Finchley.sr1</spring-cloud.version> </Properties> <Dependencies> <Dependency> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-starter-web</Artifactid> </Dependency>
<Dependency> <groupId>Org.springframework.cloud</groupId> <Artifactid>Spring-cloud-starter-config</Artifactid> </Dependency> </Dependencies> <dependencymanagement> <Dependencies> <Dependency> <groupId>Org.springframework.cloud</groupId> <Artifactid>Spring-cloud-dependencies</Artifactid> <version>${spring-cloud.version}</version> <type>Pom</type> <Scope>Import</Scope> </Dependency> </Dependencies> </dependencymanagement> <Build> <Plugins> <plugin> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-maven-plugin</Artifactid> </plugin> </Plugins> </Build></Project>
3. Configure the configuration information from the configuration center to obtain the application.properties file as follows:
server.port=8081# Configuration Center Warehouse profile Name spring.application.name=simon# Configuration Center Server address spring.cloud.config.uri=http://localhost:8888
4. The controller returns the configuration information obtained from the configuration center, the Simoncontroller.java content is as follows:
@RestController @crossorigin@requestmapping ("/api/simon") Public class Simoncontroller { @Value ("${name}") private String name; @GetMapping ("/name") public responsedata<?> get () { Returnnew responsedata<>(). Success (). data (name);} }
5. Start the Bounter-simon-app service, browser access: Http://localhost:8081/api/simon/name, the page is as follows:
True"Simon"null1534917807null}
Data shows the value of the name attribute obtained from the configuration center, it is not easy ah, then try it yourself!
Source Code GitHub Address:
Https://github.com/13babybear/bounter-simon-app
Https://github.com/13babybear/bounter-susan-app
Spring Cloud Micro Service Integration Configuration Center