(十五)spring cloud微服务分布式云架构-commonservice-config配置服务搭建

水深无声 2022-03-08 20:00 252阅读 0赞
  1. 介绍

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

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

    <?xml version=”1.0” encoding=”UTF-8”?>


    4.0.0


    com.ml.honghu
    commonservice
    0.0.1-SNAPSHOT


    commonservice-config
    jar

    commonservice-config
    Config Server



    org.springframework.cloud
    spring-cloud-config-server


    org.springframework.cloud
    spring-cloud-starter-eureka


    org.springframework.boot
    spring-boot-starter-security


    org.springframework.boot
    spring-boot-starter-test
    test






    org.springframework.boot
    spring-boot-maven-plugin


    1

    repackage



    2

    build-info






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

    package com.ml.honghu;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.config.server.EnableConfigServer;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

    @EnableConfigServer
    @EnableEurekaClient
    @SpringBootApplication
    public class ConfigApplication {

    1. public static void main(String[] args) {
    2. SpringApplication.run(ConfigApplication.class, args);
    3. }

    }

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

    server:
    port: 8888
    spring:
    application:

    1. name: commonservice-config-server

    profiles:

    1. active: discovery,native

    cloud:

    1. config:
    2. server:
    3. git:
    4. uri: http://192.168.0.254/honghu.../honghu-config.git
    5. username: honghu
    6. password: 123456
    7. searchPaths: config-dev

    security:
    basic:

    1. enabled: true

    user:

    1. name: honghu
    2. password: 123456

    eureka:
    client:

    1. serviceUrl:
    2. defaultZone: http://honghu:123456@localhost:8761/eureka/
    3. honghuZone: http://honghu:123456@localhost:8761/eureka/
    4. registry-fetch-interval-seconds: 300
    5. availability-zones:
    6. honghu: honghuZone

    instance:

    1. prefer-ip-address: true
    2. metadataMap:
    3. version: 1.0
    4. variant: A
    5. user: ${security.user.name}
    6. password: ${security.user.password}

    management:
    security:

    1. 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服务项目配置完毕!

发表评论

表情:
评论列表 (有 0 条评论,252人围观)

还没有评论,来说两句吧...

相关阅读