├── .gitignore ├── README.md ├── pom.xml ├── push.txt ├── src ├── main │ ├── java │ │ └── com │ │ │ └── sunney │ │ │ ├── Application.java │ │ │ ├── service │ │ │ ├── KafkaConsumerService.java │ │ │ ├── KafkaService.java │ │ │ ├── UserDto.java │ │ │ └── impl │ │ │ │ ├── KafkaConsumerServiceImpl.java │ │ │ │ └── KafkaServiceImpl.java │ │ │ └── task │ │ │ └── ProducerTast.java │ └── resources │ │ ├── applicationContext.xml │ │ ├── spring-kafka-consumer.xml │ │ └── spring-kafka-producer.xml └── test │ └── java │ └── com │ └── sunney │ └── AppTest.java └── 说明.txt /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | .settings 3 | .classpath 4 | .project 5 | target 6 | 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | spring-integration-kafka 1.3.0.RELEASE 应用 2 | 3 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | org.springframework.boot 6 | spring-boot-starter-parent 7 | 1.3.3.RELEASE 8 | 9 | com.sunney 10 | kafka-demo 11 | jar 12 | 1.0-SNAPSHOT 13 | kafka-demo 14 | http://maven.apache.org 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-web 19 | 20 | 21 | org.springframework.integration 22 | spring-integration-kafka 23 | 1.3.0.RELEASE 24 | 25 | 26 | junit 27 | junit 28 | 4.11 29 | test 30 | 31 | 32 | org.apache.avro 33 | avro 34 | 1.7.7 35 | 36 | 37 | com.alibaba 38 | fastjson 39 | 1.2.7 40 | 41 | 42 | com.wanrong 43 | common-share 44 | 1.0 45 | 46 | 47 | 48 | kafak-demo 49 | 50 | 51 | src/main/resources 52 | true 53 | 54 | 55 | 56 | 57 | maven-compiler-plugin 58 | 3.3 59 | 60 | 1.7 61 | 1.7 62 | 63 | 64 | 65 | org.springframework.boot 66 | spring-boot-maven-plugin 67 | 1.3.3.RELEASE 68 | 69 | 70 | 71 | repackage 72 | 73 | 74 | 75 | 76 | 78 | false 79 | 80 | 81 | 82 | org.apache.maven.plugins 83 | maven-resources-plugin 84 | 2.6 85 | 86 | 87 | @ 88 | 89 | 90 | 91 | 92 | 93 | 94 | -------------------------------------------------------------------------------- /push.txt: -------------------------------------------------------------------------------- 1 | Git提交到多个远程仓库 2 | 输入: 3 | git remote add origin git@git.oschina.net:sunney/kafka-demo.git 4 | 再添加另一个: 5 | git remote set-url --add origin git@git.oschina.net:sunney/kafka-demo.git 6 | 提交 7 | git push origin --all 8 | 查看 origin 9 | git remote -v 10 | 11 | origin git@github.com:sunney2010/kafka-demo.git (fetch) 12 | origin git@github.com:sunney2010/kafka-demo.git (push) 13 | origin git@git.oschina.net:sunney/kafka-demo.git (push) 14 | 15 | 正确 -------------------------------------------------------------------------------- /src/main/java/com/sunney/Application.java: -------------------------------------------------------------------------------- 1 | package com.sunney; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | 6 | import org.springframework.boot.SpringApplication; 7 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 8 | import org.springframework.boot.autoconfigure.SpringBootApplication; 9 | import org.springframework.boot.web.servlet.ServletComponentScan; 10 | import org.springframework.context.ApplicationContext; 11 | import org.springframework.context.annotation.ComponentScan; 12 | import org.springframework.scheduling.annotation.EnableScheduling; 13 | 14 | /** 15 | * Hello world! 16 | */ 17 | @SpringBootApplication 18 | @EnableScheduling 19 | @ComponentScan 20 | @EnableAutoConfiguration 21 | @ServletComponentScan 22 | public class Application { 23 | 24 | public static ApplicationContext applicationContext; 25 | 26 | public static void main(String[] args) throws Exception { 27 | 28 | SpringApplication app = new SpringApplication(Application.class); 29 | app.setWebEnvironment(false); 30 | Set set = new HashSet(); 31 | set.add("classpath:applicationContext.xml"); 32 | app.setSources(set); 33 | applicationContext = app.run(args); 34 | 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/sunney/service/KafkaConsumerService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1999-2024 Colotnet.com All right reserved. This software is the confidential and proprietary information of 3 | * Colotnet.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only 4 | * in accordance with the terms of the license agreement you entered into with Colotnet.com. 5 | */ 6 | package com.sunney.service; 7 | 8 | import java.util.Map; 9 | 10 | /** 11 | * 类KafkaConsumerService.java的实现描述:TODO 类实现描述 12 | * 13 | * @author Sunney 2016年5月6日 上午11:33:46 14 | */ 15 | public interface KafkaConsumerService { 16 | 17 | public void processMessage(Map> msgs); 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/sunney/service/KafkaService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1999-2024 Colotnet.com All right reserved. This software is the 3 | * confidential and proprietary information of Colotnet.com ("Confidential 4 | * Information"). You shall not disclose such Confidential Information and shall 5 | * use it only in accordance with the terms of the license agreement you entered 6 | * into with Colotnet.com. 7 | */ 8 | package com.sunney.service; 9 | 10 | /** 11 | * 类KafkaService.java的实现描述:发消息接口类 12 | * @author Sunney 2016年4月30日 上午11:30:53 13 | */ 14 | public interface KafkaService { 15 | /** 16 | * 发消息 17 | * @param topic 主题 18 | * @param obj 发送内容 19 | */ 20 | public void sendUserInfo(String topic, Object obj); 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/sunney/service/UserDto.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1999-2024 Colotnet.com All right reserved. This software is the 3 | * confidential and proprietary information of Colotnet.com ("Confidential 4 | * Information"). You shall not disclose such Confidential Information and shall 5 | * use it only in accordance with the terms of the license agreement you entered 6 | * into with Colotnet.com. 7 | */ 8 | package com.sunney.service; 9 | 10 | import java.util.Date; 11 | import java.util.List; 12 | 13 | /** 14 | * 类UserDto.java的实现描述:TODO 类实现描述 15 | * @author Sunney 2016年4月30日 上午10:53:54 16 | */ 17 | public class UserDto { 18 | private Integer userId; 19 | private String userName; 20 | private List userList; 21 | private Date gmtCeate; 22 | 23 | /** 24 | * @return the userId 25 | */ 26 | public Integer getUserId() { 27 | return userId; 28 | } 29 | 30 | /** 31 | * @param userId the userId to set 32 | */ 33 | public void setUserId(Integer userId) { 34 | this.userId = userId; 35 | } 36 | 37 | /** 38 | * @return the userName 39 | */ 40 | public String getUserName() { 41 | return userName; 42 | } 43 | 44 | /** 45 | * @param userName the userName to set 46 | */ 47 | public void setUserName(String userName) { 48 | this.userName = userName; 49 | } 50 | 51 | /** 52 | * @return the userList 53 | */ 54 | public List getUserList() { 55 | return userList; 56 | } 57 | 58 | /** 59 | * @param userList the userList to set 60 | */ 61 | public void setUserList(List userList) { 62 | this.userList = userList; 63 | } 64 | 65 | /** 66 | * @return the gmtCeate 67 | */ 68 | public Date getGmtCeate() { 69 | return gmtCeate; 70 | } 71 | 72 | /** 73 | * @param gmtCeate the gmtCeate to set 74 | */ 75 | public void setGmtCeate(Date gmtCeate) { 76 | this.gmtCeate = gmtCeate; 77 | } 78 | 79 | 80 | } 81 | -------------------------------------------------------------------------------- /src/main/java/com/sunney/service/impl/KafkaConsumerServiceImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1999-2024 Colotnet.com All right reserved. This software is the confidential and proprietary information of 3 | * Colotnet.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only 4 | * in accordance with the terms of the license agreement you entered into with Colotnet.com. 5 | */ 6 | package com.sunney.service.impl; 7 | 8 | import java.util.Collection; 9 | import java.util.Iterator; 10 | import java.util.LinkedHashMap; 11 | import java.util.List; 12 | import java.util.Map; 13 | import java.util.Set; 14 | 15 | import org.slf4j.Logger; 16 | import org.slf4j.LoggerFactory; 17 | 18 | import com.alibaba.fastjson.JSON; 19 | import com.sunney.service.KafkaConsumerService; 20 | import com.sunney.service.UserDto; 21 | 22 | /** 23 | * 类KafkaConsumerService.java的实现描述:消费接收类 24 | * 25 | * @author Sunney 2016年4月30日 上午11:46:14 26 | */ 27 | public class KafkaConsumerServiceImpl implements KafkaConsumerService{ 28 | 29 | static final Logger logger = LoggerFactory.getLogger(KafkaConsumerServiceImpl.class); 30 | @Override 31 | public void processMessage(Map> msgs) { 32 | logger.info("===============================================processMessage==============="); 33 | for (Map.Entry> entry : msgs.entrySet()) { 34 | logger.info("============Topic:" + entry.getKey()); 35 | LinkedHashMap messages = (LinkedHashMap) entry.getValue(); 36 | Set keys = messages.keySet(); 37 | for (Integer i : keys) 38 | logger.info("======Partition:" + i); 39 | Collection values = messages.values(); 40 | for (Iterator iterator = values.iterator(); iterator.hasNext();) { 41 | String message = "["+iterator.next()+"]"; 42 | logger.info("=====message:" + message); 43 | List userList = JSON.parseArray(message, UserDto.class); 44 | logger.info("=====userList.size:" + userList.size()); 45 | 46 | } 47 | 48 | } 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/sunney/service/impl/KafkaServiceImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1999-2024 Colotnet.com All right reserved. This software is the confidential and proprietary information of 3 | * Colotnet.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only 4 | * in accordance with the terms of the license agreement you entered into with Colotnet.com. 5 | */ 6 | package com.sunney.service.impl; 7 | 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.beans.factory.annotation.Qualifier; 10 | import org.springframework.integration.kafka.support.KafkaHeaders; 11 | import org.springframework.integration.support.MessageBuilder; 12 | import org.springframework.messaging.MessageChannel; 13 | import org.springframework.stereotype.Service; 14 | 15 | import com.sunney.service.KafkaService; 16 | 17 | /** 18 | * 类KafkaServiceImpl.java的实现描述:发消息实现类 19 | * 20 | * @author Sunney 2016年4月30日 上午11:31:13 21 | */ 22 | @Service("kafkaService") 23 | public class KafkaServiceImpl implements KafkaService { 24 | 25 | // 默认4个分区 26 | private static final int numPartitions = 4; 27 | @Autowired 28 | @Qualifier("kafkaTopicTest") 29 | MessageChannel channel; 30 | 31 | public void sendUserInfo(String topic, Object obj) { 32 | // 获取messageKey 33 | String messageKey = String.valueOf(obj.hashCode()); 34 | // 获取分区号 35 | int partitionId = partition(messageKey, numPartitions); 36 | channel.send(MessageBuilder.withPayload(obj).setHeader(KafkaHeaders.TOPIC, 37 | topic).setHeader(KafkaHeaders.MESSAGE_KEY, 38 | messageKey).setHeader(KafkaHeaders.PARTITION_ID, 39 | partitionId).build()); 40 | } 41 | 42 | /** 43 | * 获取分区号 44 | * 45 | * @param key KEY 46 | * @param numPartitions 分区数 47 | * @return 48 | */ 49 | private int partition(Object key, int numPartitions) { 50 | try { 51 | long partitionNum = Long.parseLong((String) key); 52 | return (int) Math.abs(partitionNum % numPartitions); 53 | } catch (Exception e) { 54 | return Math.abs(key.hashCode() % numPartitions); 55 | } 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/com/sunney/task/ProducerTast.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1999-2024 Colotnet.com All right reserved. This software is the confidential and proprietary information of 3 | * Colotnet.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only 4 | * in accordance with the terms of the license agreement you entered into with Colotnet.com. 5 | */ 6 | package com.sunney.task; 7 | 8 | import java.util.ArrayList; 9 | import java.util.Date; 10 | import java.util.HashMap; 11 | import java.util.List; 12 | import java.util.Map; 13 | import java.util.Random; 14 | 15 | import org.slf4j.Logger; 16 | import org.slf4j.LoggerFactory; 17 | import org.springframework.beans.factory.annotation.Autowired; 18 | import org.springframework.scheduling.annotation.Scheduled; 19 | import org.springframework.stereotype.Component; 20 | 21 | import com.alibaba.fastjson.JSON; 22 | import com.sunney.service.KafkaService; 23 | import com.sunney.service.UserDto; 24 | import com.wanrong.common.share.dto.EmailDto; 25 | import com.wanrong.common.share.enums.EmailTypeEnum; 26 | import com.wanrong.common.share.enums.TopicEnum; 27 | 28 | /** 29 | * 类ProducerTasl.java的实现描述:TODO 类实现描述 30 | * 31 | * @author Sunney 2016年5月6日 上午11:42:50 32 | */ 33 | @Component 34 | public class ProducerTast { 35 | 36 | protected Logger logger = LoggerFactory.getLogger(this.getClass()); 37 | @Autowired 38 | KafkaService kafkaService; 39 | 40 | @Scheduled(fixedRate = 5000) 41 | public void taskStart() { 42 | EmailDto email = new EmailDto(); 43 | email.setTitle("sunney"); 44 | email.setAsync(false); 45 | email.setEmailType(EmailTypeEnum.SEND_PASSWORD); 46 | Map paraMap = new HashMap(); 47 | paraMap.put("userName", "admin"); 48 | paraMap.put("password", "password"); 49 | paraMap.put("merchantId", "88888888888888888"); 50 | paraMap.put("merchantName", "深圳市前海万融科技有限公司"); 51 | email.setParaMap(paraMap); 52 | email.setReceiver("sunney888@qq.com"); 53 | 54 | kafkaService.sendUserInfo(TopicEnum.EMAILTOPIC.getKey(), JSON.toJSONString(email)); 55 | System.out.println("=============================:email"); 56 | } 57 | 58 | /** 59 | * 随机获取发送邮箱 60 | * 61 | * @return 62 | */ 63 | private String getemailSender() { 64 | String[] sender = new String[] { "mytopic", "sunneytopic" }; 65 | Random ra = new Random(); 66 | int val = ra.nextInt(sender.length); 67 | return sender[val]; 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /src/main/resources/applicationContext.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 20 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /src/main/resources/spring-kafka-consumer.xml: -------------------------------------------------------------------------------- 1 | 2 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 23 | 24 | 27 | 28 | 29 | 30 | 32 | 33 | 35 | 36 | 37 | smallest 38 | 10485760 39 | 5242880 40 | 1000 41 | 42 | 43 | 44 | 45 | 46 | 47 | 49 | 50 | 53 | 54 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /src/main/resources/spring-kafka-producer.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 19 | 20 | 21 | 3600000 22 | 5 23 | kafka.serializer.StringEncoder 24 | com.wanrong.payment.commons.MyPartitioner 25 | -1 26 | 5242880 27 | 10000 28 | 200 29 | 500 30 | 10000 31 | org.apache.kafka.common.serialization.ByteArraySerializer 32 | org.apache.kafka.common.serialization.ByteArraySerializer 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 46 | 48 | 49 | 51 | 55 | 57 | 58 | 59 | 65 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /src/test/java/com/sunney/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.sunney; 2 | 3 | import junit.framework.Test; 4 | import junit.framework.TestCase; 5 | import junit.framework.TestSuite; 6 | 7 | /** 8 | * Unit test for simple App. 9 | */ 10 | public class AppTest 11 | extends TestCase 12 | { 13 | /** 14 | * Create the test case 15 | * 16 | * @param testName name of the test case 17 | */ 18 | public AppTest( String testName ) 19 | { 20 | super( testName ); 21 | } 22 | 23 | /** 24 | * @return the suite of tests being tested 25 | */ 26 | public static Test suite() 27 | { 28 | return new TestSuite( AppTest.class ); 29 | } 30 | 31 | /** 32 | * Rigourous Test :-) 33 | */ 34 | public void testApp() 35 | { 36 | assertTrue( true ); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /说明.txt: -------------------------------------------------------------------------------- 1 | git push -u origin mastergit push -u origin mastergit push -u origin mastergit push -u origin master --------------------------------------------------------------------------------