(九)整合spring cloud云服务架构 - commonservice-config配置服务搭建

  1. 介绍

Spring Cloud Config为分布式系统中的外部配置提供服务器和客户端支持。使用Config Server,您可以在所有环境中管理应用程序的外部属性。客户端和服务器上的概念映射与Spring Environment和PropertySource抽象相同,因此它们与Spring应用程序非常契合,但可以与任何以任何语言运行的应用程序一起使用。随着应用程序通过从开发人员到测试和生产的部署流程,您可以管理这些环境之间的配置,并确定应用程序具有迁移时需要运行的一切。服务器存储后端的默认实现使用git,因此它轻松支持标签版本的配置环境,以及可以访问用于管理内容的各种工具。很容易添加替代实现,并使用Spring配置将其插入。(企业架构源码可以加求球:叁五三陆二肆柒二伍玖)

  1. 引入pom相关jar包,其中pom.xml配置如下:

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/m...d">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>com.ml.honghu</groupId>
  7. <artifactId>commonservice</artifactId>
  8. <version>0.0.1-SNAPSHOT</version>
  9. </parent>
  10. <artifactId>commonservice-config</artifactId>
  11. <packaging>jar</packaging>
  12. <name>commonservice-config</name>
  13. <description>Config Server</description>
  14. <dependencies>
  15. <dependency>
  16. <groupId>org.springframework.cloud</groupId>
  17. <artifactId>spring-cloud-config-server</artifactId>
  18. </dependency>
  19. <dependency>
  20. <groupId>org.springframework.cloud</groupId>
  21. <artifactId>spring-cloud-starter-eureka</artifactId>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-security</artifactId>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.springframework.boot</groupId>
  29. <artifactId>spring-boot-starter-test</artifactId>
  30. <scope>test</scope>
  31. </dependency>
  32. </dependencies>
  33. <build>
  34. <plugins>
  35. <plugin>
  36. <groupId>org.springframework.boot</groupId>
  37. <artifactId>spring-boot-maven-plugin</artifactId>
  38. <executions>
  39. <execution>
  40. <id>1</id>
  41. <goals>
  42. <goal>repackage</goal>
  43. </goals>
  44. </execution>
  45. <execution>
  46. <id>2</id>
  47. <goals>
  48. <goal>build-info</goal>
  49. </goals>
  50. </execution>
  51. </executions>
  52. </plugin>
  53. </plugins>
  54. </build>
  55. </project>

 3. 在src/main/java进行ConfigApplication.java启动文件配置:

Java代码  收藏代码 "收藏这段代码")

  1. package com.ml.honghu;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.config.server.EnableConfigServer;
  5. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  6. @EnableConfigServer
  7. @EnableEurekaClient
  8. @SpringBootApplication
  9. public class ConfigApplication {
  10. public static void main(String[] args) {
  11. SpringApplication.run(ConfigApplication.class, args);
  12. }
  13. }

 4. 在src/main/resource下进行bootstrap.yml配置

Java代码  收藏代码 "收藏这段代码")

  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.....
  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下配置使用:

Java代码  收藏代码 "收藏这段代码")

  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架构的搭建过程及如何运用于企业项目。

评论

暂无评论