在部分业务中,我们需要使用长连接,我们可以使用http长连接或者websocket,在springboot作为后端的框架中, 可以借用的技术是(netty,websocket)
| 软件 | 版本号 |
| jdk | 21 |
| springboot | 3.1.5 |
| springcloud | 2022.0.4 |
- "1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0modelVersion>
- <parent>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-parentartifactId>
- <version>3.1.5version>
- <relativePath/>
- parent>
- <groupId>com.examplegroupId>
- <artifactId>testDiartifactId>
- <version>0.0.1-SNAPSHOTversion>
- <name>testDiname>
- <description>testDidescription>
- <properties>
- <java.version>21java.version>
- <spring-cloud.version>2022.0.4spring-cloud.version>
- properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-data-redisartifactId>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-websocketartifactId>
- dependency>
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-starter-openfeignartifactId>
- dependency>
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-starter-loadbalancerartifactId>
- dependency>
- <dependency>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-testartifactId>
- <scope>testscope>
- dependency>
- dependencies>
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-dependenciesartifactId>
- <version>${spring-cloud.version}version>
- <type>pomtype>
- <scope>importscope>
- dependency>
- dependencies>
- dependencyManagement>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-maven-pluginartifactId>
- plugin>
- plugins>
- build>
-
- project>
- package com.example.testdi.config;
-
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.web.socket.server.standard.ServerEndpointExporter;
- import org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean;
-
- @Configuration
- public class WebsocketConfig {
-
- @Bean
- public ServerEndpointExporter serverEndpointExporter() {
- return new ServerEndpointExporter();
- }
-
- @Bean
- public ServletServerContainerFactoryBean createWebSocketContainer() {
- ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
- container.setMaxTextMessageBufferSize(8192 * 4);
- container.setMaxBinaryMessageBufferSize(8192 * 4);
- return container;
- }
- }
- package com.example.testdi.rpc;
-
- import org.springframework.cloud.openfeign.FeignClient;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.ResponseBody;
-
- @FeignClient(value = "http://localhost:9090")
- public interface TestRpc {
-
- @GetMapping("/hello")
- @ResponseBody
- String hello();
- }
- @Slf4j
- @Component
- @ServerEndpoint(value = "/example")
- public class ExampleWebsocket {
-
- @Resource
- private RedisTemplate
redisTemplate; -
- @Resource
- private TestRpc testRpc;
-
- @OnOpen
- public void open(Session session) {
- log.info("===============open=================");
- log.info("redisTemplate: {}", redisTemplate);
- log.info("testRpc: {}", testRpc);
- }
- }
- @EnableFeignClients
- @SpringBootApplication
- public class TestDiApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(TestDiApplication.class, args);
- }
-
- }
- spring:
- data:
- redis:
- host: xxx.xxx.xxx.xxx
- database: xx
- port: xxxx
- timeout: xxxx
- password: xxxxxxxxxxxxxxx
- server:
- port: 9090

可以看到都是空指针
- package com.example.testdi;
-
- import jakarta.annotation.Resource;
- import org.springframework.boot.CommandLineRunner;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
- import org.springframework.cloud.openfeign.EnableFeignClients;
- import org.springframework.context.ApplicationContext;
-
- import java.util.Arrays;
- import java.util.List;
-
- @EnableFeignClients
- @SpringBootApplication
- public class TestDiApplication extends SpringBootServletInitializer implements CommandLineRunner {
-
- @Resource
- private ApplicationContext appContext;
-
- public static void main(String[] args) {
- SpringApplication.run(TestDiApplication.class, args);
- }
-
-
- @Override
- public void run(String... args) throws Exception
- {
- String[] beans = appContext.getBeanDefinitionNames();
- Arrays.sort(beans);
- List
list = Arrays.stream(beans).filter(ele -> ele.contains("TestRpc") || ele.contains("redisTemplate")).toList(); - for (String bean : list)
- {
- System.out.println(bean + " of Type :: " + appContext.getBean(bean).getClass());
- }
- }
- }

发现并不是上述情况
- package com.example.testdi.utils;
-
- import org.springframework.beans.BeansException;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.ApplicationContextAware;
- import org.springframework.stereotype.Component;
-
- @Component
- public class ApplicationHelper implements ApplicationContextAware {
-
- private static ApplicationContext applicationContext;
-
- @Override
- public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
- ApplicationHelper.applicationContext = applicationContext;
- }
-
- public static ApplicationContext getApplicationContext() {
- return applicationContext;
- }
-
- public static Object getBean(String beanName) {
- return applicationContext.getBean(beanName);
- }
-
- }
-
- package com.example.testdi.websocket;
-
- import com.example.testdi.rpc.TestRpc;
- import com.example.testdi.utils.ApplicationHelper;
- import jakarta.annotation.Resource;
- import jakarta.websocket.OnOpen;
- import jakarta.websocket.Session;
- import jakarta.websocket.server.ServerEndpoint;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.data.redis.core.RedisTemplate;
- import org.springframework.stereotype.Component;
-
- @Slf4j
- @Component
- @ServerEndpoint(value = "/example")
- public class ExampleWebsocket {
-
- private RedisTemplate
redisTemplate= (RedisTemplate) ApplicationHelper.getBean("redisTemplate"); -
- private TestRpc testRpc= (TestRpc) ApplicationHelper.getBean("com.example.testdi.rpc.TestRpc");
-
- @OnOpen
- public void open(Session session) {
- log.info("===============open=================");
- log.info("redisTemplate: {}", redisTemplate);
- log.info("testRpc: {}", testRpc);
- }
- }

这里推荐2个,一个是postman,一个是网站


即可开始测试
这边推荐测试网站测试,支持ws/wss