• SpringCloud第六步:搭建分布式配置中心


    什么是配置中心

    在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在spring cloud config 组件中,分两个角色,一是config server,二是config client。

    创建git地址

    1. 使用码云创建git地址 https://gitee.com/itmayi
    2. config-client-dev.properties  ---dev环境
    3. 上传配置文件

    userName=yushengjun

    4. http://localhost:8889/foo/dev 查询配置中心

    创建config-server项目

      <parent>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-starter-parent</artifactId>

                <version>1.5.2.RELEASE</version>

                <relativePath /> <!-- lookup parent from repository -->

          </parent>

     

          <properties>

                <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

                <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

                <java.version>1.8</java.version>

          </properties>

     

          <dependencies>

                <dependency>

                      <groupId>org.springframework.cloud</groupId>

                      <artifactId>spring-cloud-config-server</artifactId>

                </dependency>

     

                <dependency>

                      <groupId>org.springframework.boot</groupId>

                      <artifactId>spring-boot-starter-test</artifactId>

                      <scope>test</scope>

                </dependency>

     

                <dependency>

                      <groupId>org.springframework.cloud</groupId>

                      <artifactId>spring-cloud-starter-eureka</artifactId>

                </dependency>

          </dependencies>

     

          <dependencyManagement>

                <dependencies>

                      <dependency>

                           <groupId>org.springframework.cloud</groupId>

                           <artifactId>spring-cloud-dependencies</artifactId>

                           <version>Camden.SR6</version>

                           <type>pom</type>

                           <scope>import</scope>

                      </dependency>

                </dependencies>

          </dependencyManagement>

     

     

          <build>

                <plugins>

                      <plugin>

                           <groupId>org.springframework.boot</groupId>

                           <artifactId>spring-boot-maven-plugin</artifactId>

                      </plugin>

                </plugins>

          </build>

     

          <repositories>

                <repository>

                      <id>spring-milestones</id>

                      <name>Spring Milestones</name>

                      <url>https://repo.spring.io/milestone</url>

                      <snapshots>

                           <enabled>false</enabled>

                      </snapshots>

                </repository>

          </repositories>

    application.properties 配置文件

    spring.application.name=config-server

    server.port=8889

    spring.cloud.config.server.git.uri=https://gitee.com/itmayi/cfg2.git

    spring.cloud.config.server.git.searchPaths=respo

    spring.cloud.config.label=master

    spring.cloud.config.server.git.username=

    spring.cloud.config.server.git.password=

    spring.cloud.config.server.git.uri:配置git仓库地址

    spring.cloud.config.server.git.searchPaths:配置仓库路径

    spring.cloud.config.label:配置仓库的分支

    spring.cloud.config.server.git.username:访问git仓库的用户名

    spring.cloud.config.server.git.password:访问git仓库的用户密码

    启动config-server项目

    @SpringBootApplication

    @EnableConfigServer

    public class ConfigServerApplication {

          public static void main(String[] args) {

                SpringApplication.run(ConfigServerApplication.class, args);

          }

    }

    创建config-client项目

      <parent>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-starter-parent</artifactId>

                <version>1.5.2.RELEASE</version>

                <relativePath /> <!-- lookup parent from repository -->

          </parent>

     

          <properties>

                <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

                <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

                <java.version>1.8</java.version>

          </properties>

     

          <dependencies>

                <dependency>

                      <groupId>org.springframework.cloud</groupId>

                      <artifactId>spring-cloud-starter-config</artifactId>

                </dependency>

     

                <dependency>

                      <groupId>org.springframework.boot</groupId>

                      <artifactId>spring-boot-starter-web</artifactId>

                </dependency>

     

                <dependency>

                      <groupId>org.springframework.boot</groupId>

                      <artifactId>spring-boot-starter-test</artifactId>

                      <scope>test</scope>

                </dependency>

          </dependencies>

     

          <dependencyManagement>

                <dependencies>

                      <dependency>

                           <groupId>org.springframework.cloud</groupId>

                           <artifactId>spring-cloud-dependencies</artifactId>

                           <version>Dalston.RC1</version>

                           <type>pom</type>

                           <scope>import</scope>

                      </dependency>

                </dependencies>

          </dependencyManagement>

     

          <build>

                <plugins>

                      <plugin>

                           <groupId>org.springframework.boot</groupId>

                           <artifactId>spring-boot-maven-plugin</artifactId>

                      </plugin>

                </plugins>

          </build>

     

          <repositories>

                <repository>

                      <id>spring-milestones</id>

                      <name>Spring Milestones</name>

                      <url>https://repo.spring.io/milestone</url>

                      <snapshots>

                           <enabled>false</enabled>

                      </snapshots>

                </repository>

          </repositories>

    bootstrap.properties

    spring.application.name=config-client

    spring.cloud.config.label=master

    spring.cloud.config.profile=dev

    spring.cloud.config.uri= http://localhost:8888/

    server.port=8881

    • spring.cloud.config.label 指明远程仓库的分支
    • spring.cloud.config.profile
      • dev开发环境配置文件
      • test测试环境
      • pro正式环境
      • spring.cloud.config.uri= http://localhost:8888/ 指明配置服务中心的网址。

    @SpringBootApplication

    @RestController

    public class ConfigClientApplication {

     

          public static void main(String[] args) {

                SpringApplication.run(ConfigClientApplication.class, args);

          }

     

          @Value("${userName}")

          String userName;

     

          @RequestMapping(value = "/getUserName")

          public String getUserName () {

                return userName;

          }

    }

  • 相关阅读:
    Codeforces Ilya and Matrix
    poj 1308 Is It A Tree?
    Codeforces Sereja and Array
    poj 1041 John's trip
    Codeforces Continued Fractions
    WM_COPYDATA实现进程间数据通信
    虚拟机中安装ubuntu后,终端模式和图形模式切换
    IIS上配置运行cgi,php,aspx运行环境
    linux c main函数参数
    Linux网络编程入门 (转载)
  • 原文地址:https://www.cnblogs.com/XJJD/p/10451023.html
Copyright © 2020-2023  润新知