目录
1.什么是Config2.使用2.1 创建config服务端2.2 pom2.3 application.yml2.4 properties下的neo-config-dev.properties2.5 启动类2.6 改造book1 添加springcloudconfig配置中心的客户端的依赖2 优化application.yml3.新建bootstrap.yml,名字不可变4.修改启动类,加入配置中心的客户端注解
2.7 改造user1 添加springcloudconfig配置中心的客户端的依赖2 优化application.yml3.新建bootstrap.yml,名字不可变4.修改启动类,加入配置中心的客户端注解
2.8 测试正常
1.什么是Config
提供服务端和客户端支持
集中管理各环境的配置文件
配置文件修改之后,可以快速的生效
可以进行版本管理
支持大的并发查询
支持各种语言
Spring Cloud Config项目是一个解决分布式系统的配置管理方案。它包含了Client和Server两个部分,server提供配置文件的存储、以接口的形式将配置文件的内容提供出去,client通过接口获取数据、并依据此数据初始化自己的应用。Spring cloud使用git或svn存放配置文件,默认情况下使用git
2.使用
2.1 创建config服务端
2.2 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">
<parent
>
<artifactId
>springCloud_parent
</artifactId
>
<groupId
>com
.wo
</groupId
>
<version
>1.0-SNAPSHOT</version
>
</parent
>
<modelVersion
>4.0.0</modelVersion
>
<artifactId
>config
</artifactId
>
<dependencies
>
<dependency
>
<groupId
>org
.springframework
.boot
</groupId
>
<artifactId
>spring
-boot
-starter
-web
</artifactId
>
</dependency
>
<!--引入config配置中心服务端的依赖
-->
<dependency
>
<groupId
>org
.springframework
.cloud
</groupId
>
<artifactId
>spring
-cloud
-config
-server
</artifactId
>
</dependency
>
<!-- eureka 客户端的依赖
-->
<dependency
>
<groupId
>org
.springframework
.cloud
</groupId
>
<artifactId
>spring
-cloud
-starter
-netflix
-eureka
-client
</artifactId
>
</dependency
>
</dependencies
>
</project
>
2.3 application.yml
server
:
port
: 9000
spring
:
application
:
name
: config
#标注配置中心使用本地的方式
profiles
:
active
: native
cloud
:
config
:
server
:
native
:
search
-locations
: classpath
:properties
/
eureka
:
client
:
service
-url
:
defaultZone
: http
://localhost
:8888/eureka
2.4 properties下的neo-config-dev.properties
#数据库连接信息
spring
.datasource
.driver
-class-name
=com
.mysql
.jdbc
.Driver
spring
.datasource
.url
=jdbc
:mysql
://localhost
:3306/qf
?useUnicode
=true&characterEncoding
=utf8
&useSSL
=false
spring
.datasource
.username
=root
spring
.datasource
.password
=123456
#mybatis
mybatis
.mapper
-locations
=classpath
:mapper
2.5 启动类
@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigApplication {
public static void main(String
[] args
) {
SpringApplication
.run(ConfigApplication
.class);
}
}
启动,配置服务 本地完成
2.6 改造book
1 添加springcloudconfig配置中心的客户端的依赖
<!-- 添加springcloudconfig配置中心的客户端的依赖
-->
<dependency
>
<groupId
>org
.springframework
.cloud
</groupId
>
<artifactId
>spring
-cloud
-starter
-config
</artifactId
>
</dependency
>
2 优化application.yml
server
:
port
: 8081
spring
:
application
:
name
: book
3.新建bootstrap.yml,名字不可变
spring
:
cloud
:
config
:
name
: neo
-config
profile
: dev
discovery
:
enabled
: true
service
-id
: config
eureka
:
client
:
service
-url
:
defaultZone
: http
://localhost
:8888/eureka
4.修改启动类,加入配置中心的客户端注解
@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
public class BookSpringBootApplication {
public static void main(String
[] args
) {
SpringApplication
.run(BookSpringBootApplication
.class);
}
}
2.7 改造user
1 添加springcloudconfig配置中心的客户端的依赖
<!-- 添加springcloudconfig配置中心的客户端的依赖
-->
<dependency
>
<groupId
>org
.springframework
.cloud
</groupId
>
<artifactId
>spring
-cloud
-starter
-config
</artifactId
>
</dependency
>
2 优化application.yml
server
:
port
: 8083
spring
:
application
:
name
: user
feign
:
hystrix
:
enabled
: true #开启降级
3.新建bootstrap.yml,名字不可变
spring
:
cloud
:
config
:
name
: neo
-config
profile
: dev
discovery
:
enabled
: true
service
-id
: config
eureka
:
client
:
service
-url
:
defaultZone
: http
://localhost
:8888/eureka
4.修改启动类,加入配置中心的客户端注解
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrix
public class UserSpringBootApplication {
public static void main(String
[] args
) {
SpringApplication
.run(UserSpringBootApplication
.class);
}
}
完成
2.8 测试正常