目录
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/*Mapper.xml
#eureka的注册信息
eureka.client.service-url.defaultZone=http://localhost:8888/eureka
#标注当前工程注册时,使用ip地址的方式
eureka.instance.prefer-ip-address=true
eureka.instance.hostname=${spring.cloud.client.ip-address}
eureka.instance.instance-id=http://${spring.cloud.client.ip-address}:${server.port}
#ribbon配置
ribbon.ReadTimeout=5000
#ribbon的连接超时时间
ribbon.ConnectTimeout=5000
feign.hystrix.enabled=true
2.5 启动类
@SpringBootApplication
//标注当前工程是配置中心的服务端
@EnableConfigServer
//标注当前工程是eureka的客户端
@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
//标注当前工程是eureka的客户端,同时也是配置中心的客户端
@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
//标注当前工程是eureka的客户端,同时也是配置中心的客户端
@EnableDiscoveryClient
//标注当前工程使用fegin来进行远程调用 feign 结合了ribbon 和resttemplate =>httpclient
@EnableFeignClients
//标注当前工程开启服务的降级处理
@EnableHystrix
public class UserSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(UserSpringBootApplication.class);
}
}
完成
2.8 测试正常