This commit is contained in:
wujiawei 2023-03-02 23:24:26 +08:00
parent 04f0524971
commit 4e93baf49c
13 changed files with 101 additions and 16 deletions

View File

@ -4,8 +4,8 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ClientApplication {
public class LazyProxyClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
SpringApplication.run(LazyProxyClientApplication.class, args);
}
}

View File

@ -3,6 +3,7 @@ package com.lazy.netty.proxy.client.proxy;
import com.lazy.netty.proxy.client.proxy.config.ClientProxyConfigurationProperties;
import com.lazy.netty.proxy.client.proxy.netty.Constant;
import com.lazy.netty.proxy.client.proxy.netty.ProxySocket;
import jakarta.annotation.PostConstruct;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
@ -18,9 +19,9 @@ public class NettyProxyAutoConfiguration {
this.clientProxyConfigurationProperties = clientProxyConfigurationProperties;
}
@Bean
public Thread xx() {
return new Thread(() -> {
@PostConstruct
public void xx() {
new Thread(() -> {
Constant.serverIp = clientProxyConfigurationProperties.getServerIp();
Constant.serverPort = clientProxyConfigurationProperties.getServerPort();
Constant.realPort = serverProperties.getPort();
@ -32,7 +33,7 @@ public class NettyProxyAutoConfiguration {
e.printStackTrace();
}
});
}).run();
}
}

View File

@ -1,13 +1,13 @@
package com.lazy.netty.proxy.client.proxy.config;
import com.lazy.netty.proxy.msg.constant.ProxyConfigConstant;
import lombok.Data;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Data
@Configuration
@ConfigurationProperties(prefix = "spring.lazy.proxy")
@ConfigurationProperties(prefix = ProxyConfigConstant.PREFIX)
public class ClientProxyConfigurationProperties {

View File

@ -1,6 +1,7 @@
package com.lazy.netty.proxy.client.proxy.netty;
import com.luck.msg.MyMsg;
import com.lazy.netty.proxy.msg.MyMsg;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;

View File

@ -1,6 +1,7 @@
package com.lazy.netty.proxy.client.proxy.netty;
import com.luck.msg.MyMsg;
import com.lazy.netty.proxy.msg.MyMsg;
import com.lazy.netty.proxy.msg.MyMsgDecoder;
import com.lazy.netty.proxy.msg.MyMsgEncoder;
import io.netty.bootstrap.Bootstrap;

View File

@ -1,6 +1,7 @@
package com.lazy.netty.proxy.client.proxy.netty;
import com.luck.msg.MyMsg;
import com.lazy.netty.proxy.msg.MyMsg;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;

View File

@ -0,0 +1,5 @@
package com.lazy.netty.proxy.msg.constant;
public class ProxyConfigConstant {
public static final String PREFIX ="spring.lazy.proxy";
}

View File

@ -1,6 +1,7 @@
package com.lazy.netty.proxy.server;
import com.luck.msg.MyMsg;
import com.lazy.netty.proxy.msg.MyMsg;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;

View File

@ -4,8 +4,8 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ProxyServerApplication {
public class LazyProxyServerApplication {
public static void main(String[] args) {
SpringApplication.run(ProxyServerApplication.class,args);
SpringApplication.run(LazyProxyServerApplication.class,args);
}
}

View File

@ -1,6 +1,7 @@
package com.lazy.netty.proxy.server;
import com.luck.msg.MyMsg;
import com.lazy.netty.proxy.msg.MyMsg;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;

View File

@ -0,0 +1,24 @@
package com.lazy.netty.proxy.server.config;
import com.lazy.netty.proxy.msg.constant.ProxyConfigConstant;
import lombok.Data;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Data
@Configuration
@ConfigurationProperties(prefix = ProxyConfigConstant.PREFIX)
public class ServerProxyConfigurationProperties {
/**
* 服务代理端口 通过该端口访问客户端服务端口
* <p>
* 是否可以将此端口与如下端口融合通过请求路径进行转发
*
* @see ServerProperties#getPort()
*/
private int visitorPort = 16002;
}

View File

@ -0,0 +1,46 @@
package com.lazy.netty.proxy.server.netty;
import com.lazy.netty.proxy.server.Constant;
import com.lazy.netty.proxy.server.ServerSocket;
import com.lazy.netty.proxy.server.VisitorSocket;
import com.lazy.netty.proxy.server.config.ServerProxyConfigurationProperties;
import jakarta.annotation.PostConstruct;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
@Component
public class NettyServerProxyAutoConfiguration {
private final ServerProperties serverProperties;
private final ServerProxyConfigurationProperties serverProxyConfigurationProperties;
public NettyServerProxyAutoConfiguration(ServerProperties serverProperties, ServerProxyConfigurationProperties serverProxyConfigurationProperties) {
this.serverProperties = serverProperties;
this.serverProxyConfigurationProperties = serverProxyConfigurationProperties;
}
@PostConstruct
public void xx() {
new Thread(() -> {
Constant.visitorPort = serverProxyConfigurationProperties.getVisitorPort();
Constant.serverPort = serverProperties.getPort();
// 启动访客服务端用于接收访客请求
try {
VisitorSocket.startServer();
} catch (Exception e) {
e.printStackTrace();
}
// 启动代理服务端用于接收客户端请求
try {
ServerSocket.startServer();
} catch (Exception e) {
e.printStackTrace();
}
}).run();
}
}

View File

@ -1,2 +1,6 @@
server:
port: 1003
port: 16001
spring:
lazy:
proxy: 16002