zoukankan      html  css  js  c++  java
  • Spring Cloud 之分布式配置基础应用

    分布式配置基础应用

    配置中心服务

    spring-config-server

    pom.xml

    
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>org.galsang.cloud</groupId>
        <artifactId>spring-config-server</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>spring-config-server</name>
        <description>配置中心服务</description>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.9.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>
            <spring-cloud.version>Edgware.RELEASE</spring-cloud.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-config-server</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka</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>${spring-cloud.version}</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>
    
    
    </project>
    
    

    application-dev.yml

    spring:
      profiles: dev
      cloud:
        inetutils:
          preferred-networks:
            - 192.168
        config:
          server:
            git:
              uri: https://gitee.com/vimx86/SpringCloud-Learning/ # git 配置仓库地址
              search-paths: galsang-z-docs/config-repo
              # 由于是公开git 仓库,故不使用账号密码
    #          username:
    #          password:
    
      # git管理配置
      # /{application}/{profile}[/{label}]
      # /{application}-{profile}.yml
      # /{label}/{application}-{profile}.yml
      # /{application}-{profile}.properties
      # /{label}/{application}-{profile}.properties
    
      # http://www.galsang.org:9001/application-dev/dev/master
      # http://www.galsang.org:9001/application/dev/master
    
      # http://www.galsang.org:9001/application-dev.properties   默认使用master分支中的配置文件
      # http://www.galsang.org:9001/dev/application-beta.properties 使用指定分支中的配置文件
    
    server:
      port: 9001
      context-path: /
      display-name: www.galsang.org
    
    # 服务注册配置
    eureka:
    # 作为服务进行注册
      client:
        register-with-eureka: true
        fetch-registry: true
        enabled: true
        service-url:
          defaultZone: http://www.galsang.org:9000/eureka/
      instance:
        prefer-ip-address: true
        instance-id: ${spring.application.name}:${server.display-name}:${server.port}
        leaseRenewalIntervalInSeconds: 1
        leaseExpirationDurationInSeconds: 2

    SpringConfigServerApplication.java

    
    package org.galsang.cloud;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    import org.springframework.cloud.config.server.EnableConfigServer;
    
    @SpringBootApplication
    @EnableConfigServer // 激活配置中心服务
    @EnableDiscoveryClient // 激活Eureka中的DiscoveryClient 服务发现
    public class SpringConfigServerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringConfigServerApplication.class, args);
        }
    }
    

    配置客户服务

    spring-config-client

    pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
     <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>org.galsang.cloud</groupId>
        <artifactId>spring-config-client</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>spring-config-client</name>
        <description>配置客户服务</description>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.9.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>
            <spring-cloud.version>Edgware.RELEASE</spring-cloud.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-config</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka</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>${spring-cloud.version}</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>
    
    
     </project>
    
    

    bootstrap.yml

    spring:
      application:
        name: spring-config-client
      cloud:
        config:
          profile: dev # 环境
          label: master # git 仓库分支
          uri: http://www.galsang.org:9001/  # 配置中心地址
    server:
      port: 9002
      context-path: /
      display-name: www.galsang.org
    
    eureka:
      client:
        register-with-eureka: true
        fetch-registry: true
        enabled: true
        service-url:
          defaultZone: http://www.galsang.org:9000/eureka/
      instance:
          prefer-ip-address: true
          instance-id: ${spring.application.name}:${server.display-name}:${server.port}
          leaseRenewalIntervalInSeconds: 1
          leaseExpirationDurationInSeconds: 2

    SpringConfigServerApplication.java

    
    package org.galsang.cloud;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    
    @SpringBootApplication
    @EnableDiscoveryClient
    public class SpringConfigClientApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringConfigClientApplication.class, args);
        }
    }
    

    TestConfigController.java

    
    package org.galsang.cloud.controller;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * Description:测试 使用 配置中心 配置文件的 接口
     * <br /> Author: vimx86
     */
    @RestController
    public class TestConfigController {
    
    
        @Value("${from}")
        private String from;
    
        @RequestMapping("/from")
        public String from() {
            return from;
        }
    
    }
    

    效果预览

    浏览器打开 http://www.galsang.org:9002/from

    这里写图片描述

    说明访问git仓库中的配置成功。

    源码请移步: https://gitee.com/vimx86/SpringCloud-Learning

    参考资料

  • 相关阅读:
    BufferedOutputStream
    BufferedInputStream
    IO异常 的处理
    FileOutStream
    FileInputStream
    File常用的方法
    IO流
    枚举
    jdk1.5新特性之-----自动装箱与自动拆箱
    jdk1.5新特性之------->可变参数
  • 原文地址:https://www.cnblogs.com/ljmatlight/p/9060776.html
Copyright © 2011-2022 走看看