kafka跨域通信

kafka跨域通信

kafka生产者 -> 前置机 -> kafka消费者

1.kafka生产者 在docker容器内部

2.kafka消费者 在内网,kafka 组件 nat 到外部网络时,生产者、消费者需要通过域名进行访问

  1. 配置hosts域名解析

kafka生产者在docker容器内部,所以hosts映射地址也需要配置到容器内部

# 进docker容器配置
docker exec -it dsc-producer bash

# 配置域名解析
vim /etc/hosts
	# 前置机IP   消费者主机域名
	192.**.0.1   CONSUMER-KAFKA-1


方案一:Nginx代理转发

在前置机上配置nginx转发kafka消息,默认使用http模块做配置转发,nginx不能识别消息;

  1. 必须安装stream模块来支持kafka的消息转发:
# 查看当前nginx配置模块,Nginx没有安装stream模块
	nginx -V
	
# 搜索stream模块
	yum search nginx-mod-stream
	
# 安装epel资源包 为CentOS、Scientific Linux等提供高质量软件包的项目。装上了epel之后,就相当于添加了一个第三方源
	yum -y install epel-release
	
# 安装stream模块
	yum -y install nginx-mod-stream
	
# 验证
	ls /usr/lib64/nginx/modules
# ngx_http_xslt_filter_module.so  ngx_mail_module.so ngx_stream_module.so

  1. 配置nginx反向代理:

vim nginx.conf

http {

}
# 加载stream模块
include /usr/share/nginx/modules/*.conf;
#load_module /usr/lib64/nginx/modules/ngx_stream_module.so;

# 反向代理转发Kafka消息 与http同级
stream{
	# stream日志配置
	log_format main
				'{"@timestamp": "$time_iso8601",'
				'"bytes_received":"$bytes_received",'
				'"bytes_sent":"$bytes_sent",'
				'"protocol":"$protocol",'
				'"status": "$status",'
				'"client_ip": "$remote_addr",'
				'"client_port": "$remote_port",'
				'"upstream_addr": "$upstream_addr"}';
	# 日志路径
    access_log /var/log/nginx/stream-access.log main;

    server {
	    # 监听9092端口
	    listen 9092;
		# 转发目标
	    proxy_pass kafka9092;
	    # 超时时间
	    proxy_connect_timeout 10s;
    }
    upstream kafka9092{
    	# 消费者 ip:port
	    server 192.**.0.1:9092;
    }

}
  1. 验证配置,重启Nginx
# 验证
nginx -t

# 重新装载配置
nginx -s reload


方案二:防火墙端口转发

语法: firewalld-cmd --permanent --zone=<区域> --add-forward-port=port=<源端口号>:proto=<协议>:toport=<目标端口号>:toaddr=<目标IP地址>

生产者-> 前置机 ->消费者: 在前置机上配置防火墙转发

  1. 开启防火墙转发配置
# 将配置写入sysctl.conf文件
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf

# 加载配置文件 sysctl -p /etc/sysctl.conf
sysctl -p 

  1. 初始化防火墙
# 查看防火墙状态
systemctl status firewalld

# 开启防火墙
systemctl start firewalld

# 关闭防火墙
# systemctl stop firewalld

# 开机自启动防火墙
systemctl enable firewalld

# 开机禁用防火墙
# systemctl disable firewalld

# 重启防火墙
# systemctl restart firewalld

  1. 开启 IP 伪装
# 查看防火墙状态
firewall-cmd --state
firewall-cmd --list-all

# 添加IP伪装
firewall-cmd --add-masquerade --zone=public --permanent

# 删除IP伪装
# firewall-cmd --remove-masquerade --zone=public --permanent

firewall-cmd --reload

firewall-cmd --list-all

  1. 添加端口转发
firewall-cmd --list-all

# 监听本机9092端口,转发到192.**.0.1:90服务器端口上
firewall-cmd --add-forward-port=port=9092:proto=tcp:toport=90:toaddr=192.**.0.1 --zone=public --permanent

# 监听9092,转发到本机90端口
# firewall-cmd --add-forward-port=port=9092:proto=tcp:toport=90 --zone=public --permanent

# 删除端口转发
# firewall-cmd --remove-forward-port=port=9092:proto=tcp:toport=90:toaddr=192.**.0.1 --zone=public --permanent

firewall-cmd --reload

firewall-cmd --list-all

  • 9
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值