This commit is contained in:
crusader 2018-04-24 02:22:23 +09:00
parent eb46396d4d
commit ce575853d5
4 changed files with 151 additions and 4 deletions

2
.gitignore vendored
View File

@ -82,7 +82,7 @@ hs_err_pid*
.idea/
*.iml
/target/
target/
.settings/
.classpath
.project

View File

@ -0,0 +1,64 @@
package com.loafle.overflow.container.configuration;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.Arrays;
import java.util.List;
import com.loafle.commons.server.Server;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.ServerChannel;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
/**
* ContainerConfiguration
*/
@Configuration
public class ContainerConfiguration {
@Autowired
private List<ChannelHandler> pipelineChannelHandlers;
@Bean
public List<Class<? extends ServerChannel>> channelClasses() {
return Arrays.asList(NioServerSocketChannel.class);
}
@Bean
public List<Server.ChannelOptionItem<?>> channelOptions() {
return Arrays.asList(new Server.ChannelOptionItem<>(ChannelOption.SO_BACKLOG, 100));
}
@Bean
public List<ChannelHandler> channelHandlers() {
return Arrays.asList(new LoggingHandler(LogLevel.INFO));
}
@Bean
public ChannelInitializer<?> channelInitializer() {
return new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline cp = ch.pipeline();
for (ChannelHandler channelHandler : pipelineChannelHandlers) {
cp.addLast(channelHandler);
}
}
};
}
@Bean
public SocketAddress address() {
return new InetSocketAddress("127.0.0.1", 60000);
}
}

View File

@ -0,0 +1,79 @@
package com.loafle.overflow.container.server;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.annotation.Resource;
import com.loafle.commons.server.Server;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
/**
* ContainerServer
*/
public class ContainerServer extends Server {
@Resource
private String pidFilePath;
private int portNumber;
public ContainerServer() {
}
protected ChannelFuture bind(ServerBootstrap serverBootstrap) throws Exception {
ChannelFuture cf = null;
for (int i = 60000; i < 61000; i++) {
try {
cf = serverBootstrap.bind("127.0.0.1", i).sync();
this.portNumber = i;
break;
} catch (Exception e) {
System.out.println(e);
continue;
}
}
if (null == cf) {
throw new Exception("There is not available port");
}
try {
FileOutputStream outputStream = new FileOutputStream(this.pidFilePath);
outputStream.write(Integer.toString(this.portNumber).getBytes());
outputStream.close();
} catch (IOException e) {
throw e;
}
return cf;
}
@Override
protected void init() throws Exception {
}
@Override
protected void onStart() throws Exception {
}
@Override
protected void onStop() throws Exception {
try {
File f = new File(this.pidFilePath);
f.delete();
} catch (Exception e) {
throw e;
}
}
@Override
protected void destroy() throws Exception {
}
}

View File

@ -1,14 +1,15 @@
package com.loafle.overflow.container.server;
package com.loafle.overflow.container.server.handler;
import com.loafle.commons.rpc.RPCException;
import com.loafle.commons.rpc.protocol.RPCServerCodec;
import com.loafle.commons.rpc.protocol.RPCServerRequestCodec;
import com.loafle.commons.rpc.registry.RPCInvoker;
import com.loafle.commons.server.socket.netty.handler.codec.SocketFrame;
import com.loafle.commons.server.socket.netty.handler.codec.TextSocketFrame;
import com.loafle.commons.server.socket.handler.codec.SocketFrame;
import com.loafle.commons.server.socket.handler.codec.TextSocketFrame;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
@ -18,7 +19,10 @@ import io.netty.channel.SimpleChannelInboundHandler;
*/
public class RPCServerHandler extends SimpleChannelInboundHandler<SocketFrame> {
private static final Logger logger = LoggerFactory.getLogger(RPCServerHandler.class);
@Autowired
private RPCServerCodec serverCodec;
@Autowired
private RPCInvoker rpcInvoker;
public RPCServerHandler(RPCServerCodec serverCodec, RPCInvoker rpcInvoker) {