2024-5-10-从0到1手写配置中心Config之Spring Value热更新

  1. 定义SpringValueProcessor处理类
  • 实现BeanPostProcessor后置处理器接口,扫描所有的Spring value,保存起来。
  • 实现ApplicationListener接口,在配置变更时,更新所有的spring value

图片

  1. 实现BeanPostProcessor后置处理器接口

实现postProcessBeforeInitialization()方法。通过工具类找到所有带有@Value的bean,遍历每个字段。通过helper工具类抽取注解上面的表达式,构建SpringValue对象并保存起来。

图片

使用SpringValue对象封装@Value注解信息。

图片

一个SpringValue对象如下:

图片

  1. 实现onApplicationEvent方法
  • 事件变更时,自动实现该方法;
  • 从前面保存的holder中获取SpringValues;
  • 遍历集合,解析获取属性值;
  • 通过反射重新赋值。

图片

  1. 测试
  • 启动 config-server
  • 启动config-client

初始值如下:都是dev100

图片

更新配置中心的值

图片

观察config-demo的日志

图片

再次获取值,就已经动态发生了变化。

图片

源码:https://github.com/midnight2104/midnight-config/tree/v4

  • 9
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是在 Mac M1 上使用 Keras 框架实现 LeNet-5 模型识别数字的代码: ```python import numpy as np import tensorflow as tf from tensorflow import keras from tensorflow.keras import layers # 加载 MNIST 数据集 (x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data() # 将输入数据转换为浮点数并缩放到 [0, 1] 范围内 x_train = x_train.astype("float32") / 255.0 x_test = x_test.astype("float32") / 255.0 # 将标签转换成 one-hot 编码 y_train = keras.utils.to_categorical(y_train, 10) y_test = keras.utils.to_categorical(y_test, 10) # 将输入数据转换为 4D 张量 [样本数, 高度, 宽度, 通道数(灰度图为1)] x_train = np.expand_dims(x_train, -1) x_test = np.expand_dims(x_test, -1) # 定义 LeNet-5 模型 model = keras.Sequential( [ layers.Conv2D(6, kernel_size=(5, 5), activation="relu", input_shape=(28, 28, 1)), layers.MaxPooling2D(pool_size=(2, 2)), layers.Conv2D(16, kernel_size=(5, 5), activation="relu"), layers.MaxPooling2D(pool_size=(2, 2)), layers.Flatten(), layers.Dense(120, activation="relu"), layers.Dense(84, activation="relu"), layers.Dense(10, activation="softmax"), ] ) # 编译模型 model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"]) # 训练模型 model.fit(x_train, y_train, batch_size=128, epochs=10, validation_split=0.1) # 在测试集上评估模型 test_loss, test_acc = model.evaluate(x_test, y_test) print("Test accuracy:", test_acc) ``` 运行代码后,你应该可以看到模型在测试集上的准确率。注意,这个代码并没有使用 ChitGPT 或者 ChatGPT 进行对话,它只是一个实现 LeNet-5 模型的示例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值