学习电脑,计算机系统故障维护,电脑技术,电脑知识学习-就上第二电脑网
当前位置: 首页 > 电脑知识 > 电脑基础

电脑waifaikafka+windows+java+springboot中的配置

 更新时间: 2019-07-24 14:42:08   作者:第二电脑网   来源:第二电脑网   浏览数:319   我要评论

1.百度kafka+zookeepe+widows配置 1.1  zookeepe配置 dataDi=/tmp/zookeepe # the pot at which the cliets will coect clietPot=2181 # disable the pe-ip l

W WW.002pc .COM认为此文章对《电脑waifaikafka+windows+java+springboot中的配置》说的很在理,002pc.com为你提供最佳的电脑知识网,程序员。

application-dev.properties

kafka.bootstrap-servers=x.x.x.x:9092

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="trace" dest="/data/logs/work/log.log">
    <appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout>
                <charset>UTF-8</charset>
                <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n</Pattern>
            </PatternLayout>
            <ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
        </Console>
        <RollingFile name="RollingFile" fileName="/data/logs/work/work.log"
                     filePattern="/data/logs/work/work-%d{yyyy-MM-dd}-%i.log">
            <PatternLayout>
                <charset>UTF-8</charset>
                <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="1000 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="20"/>
            <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
        </RollingFile>

        <RollingFile name="ErrorFile" fileName="/data/logs/work/error.log"  filePattern="/data/logs/work/error.%d{yyyy-MM-dd}.%i.log">
            <PatternLayout>
                <charset>UTF-8</charset>
                <Pattern>%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%M%n</Pattern>
            </PatternLayout>
            <Filters>
                <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
            </Filters>
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="50 MB"/>
            </Policies>
            <DefaultRolloverStrategy fileIndex="min" max="100"/>
        </RollingFile>
    </appenders>
    <loggers>
        <Root level="info">
            <appender-ref ref="Console"/>
            <appender-ref ref="RollingFile"  level="info"/>
        </Root>
        <Logger name="com.tangxin.kafka">
            <appender-ref ref="ErrorFile" />
        </Logger>
    </loggers>
</Configuration>

2.5 kafka配置类

package com.tangxin.kafka.service;

import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.serialization.StringSerializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.core.ProducerFactory;

import java.util.HashMap;
import java.util.Map;

@Configuration
@EnableKafka
public class KafkaProducerConfig {

    @Value("${kafka.bootstrap-servers}")
    private String bootstrapServers;

    public Map<String, Object> producerConfigs() {
        Map<String, Object> props = new HashMap<>();
        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        return props;
    }

    public ProducerFactory<String, String> producerFactory() {
        return new DefaultKafkaProducerFactory<>(producerConfigs());
    }

    @Bean
    public KafkaTemplate<String, String> kafkaTemplate() {
        return new KafkaTemplate(producerFactory());
    }
}

2.6 controller层调用kafka发送

package com.tangxin.kafka.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


@RestController
public class KafkaController {

    @Autowired
    private KafkaTemplate kafkaTemplate;

    @RequestMapping(value = "/send", method = { RequestMethod.GET, RequestMethod.POST })
    public String callFeedInfo() throws Exception {
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        executorService.submit(() -> {
            try {
                kafkaTemplate.send("feed-info","1000");
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        return "send done!";
    }

}
更多:电脑waifaikafka+windows+java+springboot中的配置
https://www.002pc.com/diannaojichu/212.html

你可能感兴趣的windows,java,springboot,kafka,配置

关于我们 - 广告合作 - 联系我们 - 免责声明 - 网站地图 - 投诉建议 - 在线投稿

  浙ICP备140365454号

©CopyRight 2008-2020 002pc.COM Inc All Rights Reserved. 第二电脑网 版权所有 联系QQ:282523118