给我一个 spring cloud stream kafka demo
时间: 2023-04-02 19:03:47 浏览: 191
好的,这里是一个 Spring Cloud Stream Kafka 的示例代码,你可以参考一下:
```
@SpringBootApplication
@EnableBinding(Sink.class)
public class KafkaConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(KafkaConsumerApplication.class, args);
}
@StreamListener(Sink.INPUT)
public void handle(String message) {
System.out.println("Received message: " + message);
}
}
```
这个示例代码演示了如何使用 Spring Cloud Stream 和 Kafka 来消费消息。在这个示例中,我们使用了 `@EnableBinding(Sink.class)` 来绑定 Kafka 的输入通道,然后使用 `@StreamListener(Sink.INPUT)` 来监听输入通道上的消息。当有消息到来时,`handle` 方法会被调用,并打印出接收到的消息。
希望这个示例能对你有所帮助!
相关问题
spring cloud 规则引擎 demo
这里提供一个基于Spring Cloud和Drools规则引擎的简单demo,实现了根据用户输入的商品信息进行促销活动匹配的功能。
首先,需要在pom.xml文件中添加依赖:
```
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-schema</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-schema-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-schema-serde</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-kafka-streams</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-kafka-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-spring</artifactId>
<version>7.62.0.Final</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-core</artifactId>
<version>7.62.0.Final</version>
</dependency>
```
接着,在application.yml文件中配置RabbitMQ的连接信息:
```
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
```
然后,创建一个商品实体类Product:
```
public class Product {
private String name;
private int price;
private String category;
// getter和setter方法省略
}
```
接下来,创建一个规则引擎服务类RuleEngineService:
```
@Service
public class RuleEngineService {
private final KieContainer kieContainer;
public RuleEngineService(KieContainer kieContainer) {
this.kieContainer = kieContainer;
}
public List<String> getProductPromotions(Product product) {
List<String> promotions = new ArrayList<>();
KieSession kieSession = kieContainer.newKieSession();
kieSession.insert(product);
kieSession.setGlobal("promotions", promotions);
kieSession.fireAllRules();
kieSession.dispose();
return promotions;
}
}
```
在这个服务类中,我们通过注入KieContainer来获取Drools规则引擎的实例,然后根据用户传入的商品信息,通过Drools规则引擎来匹配对应的促销活动,最终返回一个促销活动列表。
最后,创建一个消息消费者类MessageConsumer:
```
@EnableBinding(Sink.class)
public class MessageConsumer {
private final RuleEngineService ruleEngineService;
public MessageConsumer(RuleEngineService ruleEngineService) {
this.ruleEngineService = ruleEngineService;
}
@StreamListener(target = Sink.INPUT)
public void process(Product product) {
List<String> promotions = ruleEngineService.getProductPromotions(product);
System.out.println("商品名称:" + product.getName());
System.out.println("促销活动列表:" + promotions);
}
}
```
这个消费者类通过@EnableBinding注解来绑定消息通道,然后使用@StreamListener注解监听输入通道,获取用户传入的商品信息,并通过RuleEngineService来获取促销活动列表并输出。
这样,一个简单的Spring Cloud和Drools规则引擎的demo就完成了。
阅读全文