shenyu-plugin-websocket
.org.apache.shenyu.plugin.websocket.WebSocketPlugin
.Explanation of terms
Explanation of the process
Manually configure the client service on the shenyu-admin page, and the backend service will implement the websocket protocol forwarding without any changes.
Start the client service (backend websocket service).
Test the success of service forwarding.
If there are scenarios where you need to automate configuration to reduce workload, you can add annotations to the backend service to automate the configuration of the service to the shenyu gateway.
<dependency> <groupId>org.apache.shenyu</groupId> <artifactId>shenyu-spring-boot-starter-plugin-websocket</artifactId> <version>${project.version}</version> </dependency>
@ShenyuSpringWebSocketClient
annotation, which will automatically register the websocket service to the shenyu gateway.shenyu.register.*
, the following example uses the http access method, currently the client supports the following access methods: http, zookeeper, etcd, nacos, consul, please refer to client access configuration for detailed access configuration parameters.shenyu.client.websocket.*
, containing the service name, routing address and port, and the contextPath value must be configured as the routing address for each service.shenyu: register: registerType: http serverLists: http://localhost:9095 props: username: admin password: 123456 client: websocket: props: contextPath: /ws-annotation appName: ws-annotation port: 8001 # Need to be consistent with the service port
Using auto-configuration, after the client starts it will automatically register selectors and rules in shenyu-admin -> Plugin List -> Proxy -> Websocket information.
The example of websocket selector configuration, please refer to selectors and rules for general selector configuration.
host
:Fill in localhost
, this field is not used for now.ip:port
:ip
and port, here fill in the ip
+ port of your real service.protocol
:ws
protocol, do not fill in the default: ws://
.startupTime
:Start-up time in milliseconds.weight
:The default value for load balancing weight, which is automatically registered for service startup, is 50.warmupTime
:Warm-up time, in milliseconds, the server in warm-up will calculate the instantaneous weight, the calculated value will be less than the actual weight configured to protect the server just started, the default value of service start registration is 10. For example, the warm-up time is 100 milliseconds, currently started 50 milliseconds, the configured weight is 50, the actual weight is 25.status
:open or close, the start state of this processor is only valid.The example of websocket rule configuration, please refer to selectors and rules for general rule configuration .
loadStrategy
: if the websocket client is a cluster, which load balancing strategy to take when the Apache ShenYu gateway is invoked, currently supports roundRobin, random and hash.timeout
: The timeout period for calling the client.retryCount
: The number of retries to call client timeout failures.shenyu-example-spring-annotation-websocket
shenyu-example-spring-native-websocket
shenyu-example-spring-reactive-websocket
4.1 Websocket connection establishment error 1002
Possible causes: client service is not normal, shenyu gateway and client project can not establish a normal connection, please check the gateway to the client network, client service is normal.
4.2 Multiple client services are displayed as empty in the websocket selector
Possible cause: BasicConfig -> Plugin -> websocket -> multiSelectorHandle option select multiple handle.
<!DOCTYPE HTML> <html> <head> <meta http-equiv="content-type" content="text/html" /> <title>Shenyu WebSocket Test</title> <script> var websocket; function connect() { try { websocket = new WebSocket(document.getElementById("url").value); websocket.onopen = onOpen; websocket.onerror = onError; websocket.onmessage = onReceive; websocket.onclose = onClose; } catch (e) { alert('[websocket] establish connection error.'); } } function onOpen() { alert('[websocket] connect success.'); } function onError(e) { alert("[websocket] connect error. code: " + e.code); } function onReceive(msg) { var show = document.getElementById("show"); show.innerHTML += "[Server Response] => " + msg.data + "<br/>"; show.scrollTop = show.scrollHeight; } function onClose(e) { console.log("[websocket] connect closed. code: " + e.code) alert("[websocket] connect closed."); document.getElementById("show").innerHTML = ""; document.getElementById("msg").value = ""; websocket = null; } function buttonClose() { if (websocket == null) { console.log("Please establish a connection first.") } else { websocket.close(1000); document.getElementById("show").innerHTML = ""; document.getElementById("msg").value = ""; } } function send() { if (websocket == null) { alert("Please establish a connection first.") } else { var msg = document.getElementById("msg").value; show.innerHTML += "[Client Request] => " + msg + "<br/>"; websocket.send(msg); } } </script> </head> <body> <input id="url" type="text" value="ws://localhost:9195/ws-annotation/myWs"><br /> <input id="msg" type="text"><br /> <button id="connect" onclick="connect();">Connect</button> <button id="send" onclick="send();">Send</button> <button id="close" onclick="buttonClose();">Close</button></br> <div id="show" class="show"></div> </body> </html> <style> input { width: 400px; margin-bottom: 10px; } .show { width: 600px; height: 400px; overflow-y: auto; border: 1px solid #333; margin-top: 10px; } </style>