config server 端 yml配置
server:
port: 3344
spring:
application:
name: microservicecloud-config
cloud:
config:
server:
git:
uri: https://gitee.com/a188****/springcloud-config.git #GitHub上面的git仓库名字
search-paths:
- springcloud-config
label: master
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
eureka:
client:
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka
management:
endpoints:
web:
exposure:
include: "bus-refresh"
pom依赖
<!-- 添加消息总线RabbitMQ支持 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
config client端 采用的配置文件是bootstrap.yml
server:
port: 3355
spring:
application:
name: config-client
cloud:
config:
label: master
name: config
profile: dev
uri: http://localhost:3344
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
eureka:
client:
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka
# 暴露监控端点
management:
endpoints:
web:
exposure:
include: "*"
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.springcloud.entities #所有Entity别名类所在包
关键配置代码:刷新数据源配置
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import javax.sql.DataSource;
@Configuration
public class DbConfig {
@Bean
@RefreshScope //refresh
@Primary
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
}
测试不同的数据库:
变更之前:码云上面的配置
测试结果:
{
"code": 200,
"message": "查找成功,serverPort:3355",
"data": {
"id": 3,
"serial": "cloud32018"
}
}
变更之后:
config:
info: "master branch,springcloud-config/config-dev.yml version=11112"
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.Driver
jdbc-url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding-utr-8&useSSL=false
username: root
password: root
发送curl命令:
curl -X POST "http://localhost:3344/actuator/bus-refresh"
测试结果:
"code": 200,
"message": "查找成功,serverPort:3355",
"data": {
"id": 3,
"serial": "cloud3"
}
}