1.pom.xml
通常的springboot 的pom文件有springboot-starter-web依賴,這樣會為項目添加tomcat容器,端口8080,由於這個項目用netty,不需要tomcat,所以用
spring-boot-starter和spring-boot-configuration-processor
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <groupId>demo.netty</groupId> 5 <artifactId>nettyspringboot</artifactId> 6 <version>0.0.1-SNAPSHOT</version> 7 8 <parent> 9 <groupId>org.springframework.boot</groupId> 10 <artifactId>spring-boot-starter-parent</artifactId> 11 <version>2.1.6.RELEASE</version> 12 <relativePath /> <!-- lookup parent from repository --> 13 </parent> 14 15 <dependencies> 16 17 <dependency> 18 <groupId>org.springframework.boot</groupId> 19 <artifactId>spring-boot-starter</artifactId> 20 </dependency> 21 <dependency> 22 <groupId>org.springframework.boot</groupId> 23 <artifactId>spring-boot-configuration-processor</artifactId> 24 <optional>true</optional> 25 </dependency> 26 27 <!-- 測試依賴 --> 28 <dependency> 29 <groupId>org.springframework.boot</groupId> 30 <artifactId>spring-boot-starter-test</artifactId> 31 <scope>test</scope> 32 </dependency> 33 <dependency> 34 <groupId>io.netty</groupId> 35 <artifactId>netty-all</artifactId> 36 <version>5.0.0.Alpha1</version> 37 </dependency> 38 <dependency> 39 <groupId>io.netty</groupId> 40 <artifactId>netty-example</artifactId> 41 <version>5.0.0.Alpha1</version> 42 </dependency> 43 <dependency> 44 <groupId>com.google.protobuf</groupId> 45 <artifactId>protobuf-java</artifactId> 46 <version>2.5.0</version> 47 </dependency> 48 <dependency> 49 <groupId>org.jboss.marshalling</groupId> 50 <artifactId>jboss-marshalling</artifactId> 51 <version>1.4.10.Final</version> 52 </dependency> 53 </dependencies> 54 55 <build> 56 <plugins> 57 <plugin> 58 <groupId>org.springframework.boot</groupId> 59 <artifactId>spring-boot-maven-plugin</artifactId> 60 </plugin> 61 </plugins> 62 </build> 63 64 </project>
2.netty channelhandler線程安全問題和springboot單例模式的矛盾
錯誤代碼:
/*@Bean(name = "httpRequestDecoder") @Scope("prototype") public HttpRequestDecoder httpRequestDecoder() { return new HttpRequestDecoder(); } @Bean(name = "httpObjectAggregator") @Scope("prototype") public HttpObjectAggregator httpObjectAggregator() { return new HttpObjectAggregator(65536); } @Bean(name = "httpResponseEncoder") @Scope("prototype") public HttpResponseEncoder httpResponseEncoder() { return new HttpResponseEncoder(); } @Bean(name = "chunkedWriteHandler") @Scope("prototype") public ChunkedWriteHandler chunkedWriteHandler() { return new ChunkedWriteHandler(); } @Bean(name = "fileServerHandler") @Scope("prototype") public FileServerHandler fileServerHandler() { System.out.println("fileserverhandler創建成功"); return new FileServerHandler(url); }*/
原因:https://blog.csdn.net/qq_39411208/article/details/88394675
https://blog.csdn.net/shmily_lsl/article/details/81207080