一、目录结构
二、pom文件
<!-- 配置服务依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <!-- Eureka 客户端依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency>
<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>
spring-cloud-dependencies 移至microservice中的pom文件
三、YML的配置
1.bootstrap.yml
spring: profiles: active: native #配置服务器使用本地配置,默认git配置 application: name: micro-service-config # 在Eureka中注册的服务名 eureka: instance: non-secure-port: ${server.port:8763} # 环境变量中有值则使用环境变量中的值,如果没有的话默认8080端口 metadata-map: instanceId: ${spring.application.name}:${random.value} #配置在Eureka server中的ID client: service-url: defaultZone : http://localhost1:8761/eureka/,http://localhost2:8762/eureka/ #注册到Eureka Server
2.application.yml
spring: cloud: config: server: native: search-locations: classpath:/config #配置文件所在位置 server: port: 8763
四、启动microservice-config
package com.nc.cloud.microservice.config; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableConfigServer //开启配置服务器的支持 @EnableEurekaClient // 开启 Eureka 客户端的支持 public class ConfigApplication { public static void main(String[] args) { SpringApplication.run(ConfigApplication.class, args); } }