SpringCloud ConfigServer如何讀取本地文件 ,以及如何在ConfigClient中使用

1.首先要建立springcloud_config工程。pom文件以下:
 
<?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.example.config</groupId>
<artifactId>springcloud_config</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
 
<name>springcloud_config</name>
<description>Demo project for Spring Boot</description>
 
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
 
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
 
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build></project>
 
2.在入口類註冊@EnableConfigServer @EnableEurekaClient
(1)並建立application.yml 把這個工程暴露到eruka中
端口號必須默認爲8888 ,緣由正在查找中
eureka:
client:
serviceUrl:
defaultZone: http://admin:password123@127.0.0.1:8760/eureka/
server:
port: 8888
spring:
application:
name: service-config
(2)而且配置application.properties文件
#設置配置文件在本地
spring.profiles.active=native
#設置配置文件的目錄
spring.cloud.config.server.native.search-locations=E:/SpringCloudWorkSpace/localServiceProperties
 
(3)本地配置文件名稱爲 service-producer-08-dev.properties
內容:
test.type=test
test.url=jdbc:oracle:thin:@xxxxxxxxx:ORCL
test.class=oracle.jdbc.OracleDriver
test.user=xxxxxx
test.password=xxxx
 
 
以上,配置config service完成
 
配置config client:
1.在pom.xml中增長
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
2.在application.yml設置
 
spring:
application:
name: service-producer-08
#這兩個文件讀取順序是application.yml>application.properties,故application.properties文件會#覆蓋application.yml文件的相同配置
jpa:
generate-ddl: false
show-sql: true
hibernate:
ddl-auto: none
datasource:
driver-class-name: ${test.class}
url: ${test.url}
username: ${test.user}
password: ${test.password}
 
2.1在application.properties中設置config服務
(須要注意的是,spring.application.name爲文件service-producer-08-dev.properties -dev前的全部字段不然會報錯 ,config服務設置必須在application.properties文件中,不然會報錯)
spring.application.name=service-producer-08
spring.cloud.config.profile=dev
spring.cloud.config.uri= http://localhost:8888/
這樣的話就會成功讀取到本地配置文件
發散:在configserver工程對應的本地文件夾中設置多個 xxxx-dev.properties文件,供多個工程使用
或者設置(xxxx-###.properties
spring.cloud.config.profile=dev 修改成 spring.cloud.config.profile=###
一個工程對應多個配置文件
相關文章
相關標籤/搜索