spingboot+socketio
- 依赖对于socketio-client 2.x版本
<dependency>
<groupId>com.corundumstudio.socketio</groupId>
<artifactId>netty-socketio</artifactId>
<version>1.7.19</version>
</dependency>
@Component
@RequiredArgsConstructor
@Slf4j
public class ServerRunner implements CommandLineRunner, ApplicationListener<ContextClosedEvent> {
private SocketIOServer server;
private final InetUtils inetUtils;
private final NacosServiceManager nacosServiceManager;
@Value("${socket-io-port:9000}")
private int port;
@Bean
public SocketIOServer server() throws FileNotFoundException {
Configuration config = new Configuration();
int threadNum = Runtime.getRuntime().availableProcessors() * 2;
config.setPort(port);
config.setOrigin("*");
config.setBossThreads(threadNum);
config.setWorkerThreads(threadNum);
config.setMaxFramePayloadLength(1024 * 1024);
config.setMaxHttpContentLength(1024 * 1024);
server = new SocketIOServer(config);
return server;
}
@Bean
public SpringAnnotationScanner springAnnotationScanner(SocketIOServer server) {
return new SpringAnnotationScanner(server);
}
@Override
public void run(String... args) {
server.start();
log.info("启动 SocketIO Server 成功。。。。");
this.registerWithNacos();
}
@Override
public void onApplicationEvent(@NotNull ContextClosedEvent event) {
if (server != null) {
server.stop();
}
}
private void registerWithNacos() {
try {
NamingService naming = nacosServiceManager.getNamingService();
// 注册到nacos
String ip = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress();
Instance instance = new Instance();
instance.setIp(ip);
instance.setPort(port);
instance.setServiceName("socket-io-service");
instance.setWeight(1.0);
Map<String, String> map = new HashMap<>();
// 注册来源标识 默认Nacos
// map.put("preserved.register.source", "SPRING_CLOUD");
instance.setMetadata(map);
naming.registerInstance("socket-io-server", instance);
log.info("注册SocketIO到nacos成功");
} catch (NacosException e) {
throw new RuntimeException("注册SocketIO到nacos失败", e);
}
}
}
spring:
cloud:
gateway:
routes:
- id: socket_io_route
uri: lb://socket-io-server
predicates:
- Path=/socket.io/**
K8S Ingress
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
namespace: xxx
name: xxx-server
annotations:
# 文件上传限制
nginx.ingress.kubernetes.io/proxy-body-size: "200M"
# 代理发送超时
nginx.ingress.kubernetes.io/proxy-send-timeout: "3600"
# 代理读取超时
nginx.ingress.kubernetes.io/proxy-read-timeout: "3600"
# 代理连接超时
nginx.ingress.kubernetes.io/proxy-connect-timeout: "3600"
# 基于客户端出口ip哈希
nginx.ingress.kubernetes.io/upstream-hash-by: "$http_x_forwarded_for"
spec:
ingressClassName: nginx
rules:
- host: www.xxx.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: xxx-server
port:
number: 9000
vue项目nginx代理
server {
listen 80;
gzip on;
gzip_min_length 1k;
gzip_comp_level 9;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary on;
gzip_disable "MSIE [1-6]\.";
server_name localhost;
client_max_body_size 200m;
location / {
add_header Cache-Control "no-cache, no-store";
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
}
location /prod-api/{
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://server/;
proxy_read_timeout 10m;
client_body_timeout 10m;
}
location /socket.io/ {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_pass http://server;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
外部跳板nginx代理
upstream k8singress {
ip_hash;
server 192.168.33.11:31499;
server 192.168.33.12:31499;
}
server {
listen 80;
server_name www.xxx.com;
location / {
proxy_pass http://k8singress/;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}