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

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