springcloud 分散式配置中心(spring cloud config)
阿新 • • 發佈:2018-11-24
官方中文文件:https://springcloud.cc/spring-cloud-config.html
條件:每次修改配置,都需要去找相應的配置檔案,造成了不必要的開銷,現在我們可以在遠端Git上修改,然後修改相應的服務
1.現在碼雲上建立一個私有專案config-repo
2.建立config專案
2.1pom檔案
<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>
因為config也是一個服務,需要註冊到Eureka上面
2.2啟動類
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}
2.3application.yml配置檔案
eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ spring: application: name: config cloud: config: server: git: uri: https://gitee.com/DencyCheng/config-repo username: DencyCheng password: xxxxx #改成自己的密碼 basedir: D:\ideawork\springcloud_sell\baseDir #從遠端Git快取下來的配置檔案下載地址
3.測試
3.1遠端GIt配置檔案
3.2啟動Eureka,啟動三個config例項
3.3訪問
上面可以看見,配置檔案跟Git上面的一樣