SpringBoot-config

一、简介

Spring Cloud Config是一个配置管理工具包,让你可以把配置放到远程服务器,集中化管理集群配置,目前支持本地存储、Git以及Subversion。
Spring Cloud Config分为两部分

  • config-server:配置服务端,负责管理配置信息
  • config-client:配置客户端,通过调用server端暴露的接口来换取配置信息

二、项目实例(取本地文件实例)

SpringBoot-config_第1张图片

2.1 config-server 服务端

  2.1.1在pom.xml中加入以下依赖



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.5.RELEASE
         
    
    com.example
    demo
    0.0.1-SNAPSHOT
    demo
    Demo project for Spring Boot

    
        1.8
        Greenwich.SR1
    

    
        
            org.springframework.cloud
            spring-cloud-config-server
        
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            com.google.guava
            guava
            25.1-jre
        
        
            com.diffplug.guava
            guava-cache
            19.0.0
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

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


2.1.2 application.properties 配置

spring.application.name:configService
server.port=8888
#服务注册的Eureka Server地址
spring.cloud.config.server.native.searchLocations=D:/conf
spring.profiles.active=native //本地文件必须加此设置

2.1.3 D:/conf 文件

SpringBoot-config_第2张图片

文件内容

SpringBoot-config_第3张图片

2.1.4 开启Config Server

package com.example.demo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;



@EnableConfigServer
@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }


}

2.1.5 运行结果:

2.2 config-client客户端

  2.2.1在pom.xml中加入以下依赖



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.5.RELEASE
         
    
    com.example
    demo
    0.0.1-SNAPSHOT
    demo
    Demo project for Spring Boot

    
        1.8
        Greenwich.SR1
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        
       

        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            org.springframework.cloud
            spring-cloud-starter-config
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.cloud
            spring-cloud-netflix-core
            2.1.1.RELEASE
            compile
        


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

    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

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


2.2.2 application.properties 配置

spring.application.name:configClientdemo3
server.port=8889

spring.cloud.config.uri= http://localhost:8888/
spring.cloud.config.discovery.enabled=true
spring.cloud.config.profile=dev  //必须要设置
##spring.cloud.config.name=configClientdemo3
##spring.cloud.config.discovery.service-id=configService

2.2.3 开启config-client

 

package com.example.demo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@SpringBootApplication
@RestController

public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

   @Value("${name}")
    private  String name;

    @RequestMapping("/home")
    public String home() {
        return "Hello " + name;
    }
}

2.2.4 运行结果:

SpringBoot-config_第4张图片

三、项目实例(取本网上)

3.1项目图

SpringBoot-config_第5张图片

3.2 config-server 服务端

  3.2.1在pom.xml中加入以下依赖 (同上)

  3.2.2application.properties 配置

spring.application.name:configService
server.port=8808

#服务注册的Eureka Server地址

#spring.cloud.config.server.native.searchLocations=D:/conf
#spring.profiles.active=native
spring.cloud.config.server.git.uri=https://github.com/Liu19950513/config-reop.git
[email protected]
spring.cloud.config.server.git.password=asd13579
#spring.cloud.config.server.git.search-paths=configClientdemo
  ##07-springcloud-config

##spring.cloud.config.discovery.service-id=config-server
##spring.cloud.config.name=config-reop
#开启Config服务发现支持
##spring.cloud.config.discovery.enabled=true
##spring.cloud.config.discovery.service-id=configService

3.2.3 spring.cloud.config.server.git.uri

SpringBoot-config_第6张图片

注意: configClientdemo-dev.yml 里文件名称 configClientdemo 必须与 Config Server 项目名同名

3.2.4 开启Config Server 同上

package com.example.demo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;



@EnableConfigServer
@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }


}

3.2.5 运行结果

SpringBoot-config_第7张图片

3.3 config-client客户端

  3.3.1在pom.xml中加入以下依赖与 2.1.1在pom.xml中加入以下依赖 (一样)

  3.3.2application.properties 配置

spring.application.name=configClientdemo
server.port=8771

spring.cloud.config.uri=http://127.0.0.1:8808/
##spring.cloud.config.discovery.enabled=true
##spring.cloud.config.label=master
spring.cloud.config.profile=dev

 3.3.3 开启config-client

package com.example.demo;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@SpringBootApplication
@RestController
public class DemoApplication {



    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Value("${name}")
    private  String name;

    @RequestMapping("/home")
    public String home() {
        return "Hello " + name;
    }
}

3.3.4运行结果

SpringBoot-config_第8张图片

你可能感兴趣的:(SpringCloud)