ITPub博客

首页 > IT职业 > IT职场 > 分布式配置

分布式配置

IT职场 作者:youyouaq 时间:2021-09-09 09:33:45 0 删除 编辑

分布式配置:配置管理工具包,让你可以把配置放到远程服务器,集中化管理集群配置,目前支持本地存储、Git以及Subversion。

在生产环境,由于配置繁多,造成运维人员或开发人员配置上的不便,springcloud提供了分布式配置中心组建,支持从远程配置仓库(git)中读取配置。

读取配置流程如下图:

图片描述

config-server从git中读取远程配置,config-client在从config-server中读取配置。

新建项目config-server,pom文件中加入config-server依赖,完整代码如下:


    4.0.0

    com.dalaoyang
    springcloud_config_server
    0.0.1-SNAPSHOT
    jar

    springcloud_config_server
    springcloud_config_server

    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.9.RELEASE
         <!-- lookup parent from repository -->
    

    
        UTF-8
        UTF-8
        1.8
        Edgware.SR1
    

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

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

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

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

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

启动类加入@EnableConfigServer注解,表明这是一个config-server并开启配置服务器功能。代码如下:

package com.dalaoyang;

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

@SpringBootApplication
@EnableConfigServer
public class SpringcloudConfigServerApplication {

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

application.properties配置如下:

##端口号
server.port=9000

##服务名称
spring.application.name=config_server

##注册中心地址
eureka.client.service-url.defaultZone=http://eureka.dalaoyang.cn/eureka/

##远程仓库配置

##git仓库地址
spring.cloud.config.server.git.uri=https://gitee.com/dalaoyang/SpringCloudConfig.git

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

##git仓库分支
spring.cloud.config.label=master

##如果为公开仓库,用户名密码可不填
##git仓库用户名
##spring.cloud.config.server.git.username=

##git仓库密码
##spring.cloud.config.server.git.password=

config-server到这里构建完成,然后新建一个config-client服务。pom文件代码如下:

4.0.0com.dalaoyangspringcloud_config_client0.0.1-SNAPSHOTjarspringcloud_config_clientspringcloud_config_clientorg.springframework.bootspring-boot-starter-parent1.5.9.RELEASE <!-- lookup parent from repository -->
	UTF-8UTF-81.8Edgware.SR1org.springframework.cloudspring-cloud-starter-eurekaorg.springframework.bootspring-boot-starter-testtestorg.springframework.cloudspring-cloud-starter-configorg.springframework.cloudspring-cloud-dependencies${spring-cloud.version}pomimportorg.springframework.bootspring-boot-maven-plugin

然后新建一个配置文件,这里需要说明一下,config-client默认会去找8888端口的配置中心,如果配置中心使用8888端口,config-client可以使用application.properties配置文件。如果配置中心没有使用8888端口,那么config-client要使用bootstrap.properties,原因是bootstrap.* 命名的文件会被优先读取和配置。由于这个例子的项目使用端口为9000,所以新建一个bootstrap.properties,代码如下:

spring.application.name=config-client
spring.cloud.config.label=master
spring.cloud.config.profile=test
spring.cloud.config.uri= http://localhost:9000/

##注册中心地址
eureka.client.service-url.defaultZone=http://eureka.dalaoyang.cn/eureka/

然后做一个测试类,修改一下application,代码如下:

package com.dalaoyang;

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 SpringcloudConfigClientApplication {

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


	@Value("${title}")
	String title;

	@RequestMapping("/getTitle")
	public String getTitle(){
		return title;
	}

}

到这里就全部构建完成了,依次启动config-server和config-client。

先访问一下http://eureka.dalaoyang.cn/
图片描述

可以看到服务构建成功,然后访问http://localhost:8080/getTitle,可以看到:

图片描述

源码下载 :大老杨码云


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/3050/viewspace-2804718/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-13

  • 博文量
    90
  • 访问量
    786