• Spring Cloud Config Server 和Client 基本配置


    官网:http://cloud.spring.io/spring-cloud-config/single/spring-cloud-config.html#_quick_start

    Git 文件配置创建

    我的服务地址(码云):https://gitee.com/xiechenxuyuan/SpringCouldConfig

    如下:

    Spring Cloud Config Server

    1、创建项目

    从spring官网 :http://start.spring.io/创建一个简单的spring boot项目

    2、添加依赖

    
    		
    			org.springframework.boot
    			spring-boot-starter-web
    		
    		
    			org.springframework.cloud
    			spring-cloud-starter-config
    		
    		
    			org.springframework.cloud
    			spring-cloud-config-server
    		
            
    		
    			
    			
    			
    		
    		
    			org.springframework.boot
    			spring-boot-starter-test
    			test
    		
    	
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    3、在application.properties 里添加配置

    #服务端口
    server.port=8091
    #服务名称
    spring.application.name=configServer
    #服务注册中心 ,需要eureka 可以解开
    #eureka.client.service-url.defaultZone=http://localhost:9761/eureka/
    #服务的git仓库地址
    spring.cloud.config.server.git.uri=https://gitee.com/xiechenxuyuan/SpringCouldConfig
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    4、启动类添加注解

    @SpringBootApplication
    @EnableConfigServer
    //@EnableDiscoveryClient  //eurekaClinet注解
    public class SpringCloudConfigServerApplication {
    
    	public static void main(String[] args) {
    		SpringApplication.run(SpringCloudConfigServerApplication.class, args);
    	}
    
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    测试:http://localhost:8091/application/dev

    访问资源的形式如下:

    The HTTP service has resources in the following form:
    /{application}/{profile}[/{label}]
    /{application}-{profile}.yml
    /{label}/{application}-{profile}.yml
    /{application}-{profile}.properties
    /{label}/{application}-{profile}.properties

    {
        "name": "application",
        "profiles": [
            "dev"
        ],
        "label": null,
        "version": "e0fa159ad38a0a064963b37a6abca0f7bfeb2fac",
        "state": null,
        "propertySources": [
            {
                "name": "https://gitee.com/xiechenxuyuan/SpringCouldConfig/application-dev.properties",
                "source": {
                    "name": "shuaiqi",
                    "age": "22",
                    "version": "dev"
                }
            }
        ]
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    Spring Cloud Config Client

    1、创建基本的项目 ,如上

    2、添加依赖

    
    		
    			org.springframework.boot
    			spring-boot-starter-web
    		
    		
    			org.springframework.cloud
    			spring-cloud-starter-config
    		
    		
    			org.springframework.cloud
    			spring-cloud-config-server
    		
    
    		
    			
    			
    			
    		
    		
    			org.springframework.boot
    			spring-boot-starter-test
    			test
    		
    	
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    3、创建bootstrap.properties

    #文件夹名
    spring.application.name=application
    # dev根据具体情况来修改
    spring.cloud.config.profile=dev
    spring.cloud.config.label=master
    #eureka.client.service-url.defaultZone=http://localhost:9761/eureka/
    spring.cloud.config.uri= http://localhost:8091/
    #spring.cloud.config.discovery.enabled=true
    #文件服务的服务名
    spring.cloud.config.discovery.service-id=configServer
    server.port=2008
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    4、启动项添加注解 和测试内容

    @RestController
    @SpringBootApplication
    //@EnableDiscoveryClient // 表示这是一个Eureka客户端
    public class SpringCloudConfigClientApplication {
    
    	public static void main(String[] args) {
    		SpringApplication.run(SpringCloudConfigClientApplication.class, args);
    	}
    
    	@Value("${name}")
    	String name;
    
    	@Value("${version}")
    	String version;
    
    	@Value("${age}")
    	String age;
    	@RequestMapping("/")
    	public String home() {
    		return "name:" + name+",age:"+age+",version:"+version;
    	}
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    测试:输入localhost:2008

  • 相关阅读:
    rpm安装gitlab
    慢 SQL 分析与优化
    Java基础(一文读懂Java基础)
    力扣(LeetCode)82. 删除排序链表中的重复元素 II(C语言)
    学习Java的高级特性
    【网络基础】——HTTPS
    mysql报错:Duplicate entry ‘...‘ for key ‘field‘
    【云原生 | 拓展02】在单台宿主机上管理Docker容器
    python java php村委会管理系统vue+elementui
    Android 10.0 系统设置蓝牙配对时去掉配对框实现直接配对功能
  • 原文地址:https://blog.csdn.net/iijik55/article/details/126496801