前置:配置注册中心
https://yjtzfywh.blog.csdn.net/article/details/129220590
配置中心:高级的集中化地配置文件管理工具。
一、配置服务端
文件目录
1、依赖
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies>
2、本地配置文件初始化
配置文件样例
bookservice-dev.yml server: port: 8201 # 数据库 spring: application: name: bookService datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/cloudstudy username: root password: 123456 eureka: client: # 跟上面一样,需要指向Eureka服务端地址,这样才能进行注册 service-url: defaultZone: http://eureka02:8802/eureka,http://eureka02:8801/eureka
3、配置文件
server: port: 8700 spring: application: name: configserver # 远程配置文件 cloud: config: # # 访问地址 # http://localhost:8700/bookservice/prod/master # http://localhost:8700/master/bookservice-prod.yml # 配置文件地址 server: git: uri: file://C:\Users\yjtzf\Desktop\config-repo default-label: master eureka: client: service-url: defaultZone: http://localhost:8801/eureka, http://localhost:8802/eureka
4、启动类
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication @EnableConfigServer public class ConfigApplication { public static void main(String[] args) { SpringApplication.run(ConfigApplication.class, args); } }
启动服务,查看注册中心。注册正常。
http://localhost:8802/
二、配置客户端
1、依赖
<!-- 配置--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bootstrap</artifactId> </dependency>
2、配置文件
bootstrap.yml
(在application.yml之前加载,可以实现配置文件远程获取)
bootstrap.yml
spring: cloud: config: # 名称,其实就是文件名称 name: bookservice # 配置服务器的地址 uri: http://localhost:8700 # 环境 profile: prod # 分支 label: master