4.3. Startup class Configserverapplication.java
PackageCom.jacky.cloud;Importorg.springframework.boot.SpringApplication;Importorg.springframework.boot.autoconfigure.SpringBootApplication;Importorg.springframework.cloud.client.discovery.EnableDiscoveryClient;ImportOrg.springframework.cloud.config.server.EnableConfigServer, @ Springbootapplication@enablediscoveryclient@enableconfigserver Public classconfigserverapplication { Public Static voidMain (string[] args) {Springapplication.run (configserverapplication.class, args); }}
V. Client (microservice-config-client)
5.1. Pom.xml configuration file
<?xml version= "1.0" encoding= "UTF-8"? ><project xmlns= "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" > <parent> <artifact Id>miroservice-config</artifactid> <groupId>com.jacky</groupId> <version>1.0-snap shot</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactid>microservi ce-config-client</artifactid> <packaging>jar</packaging> <properties> <project.bu ild.sourceencoding>utf-8</project.build.sourceencoding> </properties> <dependencies> &L T;dependency> <groupId>org.springframework.cloud</groupId> <artifactid>spring-c Loud-starter-config</artifactid> </dependency> <dependency> <groupid>org. Springframework.boot</groupid> <artifactId>spring-boot-starter-web</artifactId> </d Ependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactid>spring-c loud-starter-eureka</artifactid> </dependency> <dependency> <groupid>org .springframework.boot</groupid> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <GROUPID>COM.SPOTIFY</GROUPID&G T <artifactId>docker-maven-plugin</artifactId> <executions> <!--settings in the execution Maven build image with install-<execution> <id>build-image</id> <phase>install</phase> <goals> <goal& gt;build</goal> </goals> </execution> </exec Utions> <configuration> <!--installed a Docker host and opened the API remote interface settings- <dockerhost>http://192.168.6.130:5678</dockerhost><pushImage>true</pushImage><!--Setting up an upload image to a private warehouse requires Docker settings specify the private warehouse address--<!--mirror name--< Imagename>${docker.repostory}/${docker.image.prefix}/${project.artifactid}:${project.version}</imagename > <!--the base version of the image--<BASEIMAGE>JAVA:OPENJDK-8-JDK-ALPINE</BASEIMAGE&G T <!--mirroring boot Parameters--<entrypoint>["Java", "-jar", "/${project.build.finalname}.jar"]</entrypoin t> <resources> <resource> <TARGETPA Th>/</targetpath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> ; </resources> </configuration> </plugin> </plugins> </build> </project>
5.2. application.yml file
Server: 8081
View Code
5.3. bootstrap.yml file
spring:cloud:config:username:jacky123 #configservice认证的用户名 password:admin123 #认证密码 label:m Aster # The Branch node of the warehouse discovery:enabled:trueService-id:microservice-config-service Profile:dev #仓库中对应文件的环境 such as Dev, prod, test, etc. fail-fast:truebus:trace:enabled:true#开启消息跟踪 Application:name:microservice-config-Client Rabbitmq:host:192.168.6.130Port:5672username:myuser password:mypasseureka:client:serviceUrl:defaultZone:http://Jacky:[email Protected]:9511/eurekaInstance:prefer-ip-address:truemanagement:security:enabled:false#刷新时关闭安全认证
Attention:
The above properties must be configured on the BOOTSTRAP.YML, and the server configuration content will be loaded correctly. Because the load priority over the BOOTSTRAP.YML is higher than the service side of the configuration center, the server load takes precedence over application.yml, so if you write the above configuration in application.yml, it is equivalent to the default not to read the configuration information from the server on the configuration center, but rather sprin The default load for G boot. When you start, you will see the loaded configuration, not the configuration content, but the default local
5.4. Startup class (Configclientapplication.java)
PackageCom.jacky.cloud;Importorg.springframework.boot.SpringApplication;Importorg.springframework.boot.autoconfigure.SpringBootApplication;ImportOrg.springframework.cloud.client.discovery.EnableDiscoveryClient, @ Springbootapplication@enablediscoveryclient Public classconfigclientapplication { Public Static voidMain (string[] args) {Springapplication.run (configclientapplication.class, args); }}
5.5. Control layer Class (Configclientcontroller.java)
PackageCom.jacky.cloud.controller;ImportOrg.springframework.beans.factory.annotation.Value;ImportOrg.springframework.cloud.context.config.annotation.RefreshScope;Importorg.springframework.web.bind.annotation.GetMapping;ImportOrg.springframework.web.bind.annotation.RestController, @RefreshScope @restcontroller Public classConfigclientcontroller {@Value ("${profile}") PrivateString profile; @GetMapping ("/profile") PublicString GetProfile () {return This. Profile; }}
Vi. creating a Git project in the code cloud, placing a configuration file
Address: Https://gitee.com/jacky-lulu/microservice-config-repo
Seven, testing
7.1, http://localhost:9511/
Source source technical support for complete projects 2147775633
Spring Cloud Spring Boot mybatis distributed microservices Cloud Architecture-config-bus (13)--Two