Skip to content

spring cloud 学习笔记0x6 #23

@hugeterry

Description

@hugeterry

分布式配置中心

构建config Server

  • 在已有工程下创建module,起名为scconfig-server
    config Server(配置中心) new ->moudle ->Spring initialir->next 填写moudle名->cloud config->✅config server

  • 在application启动类增加注解@ EnableConfigServer,开启配置服务器

    @SpringBootApplication
    @EnableConfigServer
    public class ScconfigServerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ScconfigServerApplication.class, args);
        }
    }
  • 配置文件

    spring.application.name=config-server
    server.port=8888
    
    spring.cloud.config.server.git.uri=https://github.com/do9len/SpringcloudConfig
    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仓库的用户密码 公开仓库可不填

    远程仓库https://github.com/do9len/SpringcloudConfig 中有如下属性
    name = hugeterry version 1
    声明了该仓库application为hugeterry

  • 启动,访问http://localhost:8555/hugeterry/dev ,返回如下报文

    {"name":"hugeterry","profiles":["dev"],"label":null,"version":"faf3340174cd0bf9b12853c67f655a82482711b1","state":null,"propertySources":[]}

    访问http://localhost:8555/config-client/dev ,返回如下报文

    {"name":"config-client","profiles":["dev"],"label":null,"version":"faf3340174cd0bf9b12853c67f655a82482711b1","state":null,"propertySources":[{"name":"https://github.com/do9len/SpringcloudConfig//respo/config-client-dev.properties","source":{"name":"hugeterry version 1","democonfigclient.message":"hello spring io"}}]}

    访问http://localhost:8555/config-client-dev.propertieshttp://localhost:8555/master/config-client-dev.properties ,返回如下报文

    democonfigclient.message: hello spring io
    name: hugeterry version 1

    配置服务中心可以从远程程序获取配置信息,http请求地址和资源文件映射如下:

    • /{application}/{profile}[/{label}]
    • /{application}-{profile}.yml
    • /{label}/{application}-{profile}.yml
    • /{application}-{profile}.properties
    • /{label}/{application}-{profile}.properties
      application 配置文件中name,或者根据文件名获取
      label 分支
      profile 为测试环境,一般有如下选项
      • dev开发环境
      • test测试环境
      • pro正式环境

构建config client

  • 在已有工程下创建module,起名为scconfig-client
    new ->moudle ->Spring initialir->next 填写moudle名->cloud config->✅config client

  • 增加起步依赖spring-boot-starter-web

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
  • 增加配置文件bootstrap.properties

    spring.application.name=config-client
    server.port=8551
    spring.cloud.config.label=master
    spring.cloud.config.profile=dev
    spring.cloud.config.uri= http://localhost:8555/

    关于参数的意义与上文链接映射相似,这里多出一个spring.cloud.config.uri为配置服务中心

  • 在application启动类里增加“/c”API,获取git文件里name的值

    @SpringBootApplication
    @RestController
    public class ScconfigClientApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ScconfigClientApplication.class, args);
        }
    
        @Value("${name}")
        String name;
    
        @RequestMapping(value = "/c")
        public String c() {
            return "info:" + name;
        }
    }
  • 访问http://localhost:8551/c 返回如下报文

    info:hugeterry version 1

    这个过程的调用关系为 scconfigclient->scconfigserver->github

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions