1. 程式人生 > >Springcloud之搭建Spring Cloud Config

Springcloud之搭建Spring Cloud Config

對Spring Cloud Config有所瞭解的話,就會知道,Spring Cloud Config註冊到eureka後,其他服務會通過eureka找到Spring cloud config,並在Config服務中讀取自己的配置檔案。

此配置檔案可以通過GIT伺服器獲取、在所在應該的目錄中獲取、在所在伺服器中的資料夾中獲取。

1.maven依賴

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.xichuan.dev</groupId>
	<artifactId>xichuan-config-server</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>xichuan-config-server</name>
	<description>config server</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.6.RELEASE</version>
		<relativePath/>
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<spring-cloud.version>Dalston.SR3</spring-cloud.version>
	</properties>

	<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>
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

2.application.yml配置

如果想將配置檔案放到專案中檔案,可以用一下配置

server:
  port: 2003

spring:
  application:
    name: xichuan-config-server
  profiles:
    active: native

  #本地測試使用
  context-path: /config

eureka:
  instance:
    preferIpAddress: true
    leaseRenewalIntervalInSeconds: 1
    leaseExpirationDurationInSeconds: 2
    nonSecurePort: ${server.port}
  client:
    serviceUrl:
      defaultZone: http://localhost:2001/eureka/

management:
  security:
    enabled: false

如果想讓配置檔案放到伺服器中某一目錄下,如/root/xichuan/configs目錄下,可以使用以下配置:

server:
  port: 2003

spring:
  application:
    name: xichuan-config-server
  profiles:
    active: native
##雲伺服器使用
  cloud:
    config:
      server:
        native:
          searchLocations:
            file:/root/xichuan/configs
    inetutils:
      ignoredInterfaces:
        - eth0

eureka:
  instance:
    preferIpAddress: true
    leaseRenewalIntervalInSeconds: 1
    leaseExpirationDurationInSeconds: 2
    nonSecurePort: ${server.port}
  client:
    serviceUrl:
      defaultZone: http://localhost:2001/eureka/

management:
  security:
    enabled: false

3.啟動類添加註解

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
public class XichuanConfigServerApplication {

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

 

我們可以看到eureka已經註冊了config-server服務

 

專案的啟動順序是:1.eureka,2.config-server,3.zuul(可選),4.服務