当前位置:网站首页>Does stream even have application advanced learning? As a programmer, you know what

Does stream even have application advanced learning? As a programmer, you know what

2022-06-25 14:16:00 Java enthusiast

Stream Advanced application

In the microservices architecture , Fine grained services are more prone to frequent distributed integration and interaction .

Service interaction pattern based on message oriented middleware , Or build an event driven architecture model , It can help businesses establish and implement the core domain event driven interaction mechanism .

Field events (Domain Event) The communication of changes the state of domain objects , For example, the order creation event 、 Inventory addition event .

A domain event can express the behavior that is taking place on a domain object . The occurrence of domain events is accompanied by modified data 、 Time stamp 、 polymerization ID And other additional information . In the microservices architecture , Domain events to publish / Subscription mode , Publish events to MQ middleware , Allow multiple different microservices to subscribe to events and consume events , This event may be an order creation event (OrderCreateEvent) Or order modification event (CustomerModifyEvent).

All the above methods follow the object-oriented method , However, these objects shuttle among producers 、 Distributed message queues and consumers , It becomes a shared class library , When many microservices need to rely on shared class libraries , A high degree of coupling . This shared distributed object implements remote invocation by marking the shared object as jar Packages are shared by different microservices , It is also a typical antipattern in distributed systems , When a domain event is modified , Every dependent microservice will be affected .

Use SCS adopt Spring Message Public message event mechanism , It can support event driven architecture , At the same time, avoid relying on sharing Domain object type . And now we have SCS Based on some meta annotations of Spring The implementation of some small features is similar to CORS Of EDA framework .

SpringCloudStream Handling events

SCS Provides @StreamListener Annotations to control how serialization is done , It acts as an input to the method and executes the method , for example :

The new event distribution feature is in @StreamListener I added Condition Property to make it possible to route messages to multiple listeners ,Condition The value of is in SPEL An expression boolean value .Condition Apply to incoming messages , Ability to calculate any message payload 、 A specific message header or a combination thereof . This provides an extremely flexible routing mechanism , There is no need to define classes for different event types . for example , We define a band String eventType Attribute Event type ,SCS Will provide out of the box functionality :

among ,Event The type is defined as follows :

Customized event annotations

though however can With send use through use Of Event class type , but yes still however Need to be want through too @StreamListener Annotations to join SEL Condition Annotation to filter , It's troublesome to use . Now we're based on @StreamListener Annotations implement custom event annotations , We directly locate our method by event type . Custom annotation @EventHandler Is defined as follows :

adopt @EventHandler The definition of , We can simplify the event listening annotation to the following form :

here , We can do this by Condition The expression is transformed into a template implementation , heavy load SCS It's about The reason is @StreamListener notes Explain Of BeanPostProcessor , this sample through too eventType You can implement the method of identifying events . Overloaded functions are as follows :

Next , We can use custom @EnableEventHandling Annotation to introduce this configuration:

I People repair Change EventHandler notes Explain , set The righteous One individual eventType Belong to sex Come on do Condition Another name for :

then , We can use custom event handling annotations , Just provide the event type , You can send subscription events from consumers :

Microservice integration architectures tend to use standardized services HTTP、 be based on REST API Integration with the architecture interaction pattern . Besides , Considering the performance, you can also use RPC How to invoke . For asynchronous interactive processes , Using message queue can realize full decoupling and heterogeneous integration between microservices .

Spring Cloud Provides Spring Cloud Stream frame , It can mask the details of the underlying communication technology , And a message based lightweight microservice integration solution is implemented .

You can also use Spring Cloud Stream The implementation is based on event driven and CQRS System architecture .

The content of this article is MOM asynchronous communication ,Stream Advanced application

原网站

版权声明
本文为[Java enthusiast]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206251343110536.html