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=###
一个工程对应多个配置文件
posted @ 2017-11-20 11:52  yuexijing  阅读(11726)  评论(2编辑  收藏  举报