Spring Web Socket 客户端 服务端实现 握手时传递参数

基于Spring boot的非常简单的WebSocket客户端与服务端通信,真心不想写Socket,用Netty写着太累,真心没多少时候需要那么好的性能。

 

在握手时,试着传个参数,发现没问题,整体用起来,Spring的东西很靠谱。

 

服务端:

 

import com.google.common.base.Strings;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socke
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
1. 引入Spring Cloud Config客户依赖 在POM文件中添加如下依赖: ``` <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> ``` 2. 配置bootstrap.yml 在应用程序的src/main/resources目录下,新建bootstrap.yml文件,配置如下: ``` spring: application: name: your-application-name cloud: config: uri: http://config-server-host:port profile: dev label: master ``` 其中: - your-application-name:是当前应用程序的名称,必填项。 - uri:配置服务器的地址,必填项。 - profile:用于指定当前应用程序的配置文件,选填项。 - label:用于指定Git仓库中的分支或标签,默认为master,选填项。 3. 从配置服务器获取配置信息 在需要获取配置信息的类中,注入ConfigurableApplicationContext对象,并使用该对象获取配置信息,示例代码如下: ``` @Autowired private ConfigurableApplicationContext applicationContext; public void getConfig() { String foo = applicationContext.getEnvironment().getProperty("foo"); String bar = applicationContext.getEnvironment().getProperty("bar"); String baz = applicationContext.getEnvironment().getProperty("baz"); System.out.println(foo + " " + bar + " " + baz); } ``` 其中,foo、bar和baz是配置文件中的属性名。 4. 运行应用程序 配置完成后,启动应用程序即可从配置服务器获取配置信息。如果配置服务器中的配置信息发生变化,客户会自动刷新配置信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值