netty编程之基于websocket实现聊天功能

news2024/9/23 5:17:47

写在前面

源码 。
本文看下netty如何通过websocket实现聊天功能。

类似于实现http server,netty实现websocket也很简单,同样使用对应的编码器和解码器就行了,相关的有HttpServerCodec,HttpObjectAggregator,ChunkedWriteHandler。

1:编码

1.1: netty websocket server

server类:

package com.dahuyou.netty.chat.domain.server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.net.InetSocketAddress;

@Component("nettyServer")
public class NettyServer {

    private Logger logger = LoggerFactory.getLogger(NettyServer.class);

    //配置服务端NIO线程组
    private final EventLoopGroup parentGroup = new NioEventLoopGroup(); //NioEventLoopGroup extends MultithreadEventLoopGroup Math.max(1, SystemPropertyUtil.getInt("io.netty.eventLoopThreads", NettyRuntime.availableProcessors() * 2));
    private final EventLoopGroup childGroup = new NioEventLoopGroup();
    private Channel channel;

    public ChannelFuture bing(InetSocketAddress address) {
        ChannelFuture channelFuture = null;
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(parentGroup, childGroup)
                    .channel(NioServerSocketChannel.class)    //非阻塞模式
                    .option(ChannelOption.SO_BACKLOG, 128)
                    .childHandler(new MyChannelInitializer());

            channelFuture = b.bind(address).syncUninterruptibly();
            channel = channelFuture.channel();
        } catch (Exception e) {
            logger.error(e.getMessage());
        } finally {
            if (null != channelFuture && channelFuture.isSuccess()) {
                logger.info("netty server start done. {}");
            } else {
                logger.error("netty server start error. {}");
            }
        }
        return channelFuture;
    }

    public void destroy() {
        if (null == channel) return;
        channel.close();
        parentGroup.shutdownGracefully();
        childGroup.shutdownGracefully();
    }

    public Channel getChannel() {
        return channel;
    }

}

MyChannelInitializer:

package com.dahuyou.netty.chat.domain.server;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.stream.ChunkedWriteHandler;

import java.nio.charset.Charset;

public class MyChannelInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel channel) {
        channel.pipeline().addLast("http-codec", new HttpServerCodec());
        channel.pipeline().addLast("aggregator", new HttpObjectAggregator(65536));
        channel.pipeline().addLast("http-chunked", new ChunkedWriteHandler());
        // 在管道中添加我们自己的接收数据实现方法
        channel.pipeline().addLast(new MyServerHandler());
    }

}

其中设置了支持websocket相关的编解码器是关键。
MyServerHandler:

package com.dahuyou.netty.chat.domain.server;

import com.alibaba.fastjson.JSON;
import com.dahuyou.netty.chat.pojo.ClientMsgProtocol;
import com.dahuyou.netty.chat.types.util.ChannelHandler;
import com.dahuyou.netty.chat.types.util.MsgUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.handler.codec.http.websocketx.*;
import io.netty.util.CharsetUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MyServerHandler extends ChannelInboundHandlerAdapter {

    private Logger logger = LoggerFactory.getLogger(MyServerHandler.class);

    private WebSocketServerHandshaker handshaker;

    /**
     * 当客户端主动链接服务端的链接后,这个通道就是活跃的了。也就是客户端与服务端建立了通信通道并且可以传输数据
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        SocketChannel channel = (SocketChannel) ctx.channel();
        logger.info("链接报告开始");
        logger.info("链接报告信息:有一客户端链接到本服务端");
        logger.info("链接报告IP:{}", channel.localAddress().getHostString());
        logger.info("链接报告Port:{}", channel.localAddress().getPort());
        logger.info("链接报告完毕");
        ChannelHandler.channelGroup.add(ctx.channel());
    }

    /**
     * 当客户端主动断开服务端的链接后,这个通道就是不活跃的。也就是说客户端与服务端的关闭了通信通道并且不可以传输数据
     */
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        logger.info("客户端断开链接{}", ctx.channel().localAddress().toString());
        ChannelHandler.channelGroup.remove(ctx.channel());
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

        //http
        if (msg instanceof FullHttpRequest) {

            FullHttpRequest httpRequest = (FullHttpRequest) msg;

            if (!httpRequest.decoderResult().isSuccess()) {

                DefaultFullHttpResponse httpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST);

                // 返回应答给客户端
                if (httpResponse.status().code() != 200) {
                    ByteBuf buf = Unpooled.copiedBuffer(httpResponse.status().toString(), CharsetUtil.UTF_8);
                    httpResponse.content().writeBytes(buf);
                    buf.release();
                }

                // 如果是非Keep-Alive,关闭连接
                ChannelFuture f = ctx.channel().writeAndFlush(httpResponse);
                if (httpResponse.status().code() != 200) {
                    f.addListener(ChannelFutureListener.CLOSE);
                }

                return;
            }

            WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory("ws:/" + ctx.channel() + "/websocket", null, false);
            handshaker = wsFactory.newHandshaker(httpRequest);

            if (null == handshaker) {
                WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
            } else {
                handshaker.handshake(ctx.channel(), httpRequest);
            }

            return;
        }

        //ws
        if (msg instanceof WebSocketFrame) {

            WebSocketFrame webSocketFrame = (WebSocketFrame) msg;

            //关闭请求
            if (webSocketFrame instanceof CloseWebSocketFrame) {
                handshaker.close(ctx.channel(), (CloseWebSocketFrame) webSocketFrame.retain());
                return;
            }

            //ping请求
            if (webSocketFrame instanceof PingWebSocketFrame) {
                ctx.channel().write(new PongWebSocketFrame(webSocketFrame.content().retain()));
                return;
            }

            //只支持文本格式,不支持二进制消息
            if (!(webSocketFrame instanceof TextWebSocketFrame)) {
                throw new Exception("仅支持文本格式");
            }

            String request = ((TextWebSocketFrame) webSocketFrame).text();
            System.out.println("服务端收到:" + request);

            ClientMsgProtocol clientMsgProtocol = JSON.parseObject(request, ClientMsgProtocol.class);
            //1请求个人信息
            if (1 == clientMsgProtocol.getType()) {
                ctx.channel().writeAndFlush(MsgUtil.buildMsgOwner(ctx.channel().id().toString()));
                return;
            }
            //群发消息
            if (2 == clientMsgProtocol.getType()) {
                TextWebSocketFrame textWebSocketFrame = MsgUtil.buildMsgAll(ctx.channel().id().toString(), clientMsgProtocol.getMsgInfo());
                ChannelHandler.channelGroup.writeAndFlush(textWebSocketFrame);
            }

        }

    }

    /**
     * 抓住异常,当发生异常的时候,可以做一些相应的处理,比如打印日志、关闭链接
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
        logger.info("异常信息:\r\n" + cause.getMessage());
    }

}

对解码后的websocket消息按照不同的消息类型做了不同的处理,工作中如果有类似需求的话,这里也会是我们写代码的主要地方咯。

1.2: 通过spring boot启动netty websocket server

package com.dahuyou.netty.chat.trigger;

import com.dahuyou.netty.chat.domain.server.NettyServer;
import io.netty.channel.ChannelFuture;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import java.net.InetSocketAddress;

@SpringBootApplication
@ComponentScan("com.dahuyou.netty")
public class NettyApplication implements CommandLineRunner {

    @Value("${netty.host}")
    private String host;
    @Value("${netty.port}")
    private int port;
    @Autowired
    private NettyServer nettyServer;

    public static void main(String[] args) {
        SpringApplication.run(NettyApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        InetSocketAddress address = new InetSocketAddress(host, port);
        ChannelFuture channelFuture = nettyServer.bing(address);
        Runtime.getRuntime().addShutdownHook(new Thread(() -> nettyServer.destroy()));
        channelFuture.channel().closeFuture().syncUninterruptibly();
    }

}

1.3: websocket client

<!--
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
-->
<title>大忽悠</title>

<script type="text/javascript" src="res/js/jquery.min.js"></script>
<script type="text/javascript" src="res/js/jquery.serialize-object.min.js"></script>
<script type="text/javascript" src="res/js/index.js"></script>

<style>
body{
	background-image:url(res/img/1121416_071010Daniel12.jpg);
	background-repeat:no-repeat;
	background-size:cover;
	font-family:”微软雅黑”;
}


#chatDiv{
	position: relative;
	margin:0 auto;
	margin-top:150px;
	width:839px;
	height:667px;
	background-color:#CCCCCC;
	border-radius:3px;-moz-border-radius:3px;
}

</style>

</head>

<body>

<div id="chatDiv">
	<!-- left -->
	<div style="width:60px; height:667px; background-color:#2D2B2A; float:left;">
		<!-- 头像 -->
		<div style="width:35px; height:35px; margin:0 auto; margin-top:19px; margin-left:12px; float:left; border:1px solid #666666;border-radius:3px;-moz-border-radius:3px;">
			<img src="res/img/Snipaste_2024-09-02_14-29-46.png" width="35px" height="35px"/>
		</div>

		<!-- 聊天 -->
		<div style="width:28px; height:28px; margin:0 auto; margin-top:25px; margin-left:16px; float:left;">
			<img src="res/img/chat.png" width="28px" height="28px"/>
		</div>

		<!-- 好友 -->
		<div style="width:28px; height:28px; margin:0 auto; margin-top:20px; margin-left:16px; float:left;">
			<img src="res/img/friend.png" width="28px" height="28px"/>
		</div>

		<!-- 收藏 -->
		<div style="width:28px; height:28px; margin:0 auto; margin-top:20px; margin-left:16px; float:left;">
			<img src="res/img/collection.png" width="28px" height="28px"/>
		</div>

		<!-- 设置 -->
		<div style="width:20px; height:20px; margin:0 auto; margin-left:20px; float:left; position:absolute;bottom:0; margin-bottom:12px;">
			<img src="res/img/set.png" width="20px" height="20px"/>
		</div>

	</div>

	<!-- center -->
	<div style="width:250px; height:667px; background-color:#EBE9E8; float:left;">
		<div style=" background-image:url(res/img/search.png); background-repeat:no-repeat;margin:0 auto; margin-top:25px; padding-top:5px; padding-bottom:5px; width:210px; background-color:#DBD9D8;border-radius:3px;-moz-border-radius:3px; float:left; margin-left:20px; font-size:12px; color:#333333;text-indent:27px;">
			找呀找呀找朋友!!!
		</div>

		<!-- friendList -->
		<div id="friendList" style="float:left; margin-top:5px;">
			<div style="width:250px; height:65px;">
				<table style="width:240px; height:60px; margin:0 auto; margin-top:2px; background-color:#E5E5E5;">
					<tr>
						<td rowspan="2" width="50"><img src="res/img/2487997a9f274d86a61a8dffff3e0070_wang0907.jpg" width="50px" height="50px" style="border-radius:3px;-moz-border-radius:3px;"/></td>
						<td style="color:#333333; text-indent:5px; font-size:12px; vertical-align:bottom; font-weight:bolder;">大忽悠</td>
					</tr>
					<tr>
						<td style="color:#999999; text-indent:5px; font-size:10px;">你好,这里是大忽悠有限责任公司</td>
					</tr>
				</table>
			</div>
		</div>

	</div>


	<!-- chat -->
	<div id="chat" style="width:529px; height:667px; background-color:#F5F5F5; float:right;">
		<div style="width:16px; height:16px; background-image:url(res/img/exit.png); background-repeat:no-repeat; float:right; margin-top:10px; margin-right:30px;"></div>
		<div style="width:16px; height:16px; background-image:url(res/img/min.png); background-repeat:no-repeat; float:right; margin-top:10px; margin-right:12px;"></div>
		<div style="border-bottom:1px #E7E7E7 solid;width:509px; padding-top:0px; padding-left:20px; padding-bottom:10px; font-size:18px; font-weight:bolder;float:left;">
			大忽悠(6666666)
		</div>

		<!-- 会话区域 begin -->
		<div id="show" style="width:529px; height:450px; float:left;overflow-y:scroll;">

			<!-- 消息块;系统 -->
			<div class="msgBlockSystem" style="margin-left:30px; margin-top:15px; width:340px; height:auto; margin-bottom:15px; float:left;">
				<div class="msgBlock_userHeadImg" style="float:left; width:35px; height:35px;border-radius:3px;-moz-border-radius:3px; background-color:#FFFFFF;">
					<img class="point" src="res/img/Snipaste_2024-09-02_14-29-46.png" width="35px" height="35px" style="border-radius:3px;-moz-border-radius:3px;"/>
				</div>

				<div class="msgBlock_channelId" style="float:left; width:100px; margin-top:-5px; margin-left:10px; padding-bottom:2px; font-size:10px;">
					大忽悠
				</div>

				<div class="msgBlock_msgInfo" style="height:auto;width:280px;float:left;margin-left:12px; margin-top:4px;border-radius:3px;-moz-border-radius:3px; ">
					<div style="width:4px; height:20px; background-color:#CC0000; float:left;border-radius:3px;-moz-border-radius:3px;"></div>
					<div class="point" style="float:left;width:260px; padding:7px; background-color:#FFFFFF; border-radius:3px;-moz-border-radius:3px; height:auto; font-size:12px;display:block;word-break: break-all;word-wrap: break-word;">
						这里是大忽悠有限责任公司,你可别被我忽悠了<hr/><img width="260" height="260" src="res/img/2487997a9f274d86a61a8dffff3e0070_wang0907.jpg" />
					</div>
				</div>
			</div>

			<!-- 消息块;好友 -->
			<div class="msgBlockFriendClone" style=" display:none; margin-left:30px; margin-top:15px; width:340px; height:auto; margin-bottom:15px; float:left;">
				<div class="msgBlock_userHeadImg" style="float:left; width:35px; height:35px;border-radius:3px;-moz-border-radius:3px; background-color:#FFFFFF;">
					<img class="headPoint" src="res/img/head5.jpg" width="35px" height="35px" style="border-radius:3px;-moz-border-radius:3px;"/>
				</div>

				<div class="msgBlock_channelId" style="float:left; width:100px; margin-top:-5px; margin-left:10px; padding-bottom:2px; font-size:10px;">
					<!-- 名称 -->
				</div>

				<div class="msgBlock_msgInfo" style="height:auto;width:280px;float:left;margin-left:12px; margin-top:4px;border-radius:3px;-moz-border-radius:3px; ">
					<div style="width:4px; height:20px; background-color:#CC0000; float:left;border-radius:3px;-moz-border-radius:3px;"></div>
					<div class="msgPoint" style="float:left;width:260px; padding:7px; background-color:#FFFFFF; border-radius:3px;-moz-border-radius:3px; height:auto; font-size:12px;display:block;word-break: break-all;word-wrap: break-word;">
						<!-- 信息 -->
					</div>
				</div>
			</div>

			<!-- 消息块;自己 -->
			<div class="msgBlockOwnerClone" style=" display:none; margin-right:30px; margin-top:15px; width:340px; height:auto; margin-bottom:15px; float:right;">

				<div style="float:right; width:35px; height:35px;border-radius:3px;-moz-border-radius:3px; background-color:#FFFFFF;">
					<img class="headPoint" src="res/img/head3.jpg" width="35px" height="35px" style="border-radius:3px;-moz-border-radius:3px;"/>
				</div>

				<div class="msgBlock_msgInfo" style="height:auto;width:280px;float:left;margin-left:12px; margin-top:4px;border-radius:3px;-moz-border-radius:3px; ">
					<div class="msgPoint" style="float:left;width:260px; padding:7px; background-color:#FFFFFF; border-radius:3px;-moz-border-radius:3px; height:auto; font-size:12px;display:block;word-break: break-all;word-wrap: break-word;">
						<!-- 信息 -->
					</div>
					<div style="width:4px; height:20px; background-color:#CC0000; float:right;border-radius:3px;-moz-border-radius:3px;"></div>
				</div>

			</div>

			<span id="msgPoint"></span>

		</div>
		<!-- 会话区域 end -->
		<div style="width:100%; height:2px; float:left; background-color:#CCCCCC;"></div>
		<div style="margin:0 auto; width:100%; height:149px; margin-top:5px;  background-color:#FFFFFF; float:left;">
			<textarea id="sendBox" style="font-size:14px; border:0; width:499px; height:80px; outline:none; padding:15px;font-family:”微软雅黑”;resize: none;"></textarea>

			<div style="margin-top:20px; float:right; margin-right:35px; padding:5px; padding-left:15px; padding-right:15px; font-size:12px; background-color:#F5F5F5;border-radius:3px;-moz-border-radius:3px; cursor:pointer;" onclick="javascript:util.send();">发送(S)</div>
		</div>
	</div>

</div>


</body>
</html>

2:测试

运行:
在这里插入图片描述
在这里插入图片描述
发送一个消息:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
酱!!!

写在后面

参考文章列表

idea中的html到浏览器中的乱码问题解决方案 。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2100009.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

已解决:VS2022启动闪退,错误模块名称: clr.dll,版本: 4.8.9261.0,时间戳: 0x667a1925的问题

本问题已得到解决&#xff0c;请看以下小结&#xff1a; 关于《VS2022启动闪退》的解决方案 记录备注报错时间2024年报错版本VS2022报错复现下载某款VPN软件后&#xff0c;打开VS2022闪退&#xff0c;事件查看器输出如下报错描述错误应用程序名称: devenv.exe&#xff0c;版本:…

DAY 2 - 3 : 线性表—顺序存储

线性表—顺序表 问题引入&#xff1a; 线性表 定义 若干数据元素的一个线性序列。 表示 L (D,R) (即线性表L包含数据元素集合D和关系集合R&#xff09; D{ ai | ai∈datatype ,i0,1,2...n-1 ,n≥0} R{ <ai,ai1> | ai,ai1∈D, 0 ≤ i ≤ n - 2} < ai,ai1 >在这里称…

数据结构代码集训day14(适合考研、自学、期末和专升本)

题目均来自b站up&#xff1a;白话拆解数据结构&#xff01; 今日题目如下&#xff1a;&#xff08;1&#xff09;试写一个算法判断给定字符序列是否是回文。 &#xff08;2&#xff09;给定一个算法判断输入的表达式中括号是否匹配。假设只有花、中、尖三种括号。 题1 回文序列…

学习笔记 | 一文搞懂MySQL体系架构!!!(day22)

本文章的内容会在后面文章中慢慢讲解&#xff0c;该文章主要给各位博友zaipin提供学习思路&#xff0c;也希望大家在评论区发言表述&#xff0c;觉得文章有不足指出也可点评&#xff0c;希望大家多多支持&#xff01;&#xff01;&#xff01; 目录 一、MySQL 1.1 数据库概述 …

【项目日记】高并发内存池---实现页缓存

放纵自己的欲望是最大的祸害&#xff1b; 谈论别人的隐私是最大的罪恶&#xff1b; 不知自己过失是最大的病痛。 --- 亚里士多德 --- 高并发内存池---实现页缓存 1 页缓存整体设计思路2 框架搭建3 NewSpan函数4 请求Span联动 1 页缓存整体设计思路 首先我们来看页缓存的设…

windows手工杀毒-寻找可疑进程之进程名称

上篇回顾&#xff1a;windows手工杀毒-寻找可疑进程之进程图标-CSDN博客 上篇中我们简单介绍了什么是电脑病毒&#xff0c;也介绍了一种发现可疑进程的方法即根据进程图标确认是否是病毒&#xff0c;这种方法存在的理论基础是&#xff0c;通过图标可以很容易在电脑上找…

遥控器新手操作指南!!!

一、准备工作 检查电量&#xff1a;确保无人机和遥控器的电池电量充足&#xff0c;以避免在飞行过程中因电量不足而导致意外。 安装与连接&#xff1a;确保无人机的螺旋桨安装正确且牢固&#xff0c;同时检查无人机存储卡是否插入&#xff0c;以及遥控器与无人机之间的连接是…

论文笔记:2023顶会SIGIR - Strategy-aware Bundle Recommender System

论文笔记&#xff1a;2023顶会SIGIR - Strategy-aware Bundle Recommender System

【位运算】--- 初阶题目赏析

Welcome to 9ilks Code World (๑•́ ₃ •̀๑) 个人主页: 9ilk (๑•́ ₃ •̀๑) 文章专栏&#xff1a; 算法Journey 根据上一篇位运算的总结&#xff0c;我们来体会几道初阶题目。 &#x1f3e0; 判定字符是否唯一 &#x1f4cc; 题目解析 判定字符是否唯一…

通义千问AI PPT初体验:一句话、万字文档、长文本一键生成PPT!

大家好&#xff0c;我是木易&#xff0c;一个持续关注AI领域的互联网技术产品经理&#xff0c;国内Top2本科&#xff0c;美国Top10 CS研究生&#xff0c;MBA。我坚信AI是普通人变强的“外挂”&#xff0c;专注于分享AI全维度知识&#xff0c;包括但不限于AI科普&#xff0c;AI工…

leecode刷题经典算法套路模版笔记【递归回溯篇】--根本逻辑,快速掌控

刷题套路总结&#xff1a; 双指针&#xff1a; 单调性&#xff1b;对两端按照规律进行操作移动&#xff1b; 常见移法&#xff0c;右指针右移扩大范围&#xff0c;左指针左移缩小范围&#xff1b; 先举例模拟&#xff0c;然后推导公式&#xff1b; 递归&#xff0c;回溯 &am…

线程间同步的方式有哪些?

Linux 系统提供了五种用于线程间同步的方式&#xff1a;互斥锁、读写锁、自旋锁、信号量、条件变量 互斥锁 主要用于保护共享数据&#xff0c;确保同一时间内只有一个线程访问数据。 互斥量本质上来说就是一把锁&#xff0c;在访问共享资源前对互斥量进行加锁&#xff0c;访…

【go-zero】win启动rpc服务报错 panic: context deadline exceeded

win启动rpc服务报错 panic: context deadline exceeded 问题来源 在使用go-zero生成的rpc项目后 启动不起来 原因 这个问题原因是wndows没有启动etcd 官方文档是删除了etcd配置 而我自己的测试yaml配置有etcd&#xff0c;所以需要启动etcd 下载安装好etcd后&#xff0…

Java Full GC 的常见原因及优化策略

Java Full GC 的常见原因及优化策略 1、导致Full GC的常见原因1.1 新生代设置过小1.2 新生代设置过大1.3 Survivor区设置不当 2、优化GC策略2.1 吞吐量优先2.2 暂停时间优先 3、结论 &#x1f496;The Begin&#x1f496;点点关注&#xff0c;收藏不迷路&#x1f496; 在Java应…

自控原理-传递函数(闭环 扰动 偏差 前馈 复合 顺馈)

都知道闭环传递函数定义为&#xff1a;G1G2/(1G1G2H) 但是当碰到复杂的系统&#xff0c;比如复合顺馈&#xff0c;前馈扰动等&#xff0c;就不知道分子到底要不要乘上G2了。 这个公式是如何推导出来的&#xff0c;今天看到一个公式图片&#xff1a; 过程非常详细。 由此我也…

C语言遇见的一些小问题

问题如下&#xff1a; 1&#xff1a;为什么这样的代码为报错 #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <algorithm> #include <cstdio> #include<string> #include<stdlib.h> using namespace std; int main() {int i …

C语言 ——— #define定义标识符

目录 #define 定义常变量 #define 定义字符串 #define 定义一条代码 #define 定义的标识符是否需要加分号 #define 定义常变量 代码演示&#xff1a; #define M 100 //定义常变量 代码用途&#xff1a; int a M; int arr[M] { 0 }; 此时的 M 具有常属性&#xff0c…

什么是UART?

1.什么是UART&#xff1f; 通用异步收发传输器&#xff08;Universal Asynchronous Receiver/Transmitter)&#xff0c;通常称作UART。UART 表示通用异步接收机/发射机&#xff0c;定义用于在两个设备之间交换串行数据的协议或一组规则。UART 非常简单&#xff0c;仅在发射机和…

C语言中static与extern关键字的深入解析

在C语言编程中&#xff0c;static和extern是两个非常重要的关键字&#xff0c;它们各自有着独特的用途。本文将深入探讨这两个关键字的工作原理、底层实现机制以及在实际开发中的应用。 static关键字 1. 原理与作用 static关键字用于声明变量或函数具有特定的作用域和生命周…

5.4分段线性灰度变换

目录 实验原理 分段线性灰度变换的概念 变换函数的形式 示例代码1 示例结果1 示例代码2 示例结果2 示例代码3 运行结果3 示例代码4 运行结果4 实验原理 在OpenCV中&#xff0c;分段线性灰度变换&#xff08;Piecewise Linear Gray Level Transformation&#xff09…