spring+springmvc+kafka分布式消息中间件集成方案

Honghu的消息服务平台已经抛弃了之前的ActiveMQ,改用高吞吐量比较大的Kafka分布式消息中间件方案:

kafka消息平台使用spring+kafka的集成方案,详情如下:
1. 使用最高版本2.1.0.RELEASE集成jar包:spring-integration-kafka


2. Zookeeper、Kafka分布式集群使用init.properties配置化方案。

Java代码 复制代码  收藏代码
  1. kafka.servers=127.0.0.1:9092    
  2. kafka.topic=xxxooo  
kafka.servers=127.0.0.1:9092  
kafka.topic=xxxooo

 
3. 使用消息生产者spring-context-producer配置化方案。

Java代码 复制代码  收藏代码
  1. "1.0" encoding="UTF-8"?>    
  2. "http://www.springframework.org/schema/beans"    
  3.      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  4.      xmlns:context="http://www.springframework.org/schema/context"    
  5.      xsi:schemaLocation="http://www.springframework.org/schema/beans    
  6.          http://www.springframework.org/schema/beans/spring-beans.xsd    
  7.          http://www.springframework.org/schema/context    
  8.          http://www.springframework.org/schema/context/spring-context.xsd">   
  9.   
  10.   
  11.       
  12.     "producerProperties" class="java.util.HashMap">  
  13.           
  14.               
  15.                 "bootstrap.servers" value="localhost:9092" />  
  16.                 "group.id" value="2" />  
  17.                 "retries" value="10" />  
  18.                 "batch.size" value="16384" />  
  19.                 "linger.ms" value="1" />  
  20.                 "buffer.memory" value="33554432" />  
  21.                 "key.serializer"  
  22.                     value="org.apache.kafka.common.serialization.IntegerSerializer" />  
  23.                 "value.serializer"  
  24.                     value="org.apache.kafka.common.serialization.StringSerializer" />  
  25.               
  26.           
  27.       
  28.   
  29.       
  30.     "producerFactory"  
  31.         class="org.springframework.kafka.core.DefaultKafkaProducerFactory">  
  32.           
  33.             "producerProperties" />  
  34.           
  35.       
  36.   
  37.       
  38.     "KafkaTemplate" class="org.springframework.kafka.core.KafkaTemplate">  
  39.         "producerFactory" />  
  40.         "autoFlush" value="true" />  
  41.         "defaultTopic" value="test" />  
  42.       
  43.   
  
 


	
	
		
			
				
				
				
				
				
				
				
				
			
		
	

	
	
		
			
		
	

	
	
		
		
		
	

 
4. 使用消息消费者spring-context-producer配置化方案。

Java代码 复制代码  收藏代码
  1. "1.0" encoding="UTF-8"?>    
  2. "http://www.springframework.org/schema/beans"    
  3.      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  4.      xmlns:context="http://www.springframework.org/schema/context"    
  5.      xsi:schemaLocation="http://www.springframework.org/schema/beans    
  6.          http://www.springframework.org/schema/beans/spring-beans.xsd    
  7.          http://www.springframework.org/schema/context    
  8.          http://www.springframework.org/schema/context/spring-context.xsd">   
  9.   
  10.   
  11.       
  12.     "consumerProperties" class="java.util.HashMap">  
  13.           
  14.               
  15.                 "bootstrap.servers" value="localhost:9092" />  
  16.                 "group.id" value="0" />  
  17.                 "enable.auto.commit" value="true" />  
  18.                 "auto.commit.interval.ms" value="1000" />  
  19.                 "session.timeout.ms" value="15000" />  
  20.                 "key.deserializer"  
  21.                     value="org.apache.kafka.common.serialization.IntegerDeserializer" />  
  22.                 "value.deserializer"  
  23.                     value="org.apache.kafka.common.serialization.StringDeserializer" />  
  24.               
  25.           
  26.       
  27.   
  28.       
  29.     "consumerFactory"  
  30.         class="org.springframework.kafka.core.DefaultKafkaConsumerFactory">  
  31.           
  32.             "consumerProperties" />  
  33.           
  34.       
  35.   
  36.       
  37.     "messageListernerConsumerService" class="com.sml.sz.kafka.KafKaConsumer" />  
  38.   
  39.       
  40.     "containerProperties"  
  41.         class="org.springframework.kafka.listener.config.ContainerProperties">  
  42.         "test" />  
  43.         "messageListener" ref="messageListernerConsumerService" />  
  44.       
  45.   
  46.       
  47.     "messageListenerContainer"  
  48.         class="org.springframework.kafka.listener.KafkaMessageListenerContainer"  
  49.         init-method="doStart">  
  50.         "consumerFactory" />  
  51.         "containerProperties" />  
  52.       
  53.     
  
 


	
	
		
			
				
				
				
				
				
				
				
			
		
	

	
	
		
			
		
	

	
	

	
	
		
		
	

	
	
		
		
	
  

 
5. 使用注解方式注入消息类型

@Autowired  
private KafkaTemplate kafkaTemplate;

6. 重写MessageListener 的getMessage方法获取消息(业务实现)

7. RestFul服务方式测试消息服务
Java代码 复制代码  收藏代码
  1. @CrossOrigin(origins = "*", maxAge = 3600, methods = { RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE,  
  2.         RequestMethod.PUT })  
  3. @RestController  
  4. @RequestMapping(value = "/rest/kafka")  
  5. public class KafKaProducer {  
  6.       
  7.     @RequestMapping(value = "/send", method = RequestMethod.GET)  
  8.     public JSONObject save() {  
  9.         System.out.println("+++++++++++++++++++++++++++++++");  
  10.         kafkaTemplate.sendDefault("HongHu KAFKA分布式消息服务测试");    
  11.         return null;  
  12.     }  
  13.       
  14.     @Autowired    
  15.     private KafkaTemplate kafkaTemplate;  
  16.       
  17.     
  18.   
  19. }  
@CrossOrigin(origins = "*", maxAge = 3600, methods = { RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE,
		RequestMethod.PUT })
@RestController
@RequestMapping(value = "/rest/kafka")
public class KafKaProducer {
	
	@RequestMapping(value = "/send", method = RequestMethod.GET)
	public JSONObject save() {
		System.out.println("+++++++++++++++++++++++++++++++");
		kafkaTemplate.sendDefault("HongHu KAFKA分布式消息服务测试");  
		return null;
	}
	
	@Autowired  
    private KafkaTemplate kafkaTemplate;
	
  

}
 
Java代码 复制代码  收藏代码
  1. @RestController  
  2. public class KafKaConsumer implements MessageListener {  
  3.   
  4.     @Autowired  
  5.     private LogService logService;  
  6.     public void onMessage(ConsumerRecord records) {  
  7.         System.out.println("====================" + records);  
  8.         Object o = records.value();  
  9.         Log log = new Log();  
  10.         log.setIsNewRecord(true);  
  11.         log.setId(IdGen.uuid());  
  12.         log.setTitle(String.valueOf(o));  
  13.         logService.save(log);  
  14.   
  15.     }  
  16.   
  17. }  
@RestController
public class KafKaConsumer implements MessageListener {

	@Autowired
	private LogService logService;
	public void onMessage(ConsumerRecord records) {
		System.out.println("====================" + records);
		Object o = records.value();
		Log log = new Log();
		log.setIsNewRecord(true);
		log.setId(IdGen.uuid());
		log.setTitle(String.valueOf(o));
		logService.save(log);

	}

}
 
愿意了解框架技术或者源码的朋友直接求求交流分享技术:3133806896


分布式的一些解决方案,有愿意了解的朋友可以找我们团队探讨


更多详细源码参考来源


你可能感兴趣的:(spring,spring,mvc,spring,spring,mvc)