观察者+配置中心动态策略路由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
    评论
在 Vue 3 中使用 TypeScript 并结合 Vite 构建工具来配置路由,可以按照以下步骤进行操作: 1. 首先,确保你已经创建了一个 Vue 3 项目,并且已经安装了相关依赖。可以通过以下命令创建一个新的 Vue 3 项目: ``` $ npm init vite@latest my-vue3-app --template vue-ts $ cd my-vue3-app $ npm install ``` 2. 安装 Vue Router 和相关 TypeScript 类型定义: ``` $ npm install vue-router@next $ npm install @types/vue-router ``` 3. 在 `src` 目录下创建一个新的文件夹,比如 `router`,并在其中创建一个名为 `index.ts` 的文件,用于配置路由。 4. 在 `index.ts` 文件中,首先引入 Vue Router 和相关组件: ```typescript import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'; import Home from '../views/Home.vue'; import About from '../views/About.vue'; ``` 5. 声明一个 `routes` 数组,用于定义路由配置: ```typescript const routes: Array<RouteRecordRaw> = [ { path: '/', name: 'Home', component: Home, }, { path: '/about', name: 'About', component: About, }, ]; ``` 在这个例子中,我们定义了两个路由:`'/'` 对应到 `Home` 组件,`'/about'` 对应到 `About` 组件。 6. 创建一个路由实例,并且将路由配置传递给它: ```typescript const router = createRouter({ history: createWebHistory(), routes, }); ``` 这里使用了 `createWebHistory()` 来创建一个基于 HTML5 History API 的路由模式。 7. 最后,将路由实例导出,以便在 Vue 应用中使用: ```typescript

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Max恒

为了开源加油 ! !

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

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

打赏作者

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

抵扣说明:

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

余额充值