关于微服务的搭建,大家可以参考前面几篇。
注册中心
配置中心
微服务
路由中心
今天,搭建一个简单的消息中间件Kafka.
一, zookeeper:
1 下载
http://zookeeper.apache.org/releases.html#download
2 修改配置
将“zoo_sample.cfg”重命名为“zoo.cfg”
设置dataDir为自己的路径
添加admin.serverPort=xxx端口
添加系统变量:ZOOKEEPER_HOME=F:\apache-zookeeper-3.6.1-bin;
path系统变量:添加路径:%ZOOKEEPER_HOME%\bin;
3 运行
在目录中,启动zkserver
二,kafka
1 下载
http://kafka.apache.org/downloads
2 修改配置
找到config文件夹下的:server.properties
添加:zookeeper.connect=localhost:2181
log.dirs=tmp/kafka-logs
kafka 默认 9092 端口
3 运行
目录下,运行:> bin\windows\kafka-server-start.bat config\server.properties
PS:
在Windows上,启动Zookeeper和Kafka
> bin\windows\zookeeper-server-start.bat config\zookeeper.properties (zkserver)
> bin\windows\kafka-server-start.bat config\server.properties
三,新建kafka服务
1 新建
如何新建微服务,参考之前的文章。
2 bootstrap.yml(参考 配置中心)
spring:
http:
encoding:
charset: UTF-8
enabled: true
force: true
cloud:
config:
uri: http://${host:localhost}:9020
name: config
profile: ${active:dev}
3 application.yml (参考 配置中心)
server:
port: 9900
eureka:
client:
service-url:
defaultZone: ${registry.url}
instance:
lease-expiration-duration-in-seconds: 60
lease-renewal-interval-in-seconds: 30
preferIpAddress: true
instanceId: ${spring.cloud.client.ip-address}:${server.port}
spring:
application:
name: testkafka
cloud:
stream:
kafka:
binder:
brokers: localhost:9092
bindings:
test-in: #TestStream 中 INPUT
destination: testkafka
contentType: application/json
test-out: #TestStream 中 OUTPUT
destination: testkafka
contentType: application/json
4 KafkaApplication
@SpringBootApplication
@EnableEurekaClient
public class KafkaApplication {
public static void main(String[] args) {
SpringApplication.run(KafkaApplication.class, args);
}
}
5 TestStream
package com.test.kafka.controller;
import org.springframework.cloud.stream.annotation.Input;
import org.springframework.cloud.stream.annotation.Output;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.SubscribableChannel;
/**
* @author Tyler
* @date 2020/7/28
*/
public interface TestStream {
String INPUT = "test-in";
String OUTPUT = "test-out";
@Input(INPUT)
SubscribableChannel testIn();
@Output(OUTPUT)
MessageChannel testOut();
}
6 StreamReceiver
package com.test.kafka.controller;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.stereotype.Component;
/**
* @author Tyler
* @date 2020/7/28
*/
@Component
@EnableBinding(value = {TestStream.class})
public class StreamReceiver {
@StreamListener(TestStream.INPUT)
public void receive(String message) {
System.out.println("StreamReceiver: "+message);
}
}
7 SendController
package com.test.kafka.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author Tyler
* @date 2020/7/29
*/
@RestController
@RequestMapping("api/")
public class SendController {
@Autowired
private TestStream testStream;
@GetMapping("send")
public void send() {
// System.out.println("Hello World...");
testStream.testOut().send(MessageBuilder.withPayload("Hello World...").build());
}
}
8 POM文件
主要:
spring-boot-starter-web
spring-cloud-starter-netflix-eureka-client
spring-cloud-config-client
spring-cloud-starter-stream-kafka
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.3.2.RELEASE
com.test
kafka
0.0.1-SNAPSHOT
kafka
Demo project for Spring Boot
1.8
Hoxton.SR6
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-config-client
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
org.springframework.cloud
spring-cloud-starter-stream-kafka
2.0.1.RELEASE
org.springframework
spring-web
5.2.4.RELEASE
compile
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
四,运行
1 启动 zookeeper
2 启动 kafka
3 启动注册中心,配置中心
4 启动kafka服务
5 浏览:http://localhost:9900/api/send
6 控制台