✨ 配置监听
This commit is contained in:
parent
cdc03e35e2
commit
a76dd2227e
|
@ -1,241 +1,4 @@
|
||||||
# 微服务
|
# 微服务文档
|
||||||
|
|
||||||
## 环境搭建
|
|
||||||
|
|
||||||
### Docker镜像源设置
|
|
||||||
|
|
||||||
```bash
|
|
||||||
# 创建目录
|
|
||||||
sudo mkdir -p /etc/docker
|
|
||||||
# 写入配置文件
|
|
||||||
sudo tee /etc/docker/daemon.json <<-'EOF'
|
|
||||||
{
|
|
||||||
"registry-mirrors": [
|
|
||||||
"https://docker-0.unsee.tech",
|
|
||||||
"https://docker-cf.registry.cyou",
|
|
||||||
"https://docker.1panel.live"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
EOF
|
|
||||||
|
|
||||||
# 重启docker服务
|
|
||||||
sudo systemctl daemon-reload && sudo systemctl restart docker
|
|
||||||
```
|
|
||||||
|
|
||||||
使用docker-compose有的时候会因为版本不同,但是配置文件主要内容就是这些。需要注意版本问题
|
|
||||||
|
|
||||||
### 配置相关
|
|
||||||
|
|
||||||
#### MySQL配置问题
|
|
||||||
|
|
||||||
| **特性** | `**my.cnf**` | `**conf.d**` **目录** |
|
|
||||||
|----------|:--------------------|:-------------------:|
|
|
||||||
| **文件类型** | 单个文件 | 目录,包含多个 `.cnf` 文件 |
|
|
||||||
| **配置方式** | 集中式配置 | 分布式配置 |
|
|
||||||
| **优先级** | 高(覆盖 `conf.d` 中的配置) | 低(被 `my.cnf` 覆盖) |
|
|
||||||
| **适用场景** | 全局配置,核心配置 | 模块化配置,便于扩展和维护 |
|
|
||||||
|
|
||||||
#### MongoDB配置
|
|
||||||
|
|
||||||
```bash
|
|
||||||
sudo mkdir -p ~/docker/docker_data/mongo/conf
|
|
||||||
sudo mkdir -p ~/docker/docker_data/mongo/logs
|
|
||||||
sudo chmod 777 ~/docker/docker_data/mongo/logs
|
|
||||||
sudo chmod 777 ~/docker/docker_data/mongo/conf
|
|
||||||
|
|
||||||
cd ~/docker/docker_data/mongo/logs
|
|
||||||
sudo touch mongod.log
|
|
||||||
sudo chmod 777 mongod.log
|
|
||||||
|
|
||||||
cd ~/docker/docker_data/mongo/conf
|
|
||||||
sudo vim mongod.conf
|
|
||||||
|
|
||||||
cd ~
|
|
||||||
```
|
|
||||||
|
|
||||||
##### 配置文件
|
|
||||||
|
|
||||||
```bash
|
|
||||||
# 数据库文件存储位置
|
|
||||||
dbpath = /data/db
|
|
||||||
# log文件存储位置
|
|
||||||
logpath = /data/log/mongod.log
|
|
||||||
# 使用追加的方式写日志
|
|
||||||
logappend = true
|
|
||||||
# 是否以守护进程方式运行
|
|
||||||
# fork = true
|
|
||||||
# 全部ip可以访问
|
|
||||||
bind_ip = 0.0.0.0
|
|
||||||
# 端口号
|
|
||||||
port = 27017
|
|
||||||
# 是否启用认证
|
|
||||||
auth = true
|
|
||||||
# 设置oplog的大小(MB)
|
|
||||||
oplogSize=2048
|
|
||||||
```
|
|
||||||
|
|
||||||
##### 设置账户密码
|
|
||||||
|
|
||||||
```shell
|
|
||||||
#进入容器
|
|
||||||
docker exec -it mongodb /bin/bash
|
|
||||||
|
|
||||||
#进入mongodb shell
|
|
||||||
mongosh --port 27017
|
|
||||||
|
|
||||||
#切换到admin库
|
|
||||||
use admin
|
|
||||||
|
|
||||||
#创建账号/密码
|
|
||||||
db.createUser({ user: 'admin', pwd: '02120212', roles: [ { role: "root", db: "admin" } ] });
|
|
||||||
# db.createUser({ user: 'admin', pwd: '123456', roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] });
|
|
||||||
```
|
|
||||||
|
|
||||||
### docker-compose.yml
|
|
||||||
|
|
||||||
如果休要所有的微服务环境,可以直接复制下面的内容,看清楚目录是否和自己需要的一样。
|
|
||||||
|
|
||||||
| 功能 | 旧版 (docker-compose) | 新版 (docker compose) |
|
|
||||||
|--------------|-------------------------|-------------------------|
|
|
||||||
| **启动服务** | docker-compose up -d | docker compose up -d |
|
|
||||||
| **停止服务** | docker-compose down | docker compose down |
|
|
||||||
| **查看日志** | docker-compose logs -f | docker compose logs -f |
|
|
||||||
| **列出容器** | docker-compose ps | docker compose ps |
|
|
||||||
| **停止不删除容器** | docker-compose stop | docker compose stop |
|
|
||||||
| **启动已停止的容器** | docker-compose start | docker compose start |
|
|
||||||
| **重启服务** | docker-compose restart | docker compose restart |
|
|
||||||
| **构建镜像** | docker-compose build | docker compose build |
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
name: cloud-services
|
|
||||||
services:
|
|
||||||
mysql:
|
|
||||||
container_name: mysql_master
|
|
||||||
image: mysql:8.0.33
|
|
||||||
ports:
|
|
||||||
- "3306:3306"
|
|
||||||
environment:
|
|
||||||
- MYSQL_ROOT_PASSWORD=123456
|
|
||||||
- TZ=Asia/Shanghai
|
|
||||||
volumes:
|
|
||||||
# - ~/docker/docker_data/mysql/mysql_master/etc/my.cnf:/etc/my.cnf # 如果需要创建配置文件
|
|
||||||
- ~/docker/docker_data/mysql/mysql_master/etc/mysql:/etc/mysql/conf.d
|
|
||||||
- ~/docker/docker_data/mysql/mysql_master/data:/var/lib/mysql
|
|
||||||
- ~/docker/docker_data/mysql/mysql_master/backup:/backup
|
|
||||||
command:
|
|
||||||
- "--log-bin=mysql-bin"
|
|
||||||
- "--server-id=1"
|
|
||||||
- "--collation-server=utf8mb4_unicode_ci"
|
|
||||||
- "--character-set-server=utf8mb4"
|
|
||||||
- "--lower-case-table-names=1"
|
|
||||||
restart: always
|
|
||||||
privileged: true
|
|
||||||
networks:
|
|
||||||
- cloud
|
|
||||||
|
|
||||||
redis:
|
|
||||||
container_name: redis_master
|
|
||||||
image: redis:7.0.10
|
|
||||||
ports:
|
|
||||||
- "6379:6379"
|
|
||||||
volumes:
|
|
||||||
# - ~/docker/docker_data/redis_master/redis.conf:/etc/redis/redis.conf # 需要创建配置文件
|
|
||||||
- ~/docker/docker_data/redis_master:/etc/redis # 之后要配置文件可以直接在这里创建 redis.conf
|
|
||||||
- ~/docker/docker_data/redis_master/data:/data
|
|
||||||
command:
|
|
||||||
- "--appendonly yes"
|
|
||||||
- "--daemonize no"
|
|
||||||
- "--requirepass 123456"
|
|
||||||
- "--tcp-keepalive 300"
|
|
||||||
restart: always
|
|
||||||
networks:
|
|
||||||
- cloud
|
|
||||||
|
|
||||||
minio:
|
|
||||||
image: minio/minio
|
|
||||||
container_name: minio_master
|
|
||||||
ports:
|
|
||||||
- "9000:9000"
|
|
||||||
- "9090:9090"
|
|
||||||
volumes:
|
|
||||||
- ~/docker/docker_data/minio/data:/data
|
|
||||||
environment:
|
|
||||||
- MINIO_ROOT_USER=bunny
|
|
||||||
- MINIO_ROOT_PASSWORD=02120212
|
|
||||||
command: "server /data --console-address :9090"
|
|
||||||
restart: always
|
|
||||||
networks:
|
|
||||||
- cloud
|
|
||||||
|
|
||||||
mongodb:
|
|
||||||
image: mongo:latest
|
|
||||||
container_name: mongodb
|
|
||||||
restart: always
|
|
||||||
privileged: true
|
|
||||||
ports:
|
|
||||||
- "27017:27017"
|
|
||||||
volumes:
|
|
||||||
- ~/docker/docker_data/mongo/data:/data/db
|
|
||||||
- ~/docker/docker_data/mongo/conf:/data/configdb
|
|
||||||
- ~/docker/docker_data/mongo/logs:/data/log
|
|
||||||
command: "mongod --config /data/configdb/mongod.conf"
|
|
||||||
networks:
|
|
||||||
- cloud
|
|
||||||
|
|
||||||
rabbitmq:
|
|
||||||
image: rabbitmq:management
|
|
||||||
container_name: rabbitmq
|
|
||||||
restart: unless-stopped
|
|
||||||
ports:
|
|
||||||
- "5672:5672"
|
|
||||||
- "15672:15672"
|
|
||||||
volumes:
|
|
||||||
- ~/docker/docker_data/rabbitmq/data:/var/lib/rabbitmq
|
|
||||||
- ~/docker/docker_data/rabbitmq/conf:/etc/rabbitmq
|
|
||||||
- ~/docker/docker_data/rabbitmq/log:/var/log/rabbitmq
|
|
||||||
environment:
|
|
||||||
- RABBITMQ_DEFAULT_USER=admin
|
|
||||||
- RABBITMQ_DEFAULT_PASS=admin
|
|
||||||
- RABBITMQ_DEFAULT_VHOST=/
|
|
||||||
networks:
|
|
||||||
- cloud
|
|
||||||
|
|
||||||
nacos:
|
|
||||||
image: nacos/nacos-server:v2.4.3
|
|
||||||
container_name: nacos
|
|
||||||
ports:
|
|
||||||
- "8848:8848"
|
|
||||||
- "9848:9848"
|
|
||||||
environment:
|
|
||||||
- MODE=standalone
|
|
||||||
restart: always
|
|
||||||
networks:
|
|
||||||
- cloud
|
|
||||||
|
|
||||||
sentinel:
|
|
||||||
image: bladex/sentinel-dashboard:1.8.8
|
|
||||||
container_name: sentinel
|
|
||||||
ports:
|
|
||||||
- "8858:8858"
|
|
||||||
privileged: true
|
|
||||||
restart: always
|
|
||||||
networks:
|
|
||||||
- cloud
|
|
||||||
|
|
||||||
seata-server:
|
|
||||||
image: apache/seata-server:2.3.0.jdk21
|
|
||||||
container_name: seata-server
|
|
||||||
ports:
|
|
||||||
- "8091:8091"
|
|
||||||
restart: always
|
|
||||||
networks:
|
|
||||||
- cloud
|
|
||||||
|
|
||||||
networks: # 定义网络
|
|
||||||
cloud: # 定义名为 auth 的网络
|
|
||||||
name: cloud # 网络名称为 auth
|
|
||||||
driver: bridge # 使用 bridge 驱动(默认)
|
|
||||||
```
|
|
||||||
|
|
||||||
## 注册中心
|
## 注册中心
|
||||||
|
|
||||||
|
@ -255,24 +18,25 @@ public class DiscoveryTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void discoveryClientTest() {
|
void discoveryClientTest() {
|
||||||
|
// 使用Spring Cloud标准DiscoveryClient
|
||||||
for (String service : discoveryClient.getServices()) {
|
for (String service : discoveryClient.getServices()) {
|
||||||
System.out.println(service);
|
System.out.println("服务名称: " + service);
|
||||||
|
|
||||||
for (ServiceInstance instance : discoveryClient.getInstances(service)) {
|
for (ServiceInstance instance : discoveryClient.getInstances(service)) {
|
||||||
System.out.println("IP地址:" + instance.getHost());
|
System.out.println("实例IP地址: " + instance.getHost());
|
||||||
System.out.println("端口号" + instance.getPort());
|
System.out.println("实例端口号: " + instance.getPort());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("----------------------------------------------");
|
System.out.println("----------------------------------------------");
|
||||||
|
|
||||||
// 两个方式一样,DiscoveryClient 是 Spring自带的 NacosDiscoveryClient是 Nacos
|
// 使用Nacos专用DiscoveryClient
|
||||||
for (String service : nacosDiscoveryClient.getServices()) {
|
for (String service : nacosDiscoveryClient.getServices()) {
|
||||||
System.out.println(service);
|
System.out.println("服务名称: " + service);
|
||||||
|
|
||||||
for (ServiceInstance instance : nacosDiscoveryClient.getInstances(service)) {
|
for (ServiceInstance instance : nacosDiscoveryClient.getInstances(service)) {
|
||||||
System.out.println("IP地址:" + instance.getHost());
|
System.out.println("实例IP地址: " + instance.getHost());
|
||||||
System.out.println("端口号" + instance.getPort());
|
System.out.println("实例端口号: " + instance.getPort());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -281,11 +45,11 @@ public class DiscoveryTest {
|
||||||
|
|
||||||
### 远程调用
|
### 远程调用
|
||||||
|
|
||||||
订单模块调用远程商品模块,使用了nacos,可以使用`RestTemplate`,其中`RestTemplate`是线程安全的,只要注册一次全局都是可以使用。
|
订单模块调用远程商品模块,使用Nacos作为注册中心,可以使用`RestTemplate`进行远程调用。`RestTemplate`是线程安全的,只需注册一次即可全局使用。
|
||||||
|
|
||||||
**RestTemplate源码**
|
**RestTemplate线程安全说明**
|
||||||
|
|
||||||
继承了`InterceptingHttpAccessor`,在`InterceptingHttpAccessor`中,使用了单例模式。
|
`RestTemplate`继承了`InterceptingHttpAccessor`,其中使用了单例模式保证线程安全:
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public ClientHttpRequestFactory getRequestFactory() {
|
public ClientHttpRequestFactory getRequestFactory() {
|
||||||
|
@ -296,7 +60,6 @@ public ClientHttpRequestFactory getRequestFactory() {
|
||||||
factory = new InterceptingClientHttpRequestFactory(super.getRequestFactory(), interceptors);
|
factory = new InterceptingClientHttpRequestFactory(super.getRequestFactory(), interceptors);
|
||||||
this.interceptingRequestFactory = factory;
|
this.interceptingRequestFactory = factory;
|
||||||
}
|
}
|
||||||
|
|
||||||
return factory;
|
return factory;
|
||||||
} else {
|
} else {
|
||||||
return super.getRequestFactory();
|
return super.getRequestFactory();
|
||||||
|
@ -306,9 +69,9 @@ public ClientHttpRequestFactory getRequestFactory() {
|
||||||
|
|
||||||
#### 实现远程调用
|
#### 实现远程调用
|
||||||
|
|
||||||
##### 普通方式调用
|
##### 基础调用方式
|
||||||
|
|
||||||
注册`RestTemplate`
|
1. 注册`RestTemplate` Bean:
|
||||||
|
|
||||||
```java
|
```java
|
||||||
@Bean
|
@Bean
|
||||||
|
@ -317,26 +80,27 @@ public RestTemplate restTemplate() {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
如果我们的服务启动了多个,在下面代码中即使一个服务宕机也可以做到远程调用。
|
2. 实现远程调用:
|
||||||
|
|
||||||
```java
|
```java
|
||||||
private Product getProductFromRemote(Long productId) {
|
private Product getProductFromRemote(Long productId) {
|
||||||
// 获取商品服务所有及其的 IP+port
|
// 获取商品服务所有实例
|
||||||
List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
|
List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
|
||||||
|
if (instances.isEmpty()) {
|
||||||
|
throw new RuntimeException("未找到可用的商品服务实例");
|
||||||
|
}
|
||||||
|
|
||||||
ServiceInstance instance = instances.get(0);
|
ServiceInstance instance = instances.get(0);
|
||||||
|
|
||||||
// 远程URL
|
|
||||||
String url = "http://" + instance.getHost() + ":" + instance.getPort() + "/api/product/" + productId;
|
String url = "http://" + instance.getHost() + ":" + instance.getPort() + "/api/product/" + productId;
|
||||||
|
|
||||||
// 2. 远程发送请求
|
log.info("远程调用商品服务: {}", url);
|
||||||
log.info("远程调用:{}", url);
|
|
||||||
return restTemplate.getForObject(url, Product.class);
|
return restTemplate.getForObject(url, Product.class);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
##### 负载均衡调用
|
##### 负载均衡调用
|
||||||
|
|
||||||
注册`RestTemplate`
|
1. 注册`RestTemplate` Bean:
|
||||||
|
|
||||||
```java
|
```java
|
||||||
@Bean
|
@Bean
|
||||||
|
@ -345,39 +109,32 @@ public RestTemplate restTemplate() {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
使用负载均衡`LoadBalancerClient`,通过负载均衡算法动态调用远程服务。
|
2. 使用`LoadBalancerClient`实现负载均衡:
|
||||||
|
|
||||||
```java
|
```java
|
||||||
/**
|
/**
|
||||||
* 远程调用商品模块 --- 负载均衡
|
* 使用负载均衡调用远程商品服务
|
||||||
*
|
*
|
||||||
* @param productId 商品id
|
* @param productId 商品ID
|
||||||
* @return 商品对象
|
* @return 商品信息
|
||||||
*/
|
*/
|
||||||
private Product getProductFromRemoteWithLoadBalancer(Long productId) {
|
private Product getProductFromRemoteWithLoadBalancer(Long productId) {
|
||||||
// 1. 获取商品服务所有及其的 IP+port
|
|
||||||
ServiceInstance instance = loadBalancerClient.choose("service-product");
|
ServiceInstance instance = loadBalancerClient.choose("service-product");
|
||||||
|
|
||||||
// 远程URL
|
|
||||||
String url = "http://" + instance.getHost() + ":" + instance.getPort() + "/api/product/" + productId;
|
String url = "http://" + instance.getHost() + ":" + instance.getPort() + "/api/product/" + productId;
|
||||||
|
|
||||||
// 2. 远程发送请求
|
log.info("负载均衡远程调用商品服务: {}", url);
|
||||||
log.info("负载均衡远程调用:{}", url);
|
|
||||||
return restTemplate.getForObject(url, Product.class);
|
return restTemplate.getForObject(url, Product.class);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
##### 负载均衡注解调用
|
##### 注解式负载均衡调用
|
||||||
|
|
||||||
> [!TIP]
|
> [!TIP]
|
||||||
>
|
> 关于注册中心宕机的影响:
|
||||||
> 如果远程注册中心宕机是否可以调用?
|
> - 已调用过的服务:可以继续调用,因为客户端有缓存
|
||||||
>
|
> - 未调用过的服务:首次调用会失败,因为需要从注册中心获取服务列表
|
||||||
> 调用过:远程调用不在依赖注册中心,可以通过。
|
|
||||||
>
|
|
||||||
> 没调用过:第一次发起远程调用;不能通过。
|
|
||||||
|
|
||||||
在`RestTemplate`上加上`@LoadBalanced`注解使用负载均衡。
|
1. 注册带负载均衡的`RestTemplate`:
|
||||||
|
|
||||||
```java
|
```java
|
||||||
@Bean
|
@Bean
|
||||||
|
@ -387,49 +144,45 @@ public RestTemplate restTemplate() {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
在实际的调用中并不需要再显式调用,将URL替换成服务名称即可。
|
2. 直接使用服务名进行调用:
|
||||||
|
|
||||||
```java
|
```java
|
||||||
/**
|
/**
|
||||||
* 远程调用商品模块 --- 负载均衡注解调用
|
* 使用注解式负载均衡调用远程商品服务
|
||||||
*
|
*
|
||||||
* @param productId 商品id
|
* @param productId 商品ID
|
||||||
* @return 商品对象
|
* @return 商品信息
|
||||||
*/
|
*/
|
||||||
private Product getProductFromRemoteWithLoadBalancerAnnotation(Long productId) {
|
private Product getProductFromRemoteWithLoadBalancerAnnotation(Long productId) {
|
||||||
// 远程URL,实现动态替换
|
|
||||||
String url = "http://service-product/api/product/" + productId;
|
String url = "http://service-product/api/product/" + productId;
|
||||||
|
log.info("负载均衡注解调用商品服务: {}", url);
|
||||||
// 远程发送请求
|
|
||||||
log.info("负载均衡注解调用:{}", url);
|
|
||||||
return restTemplate.getForObject(url, Product.class);
|
return restTemplate.getForObject(url, Product.class);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### 远程配置读取
|
### 远程配置管理
|
||||||
|
|
||||||
#### 1、引入依赖
|
#### 基础配置
|
||||||
|
|
||||||
|
1. 添加依赖:
|
||||||
|
|
||||||
```xml
|
```xml
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.alibaba.cloud</groupId>
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
```
|
```
|
||||||
|
|
||||||
#### 2、在nacos中配置
|
2. 在Nacos中配置:
|
||||||
|
|
||||||

|
配置内容示例:
|
||||||
|
```yaml
|
||||||

|
|
||||||
|
|
||||||
```yml
|
|
||||||
order:
|
order:
|
||||||
timeout: 30min
|
timeout: 30min
|
||||||
auto-confirm: true
|
auto-confirm: true
|
||||||
```
|
```
|
||||||
|
|
||||||
#### 3、创建接口访问
|
3. 创建配置读取接口:
|
||||||
|
|
||||||
```java
|
```java
|
||||||
@RestController
|
@RestController
|
||||||
|
@ -437,8 +190,6 @@ order:
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class OrderController {
|
public class OrderController {
|
||||||
|
|
||||||
private final OrderService orderService;
|
|
||||||
|
|
||||||
@Value("${order.timeout}")
|
@Value("${order.timeout}")
|
||||||
private String timeout;
|
private String timeout;
|
||||||
|
|
||||||
|
@ -448,38 +199,12 @@ public class OrderController {
|
||||||
@Operation(summary = "读取配置")
|
@Operation(summary = "读取配置")
|
||||||
@GetMapping("config")
|
@GetMapping("config")
|
||||||
public String config() {
|
public String config() {
|
||||||
return "timeout:" + timeout + "\nautoConfirm:" + autoConfirm;
|
return "timeout: " + timeout + "\nautoConfirm: " + autoConfirm;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
#### 4、SpringBoot中配置
|
4. 应用配置:
|
||||||
|
|
||||||
> [!CAUTION]
|
|
||||||
>
|
|
||||||
> 需要注意的是,不要在`server-addr`地址中写`${nacos.server-addr}`,否则无法访问正确的地址。
|
|
||||||
|
|
||||||
> [!NOTE]
|
|
||||||
>
|
|
||||||
> 如果某个项目模块暂时不需要动态配置,但是引入了,可以在配置中加上,导入检查。
|
|
||||||
>
|
|
||||||
> ```yaml
|
|
||||||
> cloud:
|
|
||||||
> nacos:
|
|
||||||
> server-addr: 192.168.95.135:8848
|
|
||||||
> config:
|
|
||||||
> import-check:
|
|
||||||
> enabled: false
|
|
||||||
> ```
|
|
||||||
>
|
|
||||||
> 如果某个配置是可选也可以在前面加上`ptional:`。
|
|
||||||
>
|
|
||||||
> ```yaml
|
|
||||||
> spring:
|
|
||||||
> config:
|
|
||||||
> import:
|
|
||||||
> - optional:nacos:service-order.yml
|
|
||||||
> ```
|
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
server:
|
server:
|
||||||
|
@ -500,9 +225,14 @@ spring:
|
||||||
enabled: false
|
enabled: false
|
||||||
```
|
```
|
||||||
|
|
||||||
#### 5、动态刷新读取
|
> [!CAUTION]
|
||||||
|
> 注意事项:
|
||||||
|
> - 不要在`server-addr`中使用变量引用如`${nacos.server-addr}`,这可能导致连接失败
|
||||||
|
> - 对于不需要动态配置的模块,可以禁用配置检查
|
||||||
|
|
||||||
如果需要动态刷新需要在控制器或者是启动类上加上`@RefreshScope`
|
#### 动态刷新配置
|
||||||
|
|
||||||
|
1. 添加`@RefreshScope`注解:
|
||||||
|
|
||||||
```java
|
```java
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
|
@ -515,29 +245,26 @@ public class ProductServiceApplication {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
#### 6、批量配置刷新读取
|
#### 批量配置绑定
|
||||||
|
|
||||||
> [!TIP]
|
> [!TIP]
|
||||||
>
|
> 使用`@ConfigurationProperties`批量绑定配置:
|
||||||
> 配置批量绑定无需 @RefreshScope 实现动态刷新。
|
> - 无需`@RefreshScope`即可实现动态刷新
|
||||||
>
|
> - 支持中划线命名自动转为驼峰命名
|
||||||
> 中划线写法会自动映射为小驼峰。
|
|
||||||
|
1. 创建配置类:
|
||||||
|
|
||||||
```java
|
```java
|
||||||
@Configuration
|
@Configuration
|
||||||
// 配置批量绑定无需 @RefreshScope 实现动态刷新
|
|
||||||
@ConfigurationProperties(prefix = "order")
|
@ConfigurationProperties(prefix = "order")
|
||||||
public class OrderProperties {
|
public class OrderProperties {
|
||||||
|
|
||||||
private String timeout;
|
private String timeout;
|
||||||
|
|
||||||
// 中划线写法会自动映射为小驼峰
|
|
||||||
private String autoConfirm;
|
private String autoConfirm;
|
||||||
|
// getters and setters
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
测试读取内容
|
2. 使用配置:
|
||||||
|
|
||||||
```java
|
```java
|
||||||
@RestController
|
@RestController
|
||||||
|
@ -545,15 +272,92 @@ public class OrderProperties {
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class OrderController {
|
public class OrderController {
|
||||||
|
|
||||||
private final OrderService orderService;
|
|
||||||
private final OrderProperties orderProperties;
|
private final OrderProperties orderProperties;
|
||||||
|
|
||||||
@Operation(summary = "读取配置")
|
@Operation(summary = "读取配置")
|
||||||
@GetMapping("config")
|
@GetMapping("config")
|
||||||
public String config() {
|
public String config() {
|
||||||
String timeout = orderProperties.getTimeout();
|
return "timeout: " + orderProperties.getTimeout() +
|
||||||
String autoConfirm = orderProperties.getAutoConfirm();
|
"\nautoConfirm: " + orderProperties.getAutoConfirm();
|
||||||
return "timeout:" + timeout + "\nautoConfirm:" + autoConfirm;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### 高级配置管理
|
||||||
|
|
||||||
|
1. 多环境配置:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
spring:
|
||||||
|
profiles:
|
||||||
|
active: @profileActive@
|
||||||
|
config:
|
||||||
|
import:
|
||||||
|
- nacos:service-order-${spring.profiles.active}.yml
|
||||||
|
```
|
||||||
|
|
||||||
|
2. 共享配置:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
spring:
|
||||||
|
config:
|
||||||
|
import:
|
||||||
|
- nacos:common-config.yml
|
||||||
|
- nacos:service-order.yml
|
||||||
|
```
|
||||||
|
|
||||||
|
3. 命名空间和分组:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
cloud:
|
||||||
|
nacos:
|
||||||
|
config:
|
||||||
|
namespace: dev
|
||||||
|
group: DEFAULT_GROUP
|
||||||
|
```
|
||||||
|
|
||||||
|
4. 配置优先级:
|
||||||
|
- 应用名-profile.yml (最高优先级)
|
||||||
|
- 应用名.yml
|
||||||
|
- 扩展配置
|
||||||
|
- 共享配置 (最低优先级)
|
||||||
|
|
||||||
|
### 配置监听
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
#### 实现配置监听
|
||||||
|
|
||||||
|
1. 项目启动时注册监听器:
|
||||||
|
|
||||||
|
```java
|
||||||
|
@SpringBootApplication
|
||||||
|
@EnableDiscoveryClient
|
||||||
|
public class OrderServiceApplication {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(OrderServiceApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ApplicationRunner runner(NacosConfigManager nacosConfigManager) {
|
||||||
|
return args -> {
|
||||||
|
ConfigService configService = nacosConfigManager.getConfigService();
|
||||||
|
configService.addListener("service-order.yml", "DEFAULT_GROUP", new Listener() {
|
||||||
|
@Override
|
||||||
|
public Executor getExecutor() {
|
||||||
|
return Executors.newFixedThreadPool(10);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void receiveConfigInfo(String configInfo) {
|
||||||
|
System.out.println("配置变更内容: " + configInfo);
|
||||||
|
// 实现配置变更后的处理逻辑
|
||||||
|
System.out.println("发送配置变更通知邮件...");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
System.out.println("订单服务启动完成,配置监听已注册");
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
Binary file not shown.
After Width: | Height: | Size: 56 KiB |
|
@ -1,9 +1,17 @@
|
||||||
package cn.bunny.service;
|
package cn.bunny.service;
|
||||||
|
|
||||||
|
import com.alibaba.cloud.nacos.NacosConfigManager;
|
||||||
|
import com.alibaba.nacos.api.config.ConfigService;
|
||||||
|
import com.alibaba.nacos.api.config.listener.Listener;
|
||||||
|
import org.springframework.boot.ApplicationRunner;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
@EnableDiscoveryClient
|
@EnableDiscoveryClient
|
||||||
|
@ -12,4 +20,25 @@ public class OrderServiceApplication {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(OrderServiceApplication.class, args);
|
SpringApplication.run(OrderServiceApplication.class, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ApplicationRunner runner(NacosConfigManager nacosConfigManager) {
|
||||||
|
return args -> {
|
||||||
|
ConfigService configService = nacosConfigManager.getConfigService();
|
||||||
|
configService.addListener("service-order.yml", "DEFAULT_GROUP", new Listener() {
|
||||||
|
@Override
|
||||||
|
public Executor getExecutor() {
|
||||||
|
return Executors.newFixedThreadPool(10);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void receiveConfigInfo(String configInfo) {
|
||||||
|
System.out.println("监听配置信息:" + configInfo);
|
||||||
|
System.out.println("发送邮件...");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
System.out.println("Order Service Is Running...");
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,237 @@
|
||||||
|
## 环境搭建
|
||||||
|
|
||||||
|
### Docker镜像源设置
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 创建目录
|
||||||
|
sudo mkdir -p /etc/docker
|
||||||
|
# 写入配置文件
|
||||||
|
sudo tee /etc/docker/daemon.json <<-'EOF'
|
||||||
|
{
|
||||||
|
"registry-mirrors": [
|
||||||
|
"https://docker-0.unsee.tech",
|
||||||
|
"https://docker-cf.registry.cyou",
|
||||||
|
"https://docker.1panel.live"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
EOF
|
||||||
|
|
||||||
|
# 重启docker服务
|
||||||
|
sudo systemctl daemon-reload && sudo systemctl restart docker
|
||||||
|
```
|
||||||
|
|
||||||
|
使用docker-compose有的时候会因为版本不同,但是配置文件主要内容就是这些。需要注意版本问题
|
||||||
|
|
||||||
|
### 配置相关
|
||||||
|
|
||||||
|
#### MySQL配置问题
|
||||||
|
|
||||||
|
| **特性** | `**my.cnf**` | `**conf.d**` **目录** |
|
||||||
|
| ------------ | :--------------------------- | :------------------------: |
|
||||||
|
| **文件类型** | 单个文件 | 目录,包含多个 `.cnf` 文件 |
|
||||||
|
| **配置方式** | 集中式配置 | 分布式配置 |
|
||||||
|
| **优先级** | 高(覆盖 `conf.d` 中的配置) | 低(被 `my.cnf` 覆盖) |
|
||||||
|
| **适用场景** | 全局配置,核心配置 | 模块化配置,便于扩展和维护 |
|
||||||
|
|
||||||
|
#### MongoDB配置
|
||||||
|
|
||||||
|
```bash
|
||||||
|
sudo mkdir -p ~/docker/docker_data/mongo/conf
|
||||||
|
sudo mkdir -p ~/docker/docker_data/mongo/logs
|
||||||
|
sudo chmod 777 ~/docker/docker_data/mongo/logs
|
||||||
|
sudo chmod 777 ~/docker/docker_data/mongo/conf
|
||||||
|
|
||||||
|
cd ~/docker/docker_data/mongo/logs
|
||||||
|
sudo touch mongod.log
|
||||||
|
sudo chmod 755 mongod.log
|
||||||
|
|
||||||
|
cd ~/docker/docker_data/mongo/conf
|
||||||
|
sudo vim mongod.conf
|
||||||
|
|
||||||
|
cd ~
|
||||||
|
```
|
||||||
|
|
||||||
|
##### 配置文件
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 数据库文件存储位置
|
||||||
|
dbpath = /data/db
|
||||||
|
# log文件存储位置
|
||||||
|
logpath = /data/log/mongod.log
|
||||||
|
# 使用追加的方式写日志
|
||||||
|
logappend = true
|
||||||
|
# 是否以守护进程方式运行
|
||||||
|
# fork = true
|
||||||
|
# 全部ip可以访问
|
||||||
|
bind_ip = 0.0.0.0
|
||||||
|
# 端口号
|
||||||
|
port = 27017
|
||||||
|
# 是否启用认证
|
||||||
|
auth = true
|
||||||
|
# 设置oplog的大小(MB)
|
||||||
|
oplogSize=2048
|
||||||
|
```
|
||||||
|
|
||||||
|
##### 设置账户密码
|
||||||
|
|
||||||
|
```shell
|
||||||
|
#进入容器
|
||||||
|
docker exec -it mongodb /bin/bash
|
||||||
|
|
||||||
|
#进入mongodb shell
|
||||||
|
mongosh --port 27017
|
||||||
|
|
||||||
|
#切换到admin库
|
||||||
|
use admin
|
||||||
|
|
||||||
|
#创建账号/密码
|
||||||
|
db.createUser({ user: 'admin', pwd: '02120212', roles: [ { role: "root", db: "admin" } ] });
|
||||||
|
# db.createUser({ user: 'admin', pwd: '123456', roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] });
|
||||||
|
```
|
||||||
|
|
||||||
|
### docker-compose.yml
|
||||||
|
|
||||||
|
如果休要所有的微服务环境,可以直接复制下面的内容,看清楚目录是否和自己需要的一样。
|
||||||
|
|
||||||
|
| 功能 | 旧版 (docker-compose) | 新版 (docker compose) |
|
||||||
|
| -------------------- | ----------------------- | ----------------------- |
|
||||||
|
| **启动服务** | docker-compose up -d | docker compose up -d |
|
||||||
|
| **停止服务** | docker-compose down | docker compose down |
|
||||||
|
| **查看日志** | docker-compose logs -f | docker compose logs -f |
|
||||||
|
| **列出容器** | docker-compose ps | docker compose ps |
|
||||||
|
| **停止不删除容器** | docker-compose stop | docker compose stop |
|
||||||
|
| **启动已停止的容器** | docker-compose start | docker compose start |
|
||||||
|
| **重启服务** | docker-compose restart | docker compose restart |
|
||||||
|
| **构建镜像** | docker-compose build | docker compose build |
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
name: cloud-services
|
||||||
|
services:
|
||||||
|
mysql:
|
||||||
|
container_name: mysql_master
|
||||||
|
image: mysql:8.0.33
|
||||||
|
ports:
|
||||||
|
- "3306:3306"
|
||||||
|
environment:
|
||||||
|
- MYSQL_ROOT_PASSWORD=123456
|
||||||
|
- TZ=Asia/Shanghai
|
||||||
|
volumes:
|
||||||
|
# - ~/docker/docker_data/mysql/mysql_master/etc/my.cnf:/etc/my.cnf # 如果需要创建配置文件
|
||||||
|
- ~/docker/docker_data/mysql/mysql_master/etc/mysql:/etc/mysql/conf.d
|
||||||
|
- ~/docker/docker_data/mysql/mysql_master/data:/var/lib/mysql
|
||||||
|
- ~/docker/docker_data/mysql/mysql_master/backup:/backup
|
||||||
|
command:
|
||||||
|
- "--log-bin=mysql-bin"
|
||||||
|
- "--server-id=1"
|
||||||
|
- "--collation-server=utf8mb4_unicode_ci"
|
||||||
|
- "--character-set-server=utf8mb4"
|
||||||
|
- "--lower-case-table-names=1"
|
||||||
|
restart: always
|
||||||
|
privileged: true
|
||||||
|
networks:
|
||||||
|
- cloud
|
||||||
|
|
||||||
|
redis:
|
||||||
|
container_name: redis_master
|
||||||
|
image: redis:7.0.10
|
||||||
|
ports:
|
||||||
|
- "6379:6379"
|
||||||
|
volumes:
|
||||||
|
# - ~/docker/docker_data/redis_master/redis.conf:/etc/redis/redis.conf # 需要创建配置文件
|
||||||
|
- ~/docker/docker_data/redis_master:/etc/redis # 之后要配置文件可以直接在这里创建 redis.conf
|
||||||
|
- ~/docker/docker_data/redis_master/data:/data
|
||||||
|
command:
|
||||||
|
- "--appendonly yes"
|
||||||
|
- "--daemonize no"
|
||||||
|
- "--requirepass 123456"
|
||||||
|
- "--tcp-keepalive 300"
|
||||||
|
restart: always
|
||||||
|
networks:
|
||||||
|
- cloud
|
||||||
|
|
||||||
|
minio:
|
||||||
|
image: minio/minio
|
||||||
|
container_name: minio_master
|
||||||
|
ports:
|
||||||
|
- "9000:9000"
|
||||||
|
- "9090:9090"
|
||||||
|
volumes:
|
||||||
|
- ~/docker/docker_data/minio/data:/data
|
||||||
|
environment:
|
||||||
|
- MINIO_ROOT_USER=bunny
|
||||||
|
- MINIO_ROOT_PASSWORD=02120212
|
||||||
|
command: "server /data --console-address :9090"
|
||||||
|
restart: always
|
||||||
|
networks:
|
||||||
|
- cloud
|
||||||
|
|
||||||
|
mongodb:
|
||||||
|
image: mongo:latest
|
||||||
|
container_name: mongodb
|
||||||
|
restart: always
|
||||||
|
privileged: true
|
||||||
|
ports:
|
||||||
|
- "27017:27017"
|
||||||
|
volumes:
|
||||||
|
- ~/docker/docker_data/mongo/data:/data/db
|
||||||
|
- ~/docker/docker_data/mongo/conf:/data/configdb
|
||||||
|
- ~/docker/docker_data/mongo/logs:/data/log
|
||||||
|
command: "mongod --config /data/configdb/mongod.conf"
|
||||||
|
networks:
|
||||||
|
- cloud
|
||||||
|
|
||||||
|
rabbitmq:
|
||||||
|
image: rabbitmq:management
|
||||||
|
container_name: rabbitmq
|
||||||
|
restart: unless-stopped
|
||||||
|
ports:
|
||||||
|
- "5672:5672"
|
||||||
|
- "15672:15672"
|
||||||
|
volumes:
|
||||||
|
- ~/docker/docker_data/rabbitmq/data:/var/lib/rabbitmq
|
||||||
|
- ~/docker/docker_data/rabbitmq/conf:/etc/rabbitmq
|
||||||
|
- ~/docker/docker_data/rabbitmq/log:/var/log/rabbitmq
|
||||||
|
environment:
|
||||||
|
- RABBITMQ_DEFAULT_USER=admin
|
||||||
|
- RABBITMQ_DEFAULT_PASS=admin
|
||||||
|
- RABBITMQ_DEFAULT_VHOST=/
|
||||||
|
networks:
|
||||||
|
- cloud
|
||||||
|
|
||||||
|
nacos:
|
||||||
|
image: nacos/nacos-server:v2.4.3
|
||||||
|
container_name: nacos
|
||||||
|
ports:
|
||||||
|
- "8848:8848"
|
||||||
|
- "9848:9848"
|
||||||
|
environment:
|
||||||
|
- MODE=standalone
|
||||||
|
restart: always
|
||||||
|
networks:
|
||||||
|
- cloud
|
||||||
|
|
||||||
|
sentinel:
|
||||||
|
image: bladex/sentinel-dashboard:1.8.8
|
||||||
|
container_name: sentinel
|
||||||
|
ports:
|
||||||
|
- "8858:8858"
|
||||||
|
privileged: true
|
||||||
|
restart: always
|
||||||
|
networks:
|
||||||
|
- cloud
|
||||||
|
|
||||||
|
seata-server:
|
||||||
|
image: apache/seata-server:2.3.0.jdk21
|
||||||
|
container_name: seata-server
|
||||||
|
ports:
|
||||||
|
- "8091:8091"
|
||||||
|
restart: always
|
||||||
|
networks:
|
||||||
|
- cloud
|
||||||
|
|
||||||
|
networks: # 定义网络
|
||||||
|
cloud: # 定义名为 auth 的网络
|
||||||
|
name: cloud # 网络名称为 auth
|
||||||
|
driver: bridge # 使用 bridge 驱动(默认)
|
||||||
|
```
|
||||||
|
|
Loading…
Reference in New Issue