专注Java教育14年 全国咨询/投诉热线:400-8080-105
动力节点LOGO图
始于2009,口口相传的Java黄埔军校
首页 hot资讯 多租户电子商城系统SpringCloud原理总结

多租户电子商城系统SpringCloud原理总结

更新时间:2021-09-29 08:31:33 来源:动力节点 浏览1167次

多租户电子商城系统Springcloud Bus原理总结

1.spring cloud bus

Sprcloud是根据spring的配置集成了一系列微服务框架。Sprcloud总线是微服务框架之一,用于实现微服务之间的通信。

JAVA分布式微服务云构建B2B2C电子商务平台源码 Spring Cloud大型企业

Sprcloud总线集成了java的事件处理机制和消息中间件消息收发,主要由sender、receiver和event组成。针对不同的业务需求,可以设置不同的事件,发送方发送事件,接收方接收相应的事件并进行相应的处理。

2.spring cloud bus实战

网上有很多关于spring cloud bus的demo,这里就不赘述了。

3.原理

Spring cloud bus 集成了java事件处理机制和消息中间件,所以下面两个方面来说明spring cloud bus的原理。

如图,给出如下解释:

(1)完整的过程:发送者(端点)构建体事件,发布ES它上下文的上下文(弹簧云总线具有父上下文,自举),则将该事件发送至信道(JSON串消息),则接收机获取消息从信道,转换的消息到事件(转换过程没有深入研究),然后公布事件事件事件。ish到context context,最后Listener接收到事件并调用service进行处理。在整个过程中,只有发送/接收端从上下文上下文上下文中获取事件和发送事件,需要在代码中写清楚. 其他部分由框架封装。

(2) 首先,我们概括地描述该过程。关于封装的一些过程,我们基本可以在BusAutoConfiguration.class中找到。下面的代码在这个类中。

  @EventListener(classes = RemoteApplicationEvent.class)
    public void acceptLocal(RemoteApplicationEvent event) {
        if (this.serviceMatcher.isFromSelf(event)
                && !(event instanceof AckRemoteApplicationEvent)) {
            this.cloudBusOutboundChannel.send(MessageBuilder.withPayload(event).build());
        }
    }

这封装了java事件处理机制。当接收到 RemoteApplication Event 时,如果该事件是从服务发送的,并且不是 ack 事件,则将该事件发送到通道。

  @StreamListener(SpringCloudBusClient.INPUT)
    public void acceptRemote(RemoteApplicationEvent event) {
        if (event instanceof AckRemoteApplicationEvent) {
            if (this.bus.getTrace().isEnabled() && !this.serviceMatcher.isFromSelf(event)
                    && this.applicationEventPublisher != null) {
                this.applicationEventPublisher.publishEvent(event);
            }
            // If it's an ACK we are finished processing at this point
            return;
        }
        if (this.serviceMatcher.isForSelf(event)
                && this.applicationEventPublisher != null) {
            if (!this.serviceMatcher.isFromSelf(event)) {
                this.applicationEventPublisher.publishEvent(event);
            }
            if (this.bus.getAck().isEnabled()) {
                AckRemoteApplicationEvent ack = new AckRemoteApplicationEvent(this,
                        this.serviceMatcher.getServiceId(),
                        this.bus.getAck().getDestinationService(),
                        event.getDestinationService(), event.getId(), event.getClass());
                this.cloudBusOutboundChannel
                        .send(MessageBuilder.withPayload(ack).build());
                this.applicationEventPublisher.publishEvent(ack);
            }
        }
        if (this.bus.getTrace().isEnabled() && this.applicationEventPublisher != null) {
            // We are set to register sent events so publish it for local consumption,
            // irrespective of the origin
            this.applicationEventPublisher.publishEvent(new SentApplicationEvent(this,
                    event.getOriginService(), event.getDestinationService(),
                    event.getId(), event.getClass()));
        }
    }

StreamListener 是一个有趣的标签,您可以查看。这个方法是从channel中提取事件处理的过程(message to event部分需要自己理解,我没有深入研究过),根据事件的类型,发送方和接收方来处理这个事件:如果是一个ack 事件,将其发送到上下文上下文;如果它是接收者而不是发送者,它会将事件发送到上下文上下文。

(3)消息中间件可以采用rabbitmq、kafka等。

以上就是关于“多租户电子商城系统SpringCloud原理总结”的介绍,如果大家对此感兴趣,不妨来关注一下动力节点的SpringCloud教程,相信对大家的学习会有一定的帮助。

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>