- 介绍
Spring Cloud Config为分布式系统中的内部配置提供服务器和客户端反对。应用Config Server,您能够在所有环境中管理应用程序的内部属性。客户端和服务器上的概念映射与Spring Environment
和PropertySource
形象雷同,因而它们与Spring应用程序十分符合,但能够与任何以任何语言运行的应用程序一起应用。随着应用程序通过从开发人员到测试和生产的部署流程,您能够治理这些环境之间的配置,并确定应用程序具备迁徙时须要运行的所有。服务器存储后端的默认实现应用git,因而它轻松反对标签版本的配置环境,以及能够拜访用于治理内容的各种工具。很容易增加代替实现,并应用Spring配置将其插入。(企业架构源码能够加求球:叁五三陆二肆柒二伍玖)
- 引入pom相干jar包,其中pom.xml配置如下:
1. <?xml version="1.0" encoding="UTF-8"?> 2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4. <modelVersion>4.0.0</modelVersion> 6. <parent> 7. <groupId>com.ml.honghu</groupId> 8. <artifactId>commonservice</artifactId> 9. <version>0.0.1-SNAPSHOT</version> 10. </parent> 12. <artifactId>commonservice-config</artifactId> 13. <packaging>jar</packaging> 15. <name>commonservice-config</name> 16. <description>Config Server</description> 18. <dependencies> 19. <dependency> 20. <groupId>org.springframework.cloud</groupId> 21. <artifactId>spring-cloud-config-server</artifactId> 22. </dependency> 23. <dependency> 24. <groupId>org.springframework.cloud</groupId> 25. <artifactId>spring-cloud-starter-eureka</artifactId> 26. </dependency> 27. <dependency> 28. <groupId>org.springframework.boot</groupId> 29. <artifactId>spring-boot-starter-security</artifactId> 30. </dependency> 31. <dependency> 32. <groupId>org.springframework.boot</groupId> 33. <artifactId>spring-boot-starter-test</artifactId> 34. <scope>test</scope> 35. </dependency> 36. </dependencies> 38. <build> 39. <plugins> 40. <plugin> 41. <groupId>org.springframework.boot</groupId> 42. <artifactId>spring-boot-maven-plugin</artifactId> 43. <executions> 44. <execution> 45. <id>1</id> 46. <goals> 47. <goal>repackage</goal> 48. </goals> 49. </execution> 50. <execution> 51. <id>2</id> 52. <goals> 53. <goal>build-info</goal> 54. </goals> 55. </execution> 56. </executions> 57. </plugin> 58. </plugins> 59. </build> 60. </project>
- 在src/main/java进行ConfigApplication.java启动文件配置:
1. package com.ml.honghu; 3. import org.springframework.boot.SpringApplication; 4. import org.springframework.boot.autoconfigure.SpringBootApplication; 5. import org.springframework.cloud.config.server.EnableConfigServer; 6. import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 8. @EnableConfigServer 9. @EnableEurekaClient 10. @SpringBootApplication 11. public class ConfigApplication { 13. public static void main(String[] args) { 14. SpringApplication.run(ConfigApplication.class, args); 15. } 16. }
- 在src/main/resource下进行bootstrap.yml配置
1. server: 2. port: 8888 3. spring: 4. application: 5. name: commonservice-config-server 6. profiles: 7. active: discovery,native 8. cloud: 9. config: 10. server: 11. git: 12. uri: http://192.168.0.254/honghu.../honghu-config.git 13. username: honghu 14. password: 123456 15. searchPaths: config-dev 16. security: 17. basic: 18. enabled: true 19. user: 20. name: honghu 21. password: 123456 22. eureka: 23. client: 24. serviceUrl: 25. defaultZone: http://honghu:123456@localhost:8761/eureka/ 26. honghuZone: http://honghu:123456@localhost:8761/eureka/ 27. registry-fetch-interval-seconds: 300 28. availability-zones: 29. honghu: honghuZone 30. instance: 31. prefer-ip-address: true 32. metadataMap: 33. version: 1.0 34. variant: A 35. user: ${security.user.name} 36. password: ${security.user.password} 37. management: 38. security: 39. enabled: false
留神: 如果不从近程git或者svn库加载配置文件信息,能够配置加载本地地址,比方window下配置应用:
1. server: 2. port: 8888 3. spring: 4. application: 5. name: commonservice-config-server 6. profiles: 7. active: discovery,native 8. cloud: 9. config: 10. server: 11. <span style="color: #ff0000;">native.searchLocations: d:/honghu-config</span> 12. security: 13. basic: 14. enabled: true 15. user: 16. name: honghu 17. password: 123456 18. eureka: 19. client: 20. serviceUrl: 21. defaultZone: http://honghu:123456@localhost:8761/eureka/ 22. honghuZone: http://honghu:123456@localhost:8761/eureka/ 23. registry-fetch-interval-seconds: 300 24. availability-zones: 25. honghu: honghuZone 26. instance: 27. prefer-ip-address: true 28. metadataMap: 29. version: 1.0 30. variant: A 31. user: ${security.user.name} 32. password: ${security.user.password} 33. management: 34. security: 35. enabled: false
到此,整个config服务项目配置结束!!
从当初开始,我这边会将近期研发的spring cloud微服务云架构的搭建过程和精华记录下来,帮忙更多有趣味研发spring cloud框架的敌人,大家来一起探讨spring cloud架构的搭建过程及如何使用于企业我的项目。