1.创建一个配置中心,这里我们使用码云上面的(自己创建)
2.导入包
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>
3.添加注解标签,主配置类
package cn.jiedada; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.config.server.EnableConfigServer; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; /** * @EnableConfigServer:开启配置中心 */ @SpringBootApplication @EnableEurekaClient @EnableConfigServer public class EurekaConfigService5000 { public static void main(String[] args) { new SpringApplicationBuilder(EurekaConfigService5000.class).web(true).run(args); } }
4.yml
eureka: client: serviceUrl: defaultZone: http://localhost:1000/eureka/#注册中心地址 instance: ip-address: true #使用ip配置 instance-id: config-server #指定服务的id server: port: 5000 spring: application: name: config-server cloud: config: server: git: uri: https://gitee.com/huangruijiedada/springcloud_hrm.git #路径 username: password:
其他配置文件使用的方式
1.导包
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId> </dependency>
2.更改yml配置
把application.yml的优先级提高改为bootstrap.yml这样才可以
spring: cloud: config: uri: http://localhost:5000 #这里是我们自己配置的服务器端口 name: application-user2000 #这里是我们放在仓库中的名称application-user2000-dev.yml profile: dev #这里是环境 label: master