观察者+配置中心动态策略路由Demo

在最近一期视频什么是大型项目里举了一个🌰,说是高级开发重构了业务通知这块逻辑,有好几位粉丝对具体的代码实现比较好奇,我临时自己手写了一份Demo,可以供大家参考一下,跟教科书的观察者和策略模式有些变化,能解决问题是重点,具体就不讲解了,都在注释里。

执行的顺序怎么去定义和实现?

单个通知失败了怎么办?

选不同的组件当配置中心有何区别?

增加一种新的发送渠道怎么办?

如果大批量短时间发送,有哪些地方可以优化? 

import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: @极海Channel
 * @Date:2021/12/2
 * @Description:
 */
@SpringBootApplication
public class ObserverDemo implements CommandLineRunner {

    /** 这里可以注册多个观察者,这个demo只注册了一个消息通知 */
    @Autowired
    List<Observer> observerList;

    public static void main(String[] args) {
        new SpringApplication(ObserverDemo.class).run(args);
    }

    /** 容器启动即执行 */
    @Override
    public void run(String... args) {
        sendMsg("bizType1", "业务1的内容");
        sendMsg("bizType2", "业务2的内容");
    }
    /** client调用,指明业务和需要发送的内容 */
    private void sendMsg(String bizType, String content) {
        observerList.forEach(observer -> observer.notify(bizType, content));
    }
}

/**
 * 观察者,JDK自带有观察者,定义接口不指定实现
 */
interface Observer {

    void notify(String bizType, String content);
}

/**
 * 发送消息的观察者, 也实现了发送策略的组合和选择
 */
@Component
class SendMsgObserver implements Observer {

    /** 所有发送实现,均可见*/
    @Autowired
    private List<SendMsgService> sendMsgServices;

    @Override
    public void notify(String bizType, String content) {
        // 获取业务类型 -> 策略组合
        List<String> strategy = ConfigCenter.getStrategy(bizType);
        sendMsgServices.forEach(sendMsgService -> {
            // 配置的策略在内,发送
            if (strategy.contains(sendMsgService.getClass().getDeclaredAnnotation(Service.class).value())) {
                sendMsgService.sendMsg(content);
            }
        });
    }
}

/**
 * 模拟配置中心, nacos/zookeeper/redis/MySQL等均可。
 */
class ConfigCenter {
    private static Map<String, String> sendMsgConfig = new HashMap<>();
    static {
        sendMsgConfig.put("bizType1", "email,sms");
        sendMsgConfig.put("bizType2", "sms");
    }
    // 根据业务获取配置,新业务,配置key-value即可
    public static List<String> getStrategy(String bizType) {
        return Lists.newArrayList(sendMsgConfig.get(bizType).split(","));
    }
}

/**
 * 发送消息的能力抽象,可以理解为策略接口
 */
interface SendMsgService {
    /** 发送内容*/
    void sendMsg(String content);
}

/**
 * 两种发送方式实现
 */
@Service("sms")
class SmsSendService implements SendMsgService {
    @Override
    public void sendMsg(String content) {
        System.out.println("短信发送:" + content);
    }
}
@Service("email")
class EmailSendService implements SendMsgService {
    @Override
    public void sendMsg(String content) {
        System.out.println("邮件发送:" + content);
    }
} 作者:极海Channel https://www.bilibili.com/read/cv14249669?spm_id_from=333.999.0.0 出处:bilibili

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Max恒

为了开源加油 ! !

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值