Fork me on GitHub

【Spring Cloud】服务配置中心

在GitHub上创建一个仓库

ps: 此仓库用于储存项目配置文件,也不要设置为私有仓库,否则需要配置用户名和密码

创建spring boot项目并添加依赖

创建项目过程略

添加依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>

服务配置

  • application.yml

    server:
      port: 9000
    spring:
      application:
        name: server-config
      cloud:
        config:
          server:
            git:
              uri: https://github.com/icodesoft/microservice-config //github仓库地址,注意不要在URL后面加'/'
    
  • 启动类添加注解@EnableConfigServer

    // 开户配置服务
    @EnableConfigServer
    @SpringBootApplication
    public class ServiceConfigApplication {
        public static void main(String[] args) {
            SpringApplication.run(ServiceConfigApplication.class, args);
        }
    }
    

测试

访问配置信息的URL与配置文件的映射关系如下:

  • /{application}/{profile}[/{label}]
  • /{application}-{profile}.yml
  • /{label}/{application}-{profile}.yml

完成了这些准备工作之后,我们就可以通过浏览器

ps: GitHub的默认主分支已从master改为main了,刚开始一直写master被坑怂了,哈哈

访问http://localhost:9000/service-config/dev/main`,并获得如下返回

{
    "name": "service-config",
    "profiles": [
        "dev"
    ],
    "label": "main",
    "version": null,
    "state": null,
    "propertySources": [
        {
            "name": "https://github.com/icodesoft/microservice-config/service-config-dev.yml",
            "source": {
                "info.profile": "dev"
            }
        },
        {
            "name": "https://github.com/icodesoft/microservice-config/service-config.yml",
            "source": {
                "info.profile": "default"
            }
        }
    ]
}

创建客户端项目

有了服务当然要搞个消费者来用用嘛

创建一个Spring Boot项目:service-order,并添加项目依赖

  • pom.xml

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    
  • 项目主类不用添加任务注解,只要一个@SpringBootApplication注解就够了

  • 创建文件bootstrap.yml

    ps: 这儿必须配置在bootstrap.properties中,不然config-server中的配置信息将不能被正确加载,为什么必须用这个文件,网上有很多我就不啰嗦了,问度娘

    spring:
      application:
        name: service-order
      cloud:
        config:
          label: main
          profile: dev
          uri: http://localhost:9000/
          name: service-config
    server:
      port: 8082
    

创建消费接口

  • 新建类HelloController

    package com.icodesoft.serviceorder.controller;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("/hello")
    public class HelloController {
        // 获取配置库中info.profile的值
        @Value("${info.profile}")
        private String profile;
    
        @GetMapping("/config")
        public String getProfile() {
            return "Get value from github repository- key[info.profile]: " + this.profile;
        }
    }
    

测试

启动service-config 和 service-order两个服务,并访问接口http://localhost:8082/hello/config, 返回结果如下:

Get value from github repository- key[info.profile]: dev
posted @ 2020-10-23 18:34  逍遥メ风  阅读(83)  评论(0编辑  收藏  举报