Spring Cloud Stream与Kafka(二)
文章目录
- Spring Cloud Stream与Kafka(二)
- Spring Cloud Stream提供的信道
- 自定义Binding声明接口
- Spring Cloud Stream注解
- 发布与订阅
Spring Cloud Stream提供的信道
- Source接口
package org.springframework.cloud.stream.messaging;
import org.springframework.cloud.stream.annotation.Output;
import org.springframework.messaging.MessageChannel;
public interface Source {
String OUTPUT = "output";
@Output(Source.OUTPUT)
MessageChannel output();
}
- Sink接口
package org.springframework.cloud.stream.messaging;
import org.springframework.cloud.stream.annotation.Input;
import org.springframework.messaging.SubscribableChannel;
public interface Sink {
String INPUT = "input";
@Input(Sink.INPUT)
SubscribableChannel input();
}
- Processor接口
package org.springframework.cloud.stream.messaging;
public interface Processor extends Source, Sink {
}
自定义Binding声明接口
- 创建自定义绑定接口,定义@Input和@Output时如果没有名称,默认获取当前方法的名称作为绑定名称。应用的时候和其他的绑定接口一样通过@EnableBinding进行声明。
public interface CustomBinding {
String INPUT1 = "input1";
String OUTPUT1 = "output1";
@Input
SubscribableChannel input1();
@Output
MessageChannel output1();
}
- 应用自定义接口
@SpringBootApplication
@EnableBinding({CustomBinding.class, Source.class})
public class Application implements CommandLineRunner{
}
Spring Cloud Stream注解
- @Output注解指示框架将会创建一个输出绑定目标。
public @interface Output {
String value() default "";
}
- @Input注解指示框架将会创建一个输入绑定目标。
public @interface Input {
String value() default "";
}
- @StreamListener注解可以把方法标记为通过@EnableBinding注解声明的输入的监听器。
@Target({ ElementType.METHOD, ElementType.ANNOTATION_TYPE })
@Retention(RetentionPolicy.RUNTIME)
@MessageMapping
@Documented
public @interface StreamListener {
@AliasFor("target")
String value() default "";
@AliasFor("value")
String target() default "";
String condition() default "";
String copyHeaders() default "true";
}
- @SendTo注解指示方法的返回值被转换为消息发送到指定的目的地。
public @interface SendTo {
String[] value() default {};
}
- @EnableBinding根据作为值传递给注释的接口列表,启用带有@Input和@Output注释的目标绑定到代理。
@Target({ ElementType.TYPE, ElementType.ANNOTATION_TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Configuration
@Import({ BindingBeansRegistrar.class, BinderFactoryConfiguration.class })
@EnableIntegration
public @interface EnableBinding {
Class<?>[] value() default {};
}
package org.springframework.cloud.stream.config;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.binding.BindingBeanDefinitionRegistryUtils;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.ClassUtils;
public class BindingBeansRegistrar implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata metadata,
BeanDefinitionRegistry registry) {
AnnotationAttributes attrs =
AnnotatedElementUtils.getMergedAnnotationAttributes(
ClassUtils.resolveClassName(metadata.getClassName(), null),
EnableBinding.class);
for (Class<?> type : collectClasses(attrs, metadata.getClassName())) {
if (!registry.containsBeanDefinition(type.getName())) {
BindingBeanDefinitionRegistryUtils
.registerBindingTargetBeanDefinitions(
type, type.getName(), registry);
BindingBeanDefinitionRegistryUtils
.registerBindingTargetsQualifiedBeanDefinitions(ClassUtils
.resolveClassName(metadata.getClassName(), null), type, registry); }
}
}
private Class<?>[] collectClasses(AnnotationAttributes attrs, String className) {
EnableBinding enableBinding = AnnotationUtils.synthesizeAnnotation(attrs,
EnableBinding.class, ClassUtils.resolveClassName(className, null));
return enableBinding.value();
}
}
发布与订阅
- Spring Cloud Stream默认在接收和发送消息时对应的消息格式类型都是JSON,我们可以通过绑定的contentType属性进行指定。当发送和接收消息时都会被MessageConverter消息转换器进行转换。
spring.cloud.stream.bindings.output.content-type=application/json
spring.cloud.stream.bindings.output1.destination=test
spring.cloud.stream.bindings.input1.destination=test
spring.cloud.stream.bindings.input1.group=test_group