【Java】【请你喝两杯咖啡50】springboot集成netty-socketio在本地测试正常,但部署在阿里云失败

在本地测试是正常的,

尝试一:

部署在阿里云上后端貌似只能设置本地IP,127.0.0.1或者localhost,设置公网IP或私网IP都报错
019-12-23 20:53:27.867 ERROR 2019-12-23 20:53:27.867 ERROR com.corundumstudio.socketio.SocketIOServer - SocketIO server start failed at port: 8866!
2019-12-23 20:53:27.869 INFO org.springframework.boot.autoconfigure.logging.AutoConfigurationReportLoggingInitializer -

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2019-12-23 20:53:27.874 ERROR org.springframework.boot.SpringApplication - Application startup failed
java.lang.IllegalStateException: Failed to execute CommandLineRunner

at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:735)

at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:716)

at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:703)

at org.springframework.boot.SpringApplication.run(SpringApplication.java:304)

at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)

at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)

at com.sportmonks.Application.main(Application.java:31)

Caused by: java.net.BindException: Cannot assign requested address: bind

at sun.nio.ch.Net.bind0(Native Method)

at sun.nio.ch.Net.bind(Net.java:433)

at sun.nio.ch.Net.bind(Net.java:425)

at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:22

尝试二:

前端直接写公网IP则报以下的错误
【Java】【请你喝两杯咖啡50】springboot集成netty-socketio在本地测试正常,但部署在阿里云失败

前后端的IP应该如何设置?
各位路过的大侠打救下小弟吧,小弟已经苦闷数天。最快正确回答解决此问题的,事后我会请你喝两杯咖啡50(支付宝账号)

关键代码:

后端:springboot集成socket.io

<dependency>

<groupId\>com.corundumstudio.socketio</groupId\>

<artifactId\>netty-socketio</artifactId\>

<version\>1.7.16</version\>

</dependency>

com.corundumstudio.socketio.Configuration config = new com.corundumstudio.socketio.Configuration();
// 设置主机名,默认是0.0.0.0
config.setHostname("127.0.0.1");
// 设置监听端口
config.setPort(8866);

前端:

import VueSocketIO from 'vue-socket.io
import Vue from 'vue'
import store from './store'
import App from './App.vue'
import VueSocketIO from 'vue-socket.io'

Vue.use(new VueSocketIO({

debug: true,

connection: 'http://127.0.0.1:8866',

vuex: {

store,

actionPrefix: 'SOCKET\_',

mutationPrefix: 'SOCKET\_'

},

options: { path: "/my-app/" } //Optional options

}))

new Vue({

router,

store,

render: h \=> h(App)

}).$mount('#app')

回答

找到原因了!彻底的犯傻!归根到底是自己不够熟悉,使用docker部署使用的是容器内的IP,当然绑定报错。不使用docker直接在服务器部署,服务端设置私网IP,前端设置公网IP。websocket建立成功!

以上是 【Java】【请你喝两杯咖啡50】springboot集成netty-socketio在本地测试正常,但部署在阿里云失败 的全部内容, 来源链接: utcz.com/a/91169.html

回到顶部