执行脚本目录 /bin

windows 在其单独的目录

快速上手

下载并且解压kafka压缩包

运行服务

以Windows为例,首先打开cmd:

1.  启动zookeeper:

bin\windows\zookeeper-server-start.bat config\zookeeper.properties

2.  启动kafka:

bin\windows\kafka-server-start.bat config\server.properties

创建主题topic

bin\windows\kafka-topics.bat --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic cloud

cloud

生产者:发送消息

\bin\windows>kafka-console-producer.bat --broker-list localhost:9092 --topic cloud

>hello

消费者:接收消息

\bin\windows>kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic cloud --from-beginning

hello

同类产品比较

ActiveMQ:JMS(Java Message Service)规范实现

RabbitMQ:AMQP(Advanced Message Queue Protocol)规范实现

Kafka:并非某种规范实现,它的灵活和性能相对是优势

Spring Kafka

import java.util.Properties;

import java.util.concurrent.ExecutionException;

import java.util.concurrent.Future;

import org.apache.kafka.clients.producer.KafkaProducer;

import org.apache.kafka.clients.producer.ProducerRecord;

import org.apache.kafka.clients.producer.RecordMetadata;

import org.apache.kafka.common.serialization.StringSerializer;

public class TestKafka{

public static void main(String[] args) throws ExecutionException, InterruptedException {

Properties properties = new Properties();

properties.setProperty("bootstrap.servers", "localhost:9092");

properties.setProperty("key.serializer", StringSerializer.class.getName());

properties.setProperty("value.serializer", StringSerializer.class.getName());

// 创建 Kafka Producer

KafkaProducer<String, String> kafkaProducer = new KafkaProducer(properties);

// 创建Kafka消息 = ProducerRecord

String topic = "cloud";

Integer partition = 0;

Long timestamp = System.currentTimeMillis();

String key = "message-key";

String value = "how are you!";

ProducerRecord<String, String> record = new ProducerRecord<String, String>(topic, partition, timestamp, key, value);

// 发送kafka消息

Future<RecordMetadata> metadataFuture = kafkaProducer.send(record);

// 强制执行

metadataFuture.get();

}

}

设计模式

Spring社区对data(Spring-data)操作,有一个基本的模式,Template模式:

JDBC:JdbcTemplate

Redis:RedisTemplate

Kafka:KafkaTemplate

JMS:JmsTemplate

Rest:RestTemplate

XXXTemplate一定实现XXXOpeations

KafkaTemplate implements KafkaOpeations

Maven依赖

<dependency>

<groupId>org.springframework.kafka</groupId>

<artifactId>spring-kafka</artifactId>

</dependency>

自动装配器: 外汇返佣http://www.fx61.com/,KafkaAutoConfiguration

其中KafkaTemplate会被自动装配:

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;

import org.springframework.boot.autoconfigure.kafka.KafkaProperties;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.kafka.core.KafkaTemplate;

import org.springframework.kafka.core.ProducerFactory;

import org.springframework.kafka.support.ProducerListener;

@Configuration

public class KafkaAutoConfiguration {

private final KafkaProperties properties;

public KafkaAutoConfiguration(KafkaProperties properties) {

this.properties = properties;

}

@Bean

@ConditionalOnMissingBean(KafkaTemplate.class)

public KafkaTemplate<?,?> kafkaTemplate(ProducerFactory<Object,Object> kafkaProducerFactory,

ProducerListener<Object,Object> kafkaProducerListener){

KafkaTemplate<Object,Object> kafkaTemplate = new KafkaTemplate<Object,Object>(kafkaProducerFactory);

kafkaTemplate.setProducerListener(kafkaProducerListener);

kafkaTemplate.setDefaultTopic(this.properties.getTemplate().getDefaultTopic());

return kafkaTemplate;

}

}

创建生产者

增加生产者配置

application.properties

全局配置:

### Kafka生产者配置

spring.kafka.producer.bootstrapServers = localhost:9092

### Kafka生产者配置

# spring.kafka.producer.bootstrapServers = localhost:9092

spring.kafka.producer.keySerializer = org.apache.kafka.common.serialization.StringSerializer

spring.kafka.producer.valueSerializer = org.apache.kafka.common.serialization.StringSerializer

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.kafka.core.KafkaTemplate;

import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.bind.annotation.RestController;

@RestController

public class KafkaProducerController {

private final KafkaTemplate<String, String> kafkaTemplate;

private final String topic;

@Autowired

public KafkaProducerController(KafkaTemplate<String, String> kafkaTemplate, @Value("${kafka.topic}") String topic) {

this.kafkaTemplate = kafkaTemplate;

this.topic = topic;

}

@PostMapping("/message/send")

public Boolean sendMessage(@RequestParam(required=false)String message) {

kafkaTemplate.send(topic, message);

return true;

}

}

创建消费者

增加消费者配置

### Kafka消费者配置

spring.kafka.consumer.groupId = cloud-1

spring.kafka.consumer.keyDeserializer = org.apache.kafka.common.serialization.StringDeserializer

spring.kafka.consumer.valueDeserializer = org.apache.kafka.common.serialization.StringDeserializer

import org.springframework.kafka.annotation.KafkaListener;

import org.springframework.stereotype.Component;

@Component

public class KafkaConsumerListener {

@KafkaListener(topics = "${kafka.topic}")

public void onMessage(String message) {

System.out.print("kafka 消费者监听器,接收到消息:" + message);

}

}

端口信息

spring-cloud-zuul:7070

person-client:8080

person-service:9090

Eureka Server:12345

ZipKin Server:23456

Config Server:10001

服务启动顺序

zipkin Server

Eureka Server

spring-cloud-config-server

person-server

person-client

spring-cloud-zuul

spring-cloud-sleuth

spring-cloud-sleuth-demo改造

增加Eureka客户端依赖

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

</dependency>

配置调整

spring.application.name = spring-cloud-sleuth

server.port = 6060

spring.zipkin.base-url=http://localhost:23456/

eureka.client.serviceUrl.defaultZone=http://localhost:12345/eureka

调整代码链接:spring-cloud-zuul

完整调用链路

spring-cloud-sleuth →   spring-cloud-zuul →   person-client →   person-service

@RestController

public class TestLoggerController {

final static Logger LOGGER = LoggerFactory.getLogger(TestLoggerController.class);

@Autowired

@Qualifier("restTemplate")

private RestTemplate restTemplate;

@GetMapping("/send")

public void send() {

LOGGER.info(" 欢迎欢迎!");

}

@GetMapping("/to/zuul/pseron-clint/findall")

public Object findall() {

LOGGER.info("TestLoggerController#findall()");

return restTemplate.getForObject("http://spring-cloud-zuul/person-client/person/findall", Object.class);

}

Spring Cloud (Spring Cloud Stream)解析的更多相关文章

  1. Feign 系列(05)Spring Cloud OpenFeign 源码解析

    Feign 系列(05)Spring Cloud OpenFeign 源码解析 [TOC] Spring Cloud 系列目录(https://www.cnblogs.com/binarylei/p/ ...

  2. Spring Cloud 系列之 Stream 消息驱动(二)

    本篇文章为系列文章,未读第一集的同学请猛戳这里:Spring Cloud 系列之 Stream 消息驱动(一) 本篇文章讲解 Stream 如何实现消息分组和消息分区. 消息分组 如果有多个消息消费者 ...

  3. Spring Boot,Spring Cloud,Eureka,Actuator,Spring Boot Admin,Stream,Hystrix

    Spring Boot,Spring Cloud,Eureka,Actuator,Spring Boot Admin,Stream,Hystrix 一.Spring Cloud 之 Eureka. 1 ...

  4. spring/spring boot/spring cloud开发总结

    背景        针对RPC远程调用,都在使用dubbo.dubbox等,我们也是如此.由于社区暂停维护.应对未来发展,我们准备尝试新技术(或许这时候也不算什么新技术了吧),选择使用了spring ...

  5. spring cloud + spring boot + springmvc+mybatis分布式微服务云架构

    做一个微服务架构需要的技术整理: View: H5.Vue.js.Spring Tag.React.angularJs Spring Boot/Spring Cloud:Zuul.Ribbon.Fei ...

  6. Spring Boot + Spring Cloud 实现权限管理系统 (Spring Security 版本 )

    技术背景 到目前为止,我们使用的权限认证框架是 Shiro,虽然 Shiro 也足够好用并且简单,但对于 Spring 官方主推的安全框架 Spring Security,用户群也是甚大的,所以我们这 ...

  7. Spring MVC & Boot & Cloud 技术教程汇总(长期更新)

    昨天我们发布了Java成神之路上的知识汇总,今天继续. Java成神之路技术整理(长期更新) 以下是Java技术栈微信公众号发布的关于 Spring/ Spring MVC/ Spring Boot/ ...

  8. Spring Boot + Spring Cloud 实现权限管理系统 后端篇(二十五):Spring Security 版本

    在线演示 演示地址:http://139.196.87.48:9002/kitty 用户名:admin 密码:admin 技术背景 到目前为止,我们使用的权限认证框架是 Shiro,虽然 Shiro ...

  9. spring 和spring cloud 组成

    spring 顶级项目:Spring IO platform:用于系统部署,是可集成的,构建现代化应用的版本平台,具体来说当你使用maven dependency引入spring jar包时它就在工作 ...

  10. spring Boot+spring Cloud实现微服务详细教程第二篇

    上一篇文章已经说明了一下,关于spring boot创建maven项目的简单步骤,相信很多熟悉Maven+Eclipse作为开发常用工具的朋友们都一目了然,这篇文章主要讲解一下,构建spring bo ...

随机推荐

  1. 手机作为蓝牙音频源连接到Linux时,如何通过音量键调节传入的音量大小

    背景一: 我们知道,把手机作为音频源通过蓝牙连接到电脑,就可以把手机的声音转移到电脑上. 背景二: 我喜欢带着耳机用我的Linux本刷youtube,也喜欢用我的iPhone听音乐.为了同时做这两件事 ...

  2. 【学术篇】CF833B TheBakery 分治dp+主席树

    题目の传送门~ 题目大意: 将\(n\)个蛋糕分成恰好\(k\)份, 求每份中包含的蛋糕的种类数之和的最大值. 这题有两种做法. 第一种是线段树优化dp, 我还没有考虑. 另一种就是分治+主席树. 然 ...

  3. 互斥量mutex简介

    互斥量又称互斥锁.互斥量是一个可以处于两态之一的变量:解锁和加锁. 简介 编辑 如果不需要信号量的计数能力,有时可以使用信号量的一个简化版本,称为互斥量(mutex).互斥量仅仅适用于管理共享资源或一 ...

  4. redux combineReducers的用法

    给这种 state 结构写 reducer 的方式是分拆成多个 reducer,拆分之后的 reducer 都是相同的结构(state, action),并且每个函数独立负责管理该特定切片 state ...

  5. Hadoop常用端口和定义方法

    Hadoop集群的各部分一般都会使用到多个端口,有些是daemon之间进行交互之用,有些是用于RPC访问以及HTTP访问.而随着Hadoop周边组件的增多,完全记不住哪个端口对应哪个应用,特收集记录如 ...

  6. php常用函数总结2

    文件系统函数 函数名 描述 实例 输入 输出 操作 fopen() 打开文件或者 URL $handle = fopen("ftp://user:password@example.com/s ...

  7. 分块——cf1207F

    这么傻逼的题当时想了那么久 用a数组维护原序列,b[i][j]表示 pos%i=j 的 a[pos]之和 对于每个修改1 x y,先直接修改a[x],然后枚举i=1..700,修改b[i][x%i] ...

  8. linux中CentOS、Ubuntu、Debian三个版本系统 差别

    Linux有非常多的发行版本,从性质上划分,大体分为由商业公司维护的商业版本与由开源社区维护的免费发行版本. 商业版本以Redhat为代表,开源社区版本则以debian为代表.这些版本各有不同的特点, ...

  9. webpack 添加eslint代码审查

    1). 添加包 npm install eslint --save-dev npm install eslint-loader --save-dev npm install eslint-plugin ...

  10. NX二次开发-UFUN打开本地文本文档uc4504

    NX9+VS2012 #include <uf.h> #include <uf_cfi.h> #include <uf_ui.h> using std::strin ...