1. 程式人生 > >Spring Boot 入門(四)微服務之 Config Server 統一配置中心

Spring Boot 入門(四)微服務之 Config Server 統一配置中心

bootstra pan pat 默認 star default client efault localhost

一、目錄結構

技術分享

二、pom文件

<!-- 配置服務依賴 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <!-- Eureka 客戶端依賴-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import
</scope> </dependency> </dependencies> </dependencyManagement>

spring-cloud-dependencies 移至microservice中的pom文件

三、YML的配置

1.bootstrap.yml

spring:
  profiles:
    active: native #配置服務器使用本地配置,默認git配置
  application:
    name: micro-service-config # 在Eureka中註冊的服務名
    
eureka:
  instance:
    non
-secure-port: ${server.port:8763} # 環境變量中有值則使用環境變量中的值,如果沒有的話默認8080端口 metadata-map: instanceId: ${spring.application.name}:${random.value} #配置在Eureka server中的ID client: service-url: defaultZone : http://localhost1:8761/eureka/,http://localhost2:8762/eureka/ #註冊到Eureka Server

2.application.yml

spring:
  cloud:
    config:
      server:
        native:
          search-locations: classpath:/config #配置文件所在位置
server:
  port: 8763
  

四、啟動microservice-config

package com.nc.cloud.microservice.config;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableConfigServer //開啟配置服務器的支持
@EnableEurekaClient // 開啟 Eureka 客戶端的支持
public class ConfigApplication {
    
    public static void main(String[] args) {
        SpringApplication.run(ConfigApplication.class, args);
    }
}

Spring Boot 入門(四)微服務之 Config Server 統一配置中心