9.1统一管理微服务config server和client

config server 是一个可横向扩展,集中式的配置服务器,默认使用git存储配置内容

config server,用于操作存储在 config server中的配置属性。微服务启动时,会请求config server获取所需要的配置属性,然后缓存这些属性。

在git建立文件

microservice-foo.properties
microservice-foo-dev.properties
microservice-froo-test.properties
microservice-froo-production.properties

内容为:

profile=production-1.0

创建:config-label-v2.0 分支,里面内容写 2.0

映入pom

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

开启server

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
  public static void main(String[] args) {
    SpringApplication.run(ConfigServerApplication.class, args);
  }
}

application.yml

server:
  port: 8080
spring:
  application:
    name: microservice-config-server
  cloud:
    config:
      server:
        git:
          uri: https://git.oschina.net/itmuch/spring-cloud-config-repo      # 配置Git仓库的地址
          username:                                                         # Git仓库的账号
          password:                                                         # Git仓库的密码

config server的端点

/{application}/{profiles}[/{label}] /项目名/dev/[/master]

/{application}-{profiles}.yml /项目名-dev.yml

/{label}/{application}-{profile}.yml /master/项目名-dev.yml

/{application}-{profile}.properties /项目名-dev.properties

/{label}/{application}-{profile}.properties /master/项目名-dev.properties

以上端点都可以映射到: {application}-{profile}.properties

http://localhost:8080/microservice-foo/dev/master

http://localhost:8080/microservice-foo/dev/ “label”:null,

{
    "name": "microservice-foo",
    "profiles": [
        "dev"
    ],
    "label": "master",
    "version": "sssdddf",
    "state": null,
    "propertySources": [
        {
            "name": "https://git.oschina.net/itmuch/spring-cloud-config-repo/microservice-foo-dev.properties",
            "source": {
                "profile": "dev-1.0"
            }
        },
        {
            "name": "https://git.oschina.net/itmuch/spring-cloud-config-repo/microservice-foo.properties",
            "source": {
                "profile": "default-1.0"
            }
        },
        {
            "name": "https://git.oschina.net/itmuch/spring-cloud-config-repo/application.properties",
            "source": {
                "profile": "default",
                "test": "1"
            }
        }
    ]
}

http://localhost:8080/microservice-foo-dev.yml

profile: dev-1.0
test: '1'

http://localhost:8080/microservice-foo-dev.properties

profile: dev-1.0
test: 1 //注意,这里是数字1

http://localhost:8080/config-label-v2.0/microservice-foo-dev.properties

profile: dev-2.0

编写config client

引入pom

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>

不用再main上开启注释,application.yml 不写配置

server:
  port: 8081

bootstrap.yml

spring:
  application:
    name: microservice-foo    # 对应config server所获取的配置文件的{application}
  cloud:
    config:
      uri: http://localhost:8080/
      profile: dev            # profile对应config server所获取的配置文件中的{profile} 
      label: master           # 指定Git仓库的分支,对应config server所获取的配置文件的{label}

引导上下文加载bootstrap.* 中的属性。配置在bootstrap.*中的属性有更高的优先级,因此默认环境下她们不能被本地配置覆盖

使用测试

@RestController
public class ConfigClientController {
  @Value("${profile}")
  private String profile;

  @GetMapping("/profile")
  public String hello() {
    return this.profile;
  }
}

http://localhost:8081/profile

结果:dev-1.0

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章