简体   繁体   English

EnableBinding 在 Spring Cloud Stream 3.x 中被弃用

[英]EnableBinding is deprecated in Spring Cloud Stream 3.x

I am using Kafka for a microservices project.我正在将 Kafka 用于微服务项目。 I want to invoke an event whenever I save a record to the database.每当我将记录保存到数据库时,我都想调用一个事件。 I have been looking at tutorials about Spring Cloud Stream. All of them are using @EnableBinding, @Input, @Output annotations.一直在看Spring Cloud Stream的教程,都是用@EnableBinding、@Input、@Output注解。 When I try to use them, it says they are deprecated.当我尝试使用它们时,它说它们已被弃用。 I am using spring initialzr.我正在使用 spring initialzr。 The release notes say that I should use Supplier, Consumer, and Function instead of the old methods like Input, Output, and Process.发行说明说我应该使用 Supplier、Consumer 和 Function 而不是 Input、Output 和 Process 等旧方法。

@Bean
public Supplier<String> toUpperCase() {
    return () -> {
        return "hello from supplier";
    };
}

When I use a Supplier like this, it generates the message every second as it is also highlighted in the tutorials.当我像这样使用 Supplier 时,它每秒都会生成一条消息,因为它也在教程中突出显示。 I don't want it to be published every second.我不希望它每秒都被发布。 I want it to be published when I want it to.我希望它在我想要的时候发布。 It says I should invoke its get() method but I don't know how.它说我应该调用它的 get() 方法,但我不知道如何调用。 Tutorials use deprecated functions to achieve such a functionality.教程使用不推荐使用的函数来实现这样的功能。 How can I achieve such behavior without deprecated functions or how can I use the EnableBinder annotation without it is saying it is deprecated?我如何在没有弃用函数的情况下实现这种行为,或者我如何使用 EnableBinder 注释而不表明它已被弃用?

You can check my repo for a demo project athttps://github.com/HabeebCycle/spring-cloud-stream-implemention您可以在https 查看我的演示项目的 repo://github.com/HabeebCycle/spring-cloud-stream-implemention

It shows how to implement cloud-stream using RabbitMQ and Kafka for both the supplier and the consumer and also end-to-end testing of both services.它展示了如何使用 RabbitMQ 和 Kafka 为供应商和消费者实施云流,以及如何对这两种服务进行端到端测试。

for your case: In your supplier bean do something like this:对于你的情况:在你的供应商 bean 中做这样的事情:

@Bean
public Supplier<DataEvent<String, User>> savedMessage() {
    return () -> {
        return null;
    };
}

Spring provides StreamBridge in the function package that can be used to send events. Spring提供function package中的StreamBridge可以用来发送事件。 Let's assume you have a service layer that saves into the database.假设您有一个保存到数据库中的服务层。 The first thing to do is to create an autowired StreamBridge that is injected by constructor binding and use it to send your message as follows.要做的第一件事是创建一个由构造函数绑定注入的自动装配的 StreamBridge,并使用它来发送您的消息,如下所示。 Notice that the name of the supplier should be the binding name of your output as explained in the documentation.请注意,供应商的名称应该是您的 output 的绑定名称,如文档中所述。

private final StreamBridge stream;
private final UserRepository repo;

// Store your topic/binding name as the supplier name as follows
private static final String SUPPLIER_BINDING_NAME = "savedMessage-out-0"

public UserService(UserRepository repo, StreamBridge stream) {
   this.repo = repo;
   this.stream = stream;
}

// Your save method
public void saveUser(User user) {
  // Do some checking...

  //save your record
  User user = repo.save(user);
  
  //check if user is saved or not null
  //create your message event (Assuming you have a DataEvent class)
  DataEvent<String, User> event = new DataEvent<>("User Saved", user);
  boolean sent = stream.send(SUPPLIER_BINDING_NAME, event));

  // Check the repo above for proper implementation.
}

For the consumer implementation, check my repo above.对于消费者实施,请查看我上面的回购协议。

There is also an implementation here although written in Kotlin https://piotrminkowski.com/2020/06/05/introduction-to-event-driven-microservices-with-spring-cloud-stream/这里也有实现虽然写在Kotlin https://piotrminkowski.com/2020/06/05/introduction-to-event-driven-microservices-with-spring-cloud-stream/

You can also check Spring recent project on GitHub here https://github.com/spring-cloud/spring-cloud-stream-samples/您还可以在 GitHub 此处查看 Spring 最近的项目https://github.com/spring-cloud/spring-cloud-stream-samples/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM