zipkin2.reporter.Sender 在 docker 中使用 Zipkin 和 kafka 时找不到 bean

zipkin2.reporter.Sender bean not found when using with Zipkin with kafka in docker

我正在研究微服务,其中微服务相互通信。我将 Zipkin 与 Sleuth 和 Apache Kafka 一起用作消息代理,运行 微服务和 kafka 使用 docker-compose.

我正在使用 Spring Boot (2.2.6.RELEASE), Spring Cloud (Hoxton.SR3), Kafka Image(wurstmeister/kafka:2.12-2.4.1 ) 和 Zipkin 的最新图像。

当我尝试旋转容器时,微服务给出以下错误:

APPLICATION FAILED TO START

Description:

Parameter 2 of method reporter in org.springframework.cloud.sleuth.zipkin2.ZipkinAutoConfiguration > required a bean of type 'zipkin2.reporter.Sender' that could not be found.

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.adi.example</groupId>
    <artifactId>currency-conversion-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>currency-conversion-service</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR3</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-sleuth</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zipkin</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <finalName>conversion-server</finalName>
    </build>

</project>


Application.properties

spring.application.name=exchange-service
server.port=9000
    
eureka.client.fetch-registry=true
eureka.client.register-with-eureka=true
#eureka.client.service-url.defaultZone=http://localhost:9765/eureka/
eureka.client.service-url.defaultZone=http://eureka-server:9765/eureka/
eureka.instance.prefer-ip-address=true

spring.sleuth.sampler.probability=1

spring.zipkin.base-url=http://zipkin-server:9411/
spring.zipkin.sender.type=kafka

spring.kafka.bootstrap-servers=http://kafka:9092

docker-compose.yml

version: '3'

services: 
  
  zookeeper: 
    image: wurstmeister/zookeeper
    container_name: zookeeper-server
    ports:
      - "2181:2181"
    environment:
      - ALLOW_ANONYMOUS_LOGIN= 'yes'
    networks:
      - service-network
      
  kafka: 
    image: wurstmeister/kafka:2.12-2.4.1
    container_name: kafka-server
    ports: 
      - "9092:9092"
    depends_on: 
      - zookeeper
    networks: 
      - service-network
    environment: 
      - KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR= 1
      - KAFKA_ZOOKEEPER_CONNECT= zookeeper:2181
      - KAFKA_LISTENERS= PLAINTEXT://:9092
      - KAFKA_ADVERTISED_LISTENERS= PLAINTEXT://:9092
    
    
  eureka-server:
    image: discovery-server:1.0.0
    restart: always
    depends_on: 
      - zipkin-server
    ports: 
      - "9765:9765"
    networks: 
      - service-network
    
  exchange-server: 
    image: exchange-server:1.0.0
    restart: always
    ports: 
      - "9000:9000"
    depends_on: 
      - eureka-server
    networks: 
      - service-network
    
  conversion-server: 
    image: conversion-server:1.0.0
    restart: always
    ports: 
      - "9100:9100"
    depends_on: 
      - eureka-server
      - exchange-server
    networks: 
      - service-network
      
  zipkin-server: 
    image: openzipkin/zipkin
    ports: 
      - "9411:9411"
    restart: always
    depends_on: 
      - zookeeper
    networks:
      - service-network  
    
networks: 
  service-network: 

console-log

. ____ _ __ _ _

/\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \

( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \

\/ ___)| |_)| | | | | || (_| | ) ) ) )

' |____| .__|_| |_|_| |_\__, | / / / /

=========|_|==============|___/=/_/_/_/

:: Spring Boot :: (v2.2.6.RELEASE)

2020-04-22 15:03:47.165 INFO [exchange-service,,,] 1 --- [ main] c.e.d.CurrencyExchangeServiceApplication : No active profile set, falling back to default profiles: default

2020-04-22 15:03:51.847 INFO [exchange-service,,,] 1 --- [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=a980a587-b2e4-3cb6-b4e7-9abfad8a56f8

2020-04-22 15:03:55.619 INFO [exchange-service,,,] 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 9000 (http)

2020-04-22 15:03:55.687 INFO [exchange-service,,,] 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]

2020-04-22 15:03:55.688 INFO [exchange-service,,,] 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.33]

2020-04-22 15:03:55.890 INFO [exchange-service,,,] 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext

2020-04-22 15:03:55.893 INFO [exchange-service,,,] 1 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 8565 ms

2020-04-22 15:03:56.382 ERROR [exchange-service,,,] 1 --- [ main] o.s.b.web.embedded.tomcat.TomcatStarter : Error starting Tomcat context. Exception: org.springframework.beans.factory.UnsatisfiedDependencyException. Message: Error creating bean with name 'tracingFilter' defined in class path resource [org/springframework/cloud/sleuth/instrument/web/TraceWebServletAutoConfiguration.class]: Unsatisfied dependency expressed through method 'tracingFilter' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'httpTracing' defined in class path resource [org/springframework/cloud/sleuth/instrument/web/TraceHttpAutoConfiguration.class]: Unsatisfied dependency expressed through method 'httpTracing' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'tracing' defined in class path resource [org/springframework/cloud/sleuth/autoconfig/TraceAutoConfiguration.class]: Unsatisfied dependency expressed through method 'tracing' parameter 6; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'zipkinReporter' defined in class path resource [org/springframework/cloud/sleuth/zipkin2/ZipkinAutoConfiguration.class]: Unsatisfied dependency expressed through method 'reporter' parameter 2; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'zipkin2.reporter.Sender' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Qualifier(value=zipkinSender)}

2020-04-22 15:03:56.443 INFO [exchange-service,,,] 1 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]

2020-04-22 15:03:56.472 WARN [exchange-service,,,] 1 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat

2020-04-22 15:03:56.522 INFO [exchange-service,,,] 1 --- [ main] ConditionEvaluationReportLoggingListener :

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.

2020-04-22 15:03:57.082 ERROR [exchange-service,,,] 1 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :

***************************

APPLICATION FAILED TO START

Description:

Parameter 2 of method reporter in org.springframework.cloud.sleuth.zipkin2.ZipkinAutoConfiguration required a bean of type 'zipkin2.reporter.Sender' that could not be found.

The following candidates were found but could not be injected:

- Bean method 'rabbitSender' in 'ZipkinRabbitSenderConfiguration' not loaded because @ConditionalOnProperty (spring.zipkin.sender.type=rabbit) found different value in property 'spring.zipkin.sender.type'

- Bean method 'restTemplateSender' in 'ZipkinRestTemplateSenderConfiguration' not loaded because ZipkinSender org.springframework.cloud.sleuth.zipkin2.sender.ZipkinRestTemplateSenderConfiguration kafka sender type

Action:

Consider revisiting the entries above or defining a bean of type 'zipkin2.reporter.Sender' in your configuration.

MainApp.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;

import brave.sampler.Sampler;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients("com.example")
public class MainApp {

    public static void main(String[] args) {
        SpringApplication.run(CurrencyConversionServiceApplication.class, args);
    }
    
    @Bean
    public Sampler sampler() {
        return Sampler.ALWAYS_SAMPLE;
    }

}

错误原因是我忘记在pom.xml中添加kafka依赖 添加依赖后,错误消失了。

在我的例子中,我添加了 @EnableConfigurationProperties({KafkaProperties.class}) to @SpringBootApplication class

@SpringBootApplication
@EnableConfigurationProperties({KafkaProperties.class})
public class Application{
...
}