Spring Cloud Stream Kafka不能在两个模块中使用消息,但可以在一个模块中工作

时间:2019-04-20 13:10:56

标签: java apache-kafka spring-cloud-stream

我正在使用Spring Cloud Stream kafka进行消息通信。当我将消费者和生产者放在两个模块中时,生产者可以将消息发送到主题,但是消费者无法获取消息。但是它可以在一个模块中工作,并且代码不变。

  • Springboot版本:2.1.3发行版
  • Spring Cloud版本:Greenwich.RELEASE
  • Kafka版本:2.0.1

我的pom.xml

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-stream-kafka</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

制作人

spring:
  kafka:
    bootstrap-servers: localhost:9092
  cloud:
    stream:
      bindings:
        output:
          destination: org_change
          content-type: application/json

OrganizationServiceApplication.java

@SpringBootApplication
@RefreshScope
@EnableEurekaClient
@EnableCircuitBreaker
@EnableBinding(Source.class)
public class OrganizationServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(OrganizationServiceApplication.class, args);
    }
}

SimpleSourceBean.java

@Component
@Slf4j
public class SimpleSourceBean {
    private final Source organizationStreams;

    @Autowired
    public SimpleSourceBean(Source organizationStreams) {
        this.organizationStreams = organizationStreams;
    }

    public void publishOrgChange(String action, String orgId) {
        log.debug("Send Kafka message {} for Organization Id: {}", action, orgId);
        // OrganizationChangeModel is a model(JOPO).
        OrganizationChangeModel model = new OrganizationChangeModel(
                OrganizationChangeModel.class.getTypeName(),
                action,
                orgId,
                UserContext.CORRELATION_ID
        );

        organizationStreams
                .output()
                .send(MessageBuilder
                        .withPayload(model)
                        .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON)
                        .build());
    }

消费者

config:

spring:
  kafka:
    bootstrap-servers: localhost:9092
  cloud:
    stream:
      bindings:
        input:
          destination: org_change
          content-type: application/json
          group: licensingGroup

LicensesServiceApplication.java


/**
 * @author ming
 */
@SpringBootApplication
@RefreshScope
@EnableDiscoveryClient
@EnableFeignClients("com.ming.licenses.client")
@EnableCircuitBreaker
@EnableBinding(Sink.class)
@Slf4j
public class LicensesServiceApplication {

    @Autowired
    private OAuth2ClientContext oAuth2ClientContext;

    public static void main(String[] args) {
        SpringApplication.run(LicensesServiceApplication.class, args);
    }

    @StreamListener(Sink.INPUT)
    public void loggerSink(OrganizationChangeModel changeModel) {
        log.debug("Received an event for organization id {}.", changeModel.getOrganizationId());
    }
}

0 个答案:

没有答案