pom,服务端
<dependencies>
<!--config server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--eureka client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
yml 这里真的是磕磕碰碰,刚开始用的是ssh,后来各种报错,最后用的https
这里用的是 国产gitee。并且已经有个仓库名字springcloud-config
server:
port: 3344
spring:
application:
name: cloud-config-center
cloud:
config:
server:
git:
uri: https://gitee.com/caomengwei1/springcloud-config.git
search-paths:
- springcloud-config
username: git账号
password: 密码
label: master #分支
eureka:
client:
register-with-eureka: true
fetchRegistry: true
service-url:
defaultZone: http://eureka7002.com:7002/eureka,http://eureka7001.com:7001/eureka
instance:
instance-id: config-3344 #修改主机name
prefer-ip-address: true #显示ip端口
主启动
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class ConfigMain3344 {
public static void main(String[] args) {
SpringApplication.run(ConfigMain3344.class,args);
}
}
gitee springcloud-config仓库,里面有个 config-text.yml文件 里的内容乱打的字母
这已经是踩完坑的代码了,当时报了很多个错,忘了截图了,就记得有个 验证失败的错误,后来加了账号密码就好了。好了下面测试图,因为在win 已经做过映射,直接config-3344.com,如果没加,访问http://localhost:3344/master/config-text.yml
服务端自测通过
client端 3355
pom
dependencies>
<!--client端 config-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--eureka client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
bootstrap.properties配置,因为发生了很多事,后来解决了本身是yml的。又改成 properties。就没改回去
这里随便说一下,bootstrap 和 application,bootstrap优先级要高于application
spring cloud config要做的就是 共有的配置提取。从git获取,所以先配置必须得是共有的,所以用了系统级别的 bootstrap配置。
server.port=3355
spring.application.name=config-client
spring.cloud.config.label=master
#spring.cloud.config.name=config name 和 profile 这两个是会组合到一起 最终形成,config-text,自动会加上 “-”
#spring.cloud.config.profile=text
spring.cloud.config.name=config
spring.cloud.config.profile=text
# 通过服务端 334去拿数据
spring.cloud.config.uri=http://localhost:3344
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
eureka.client.service-url.defaultZone=http://eureka7002.com:7002/eureka,http://eureka7001.com:7001/eureka
management.endpoints.web.exposure.include=*
主启动
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3355 {
public static void main(String[] args) {
SpringApplication.run(ConfigClientMain3355.class);
}
}
controller
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConfigClientController {
@Value("${config.info}") //这里的意思 下面会给截图,
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo(){
return configInfo;
}
}
@Value("${config.info}") 在springcloud-config仓库里有个键值对的方式,config info 简单一句话就是 读取 config下的info里面的内容
测试成功图,成功拿到数据,
但是这里是有问题的,如果仓库的数据更改,服务端3344是可以拿到最新的数据,但是3355需要重启,或者需要重新发一个post 请求刷新3355,才能拿到最新的数据。
前提已经安装 rabbitmq。并且通过,
config端口 3344 yml修改 添加如下
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
management:
endpoints:
web:
exposure:
include: 'bus-refresh'
client端 3355yml添加如下
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
controller
package com.atguigu.springcloud.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RefreshScope //刷新
public class ConfigClientController {
@Value("${config.info}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo(){
return configInfo;
}
}
测试启动 3344 3355 下面分别是 3344 3355通过截图
简单说一下ribbmq 解决的问题,只需要发送一个post请求,刷新3344,所有client都会被刷新,这里就测试一个 client 3355
cmd命令
curl -X POST "http://localhost:3344/actuator/bus-refresh"
测试成功