admin管理员组文章数量:1608811
错误:
1.ioty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: no further information: /127.0.0.1:8080
第一个错是没有启动 server 端
2.Caused by: java.ConnectException: Connection refused: no further information:/127.0.0.1:8080
代码:
public void bind(int port) {
EventLoopGroup mainGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(mainGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 1024)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(new NettyHeartbeatServerHandler());
}
});
ChannelFuture future = serverBootstrap.bind(port).sync();
/**这没有加sync()方法造成的*/
future.channel().closeFuture();
} catch (Exception e) {
e.printStackTrace();
} finally {
mainGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
3.ioty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: no further information: /127.0.0.1:8080
public void bind(int port) {
EventLoopGroup mainGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(mainGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 1024)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(new NettyHeartbeatServerHandler());
}
});
/***模拟的一个异常**/
int i = 10/0;
ChannelFuture future = serverBootstrap.bind(port).sync();
future.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
} finally {
mainGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
/这里是我的代码报错点,netty-all 4.1.50Final 仅参照***/
~ server端
public void bind(int port) {
EventLoopGroup mainGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(mainGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 1024)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(new NettyHeartbeatServerHandler());
}
});
ChannelFuture future = serverBootstrap.bind(port).sync();
future.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
} finally {
mainGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
~ protobuf
pom.xml
<!--netty -->
<!-- https://mvnrepository.com/artifact/io.netty/netty-all -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.50.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.protobuf/protobuf-java -->
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.12.2</version>
</dependency>
<!--netty -->
安装windows - protoc
下载 : https://github/google/protobuf/releases
1.解压protoc-3.12.3-win64.zip
2.配置环境变量:F:\pro\protoc-3.12.3-win64\bin
3.将protoc.exe拷贝到C:\Windows\System32
4.在cmd下执行 protoc --version 或 protoc --help查看
创建UserProto.proto
syntax = "proto2";
package protocol;
option java_package = "com.jxfj.protoful"; //记得改成你的java文件生成路径
option java_outer_classname = "UserProto";
message UserProtocol {
required string id = 1;
required string name = 2;
}
这个文件放置位置,找得到就好,就好。
在cmd或idea terminal下 执行:
protoc --java_out=./netty/src/main/java ./netty/protobuf/UserProto.proto
// ./netty/src/main/jav 生成java的package位置
// ./netty/protobuf/UserProto.proto 上图给你画红框的位置
生成 Useproto.java完成。
~~ 好好奇下面这个类怎么生成的 特别是心跳协议部分~~
怎么做啊?
package com.snowcattle.game.message.auto.http.client;
public final class OnlineClientHttpProBuf {
private OnlineClientHttpProBuf() {
}
public static void registerAllExtensions(
com.google.protobuf.ExtensionRegistryLite registry) {
}
public static void registerAllExtensions(
com.google.protobuf.ExtensionRegistry registry) {
registerAllExtensions(
(com.google.protobuf.ExtensionRegistryLite) registry);
}
public interface OnlineHeartClientHttpProBufOrBuilder extends
// @@protoc_insertion_point(interface_extends:OnlineHeartClientHttpProBuf)
com.google.protobuf.MessageOrBuilder {
/**
* <code>required int32 id = 1;</code>
*/
boolean hasId();
/**
* <code>required int32 id = 1;</code>
*/
int getId();
}
/**
* <pre>
*心跳协议
* </pre>
*
* Protobuf type {@code OnlineHeartClientHttpProBuf}
*/
public static final class OnlineHeartClientHttpProBuf extends
com.google.protobuf.GeneratedMessageV3 implements
// @@protoc_insertion_point(message_implements:OnlineHeartClientHttpProBuf)
OnlineHeartClientHttpProBufOrBuilder {
// Use OnlineHeartClientHttpProBuf.newBuilder() to construct.
private OnlineHeartClientHttpProBuf
版权声明:本文标题:netty 学习入门 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dongtai/1728550014a1163307.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论