1. 程式人生 > >微服務框架——SpringCloud(四)

微服務框架——SpringCloud(四)

分布式配置 serve boot map nta name str 選擇 oid

1.Spring Cloud Config 分布式配置

  a.Config服務器

    ①新建springboot項目,依賴選擇Config Server

    ②pom文件關鍵依賴

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version
> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId>
</dependency> ...... </dependencies> ......

    ③application.yml文件

spring:
  application:
    name: config-server
  profiles:
    #配置文件在本地
    active: native
    #配置文件的目錄
    cloud:
      config:
        server:
          native:
            search
-locations: classpath:/config server: port: 8101

    ④啟動類添加註解@EnableConfigServer

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

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

}

    ⑤在resources下新建config/commom-dev.properties,用於測試

test.name=vettel
test.password=111111

    ⑥啟動後訪問 http://localhost:8101/common/dev 可查看配置文件信息,訪問路徑有如下幾種

      /{application}/{profile}[/{label}]

      /{application}-{profile}.yml

      /{label}/{application}-{profile}.yml

      /{application}-{profile}.properties

      /{label}/{application}-{profile}.properties

      註:對於resources下的config/commom-dev.properties,{application}為文件名"commom",{profile}為環境名"dev",{label}為路徑名"config"。

  b.Config客戶端

    ①新建springboot項目,依賴選擇 Config Client 、Web

    ②pom文件關鍵依賴

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <dependencies>
        <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>

        ......

    </dependencies>
    
    ......

    ③bootstrap.yml文件

spring:
  application:
    name: config-client
  cloud:
    config:
      uri: http://localhost:8101
      profile: dev
      name: common

server:
  port: 8102

    註意:此處需要將配置寫入bootstrap.yml(會優先於application.yml加載)中,因為config的配置是優先於application.yml加載的,否則會報錯。

    ④具體使用

@SpringBootApplication
@RestController
public class ConfigClientApplication {

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

    @Value("${test.name}")
    String name;
    @Value("${test.password}")
    String password;

    @RequestMapping(value="/getConfig")
    public String getConfig(){
        return "name[" + name + "], password[" + password + "]";
    }

}

微服務框架——SpringCloud(四)