ITPub博客

首页 > 应用开发 > Java > (九)整合spring cloud云服务架构 - commonservice-config配置服务搭建

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

原创 Java 作者:fumi106 时间:2020-10-26 14:15:28 0 删除 编辑

1. 介绍

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

2. 引入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/maven-4.0.0.xsd" >   
  4.      < modelVersion > 4.0.0 </ modelVersion >   
  5.   
  6.      < parent >   
  7.          < groupId > com.ml.honghu </ groupId >   
  8.          < artifactId > commonservice </ artifactId >   
  9.          < version > 0.0.1-SNAPSHOT </ version >   
  10.      </ parent >   
  11.       
  12.      < artifactId > commonservice-config </ artifactId >   
  13.      < packaging > jar </ packaging >   
  14.   
  15.      < name > commonservice-config </ name >   
  16.      < description > Config Server </ description >   
  17.   
  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 >   
  37.   
  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 >   

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

 

Java代码   收藏代码
  1. package  com.ml.honghu;  
  2.   
  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;  
  7.   
  8. @EnableConfigServer   
  9. @EnableEurekaClient   
  10. @SpringBootApplication   
  11. public   class  ConfigApplication {  
  12.   
  13.      public   static   void  main(String[] args) {  
  14.         SpringApplication.run(ConfigApplication. class , args);  
  15.     }  
  16. }  

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

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


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69984376/viewspace-2729765/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2020-09-22

  • 博文量
    51
  • 访问量
    25934