4.三、啓動類ConfigServerApplication.javajava
package com.jacky.cloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication @EnableDiscoveryClient @EnableConfigServer public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }
5、客戶端(microservice-config-client)git
5.一、pom.xml配置文件web
<?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> <artifactId>miroservice-config</artifactId> <groupId>com.jacky</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>microservice-config-client</artifactId> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-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> <artifactId>docker-maven-plugin</artifactId> <executions> <!--設置在執行maven 的install時構建鏡像--> <execution> <id>build-image</id> <phase>install</phase> <goals> <goal>build</goal> </goals> </execution> </executions> <configuration> <!--安裝了docker的主機,而且打開了api remote接口設置--> <dockerHost>http://192.168.6.130:5678</dockerHost> <pushImage>true</pushImage><!--設置上傳鏡像到私有倉庫,須要docker設置指定私有倉庫地址--> <!--鏡像名稱--> <imageName>${docker.repostory}/${docker.image.prefix}/${project.artifactId}:${project.version}</imageName> <!--鏡像的基礎版本--> <baseImage>java:openjdk-8-jdk-alpine</baseImage> <!--鏡像啓動參數--> <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin> </plugins> </build> </project>
5.二、application.yml文件spring
View Code
5.三、bootstrap.yml文件docker
spring: cloud: config: username: jacky123 #configservice認證的用戶名 password: admin123 #認證密碼 label: master # 倉庫的分支節點 discovery: enabled: true service-id: microservice-config-service profile: dev #倉庫中對應文件的環境,如dev、prod、test等 fail-fast: true bus: trace: enabled: true #開啓消息跟蹤 application: name: microservice-config-client rabbitmq: host: 192.168.6.130 port: 5672 username: myuser password: mypass eureka: client: serviceUrl: defaultZone: http://jacky:admin@localhost:9511/eureka instance: prefer-ip-address: true management: security: enabled: false #刷新時關閉安全認證
注意:apache
上面這些屬性必須配置在bootstrap.yml,服務端的配置內容才能正確加載。由於經過bootstrap.yml的加載優先級比配置中心的服務端的高,服務端加載優先於application.yml,因此若是你把上面的配置寫在application.yml中,至關於默認不是從配置中心的服務端中讀取的配置信息,而是spring boot的默認加載。啓動的時候就會看到加載的配置,不是配置中心得配置內容,而是默認的本地的bootstrap
5.四、啓動類(ConfigClientApplication.java)api
package com.jacky.cloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class ConfigClientApplication { public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); } }
5.五、控制層類(ConfigClientController.java)安全
package com.jacky.cloud.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RefreshScope @RestController public class ConfigClientController { @Value("${profile}") private String profile; @GetMapping("/profile") public String getProfile() { return this.profile; } }
6、在碼雲建立git項目,放置配置文件app
地址:https://gitee.com/jacky-lulu/microservice-config-repo
7、測試
7.一、http://localhost:9511/
Spring Cloud大型企業分佈式微服務雲構建的B2B2C電子商務平臺源碼請加企鵝求求:一零三八七七四六二六