最近公司可谓是风云变幻,年前说要拆开卖,后来说要整体卖,表示像我这种渣渣,始终逃脱不掉被卖的命运
下面进入正题
spring webflux 是spring 支持的高并发web框架,将每个http请求都以java nio的非阻塞方式来进行处理
这样当cpu在处理一个请求的空隙时,还有时间来处理其他请求。提高CPU资源的运行效率
下面来看如何进行实现
1、首先eclipse中新建一个maven的项目。然后完整版的pom文件如下所示。(本人翻遍了网上的教程,都没有提供完整版的pom文件,哪里有?哪里有?感觉这里是独一份,除了github以外)
4.0.0
Pactera
webflux-test
0.0.1-SNAPSHOT
jar
webflux-test
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-parent
2.0.6.BUILD-SNAPSHOT
junit
junit
3.8.1
test
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-webflux
org.springframework.boot
spring-boot-starter-test
test
io.projectreactor
reactor-test
test
compile
maven-jar-plugin
package
spring-snapshots
Spring Snapshots
https://repo.spring.io/snapshot
true
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
spring-snapshots
Spring Snapshots
https://repo.spring.io/snapshot
true
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
2、下面是一个handler处理类,这个类是用来处理http请求的核心类
package org.spring.springboot.handler;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;
@Component
public class CityHandler {
public Mono helloCity(ServerRequest request) {
return ServerResponse.ok().contentType(MediaType.TEXT_PLAIN)
.body(BodyInserters.fromObject("Hello, City!"));
}
}
3、下面一个类是当监测到CPU有空闲时间的时候,将HTTP请求分发给handler类的router类
package org.spring.springboot.router;
import org.spring.springboot.handler.CityHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
@Configuration
public class CityRouter {
@Bean
public RouterFunction routeCity(CityHandler cityHandler) {
return RouterFunctions.route(
RequestPredicates.GET("/hello").and(RequestPredicates.accept(MediaType.TEXT_PLAIN)),
cityHandler::helloCity);
}
}
4、然后在最外层的主入口,Application.java
package org;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableAutoConfiguration
@SpringBootApplication
@ComponentScan("org.spring")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
5、整个包的结构如下所示:
6、然后程序跑起来啦
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.6.BUILD-SNAPSHOT)
2019-03-05 07:37:15.713 INFO 8284 --- [ main] org.Application : Starting Application on DESKTOP-QGKILFJ with PID 8284 (D:\eclipse-workspace\webflux-test\target\classes started by weizhen in D:\eclipse-workspace\webflux-test)
2019-03-05 07:37:15.725 INFO 8284 --- [ main] org.Application : No active profile set, falling back to default profiles: default
2019-03-05 07:37:15.818 INFO 8284 --- [ main] onfigReactiveWebServerApplicationContext : Refreshing org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext@62e136d3: startup date [Tue Mar 05 07:37:15 CST 2019]; root of context hierarchy
2019-03-05 07:37:17.662 WARN 8284 --- [ main] reactor.ipc.netty.tcp.TcpResources : [http] resources will use the default LoopResources: DefaultLoopResources {prefix=reactor-http, daemon=true, selectCount=4, workerCount=4}
2019-03-05 07:37:17.662 WARN 8284 --- [ main] reactor.ipc.netty.tcp.TcpResources : [http] resources will use the default PoolResources: DefaultPoolResources {name=http, provider=reactor.ipc.netty.resources.PoolResources$$Lambda$188/243575009@2e554a3b}
2019-03-05 07:37:18.293 INFO 8284 --- [ main] o.s.w.r.f.s.s.RouterFunctionMapping : Mapped ((GET && /hello) && Accept: [text/plain]) -> org.spring.springboot.router.CityRouter$$Lambda$197/416201381@64bc21ac
2019-03-05 07:37:18.310 INFO 8284 --- [ main] o.s.w.r.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.reactive.resource.ResourceWebHandler]
2019-03-05 07:37:18.310 INFO 8284 --- [ main] o.s.w.r.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.reactive.resource.ResourceWebHandler]
2019-03-05 07:37:18.437 INFO 8284 --- [ main] o.s.w.r.r.m.a.ControllerMethodResolver : Looking for @ControllerAdvice: org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext@62e136d3: startup date [Tue Mar 05 07:37:15 CST 2019]; root of context hierarchy
2019-03-05 07:37:18.750 INFO 8284 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2019-03-05 07:37:20.073 INFO 8284 --- [ctor-http-nio-1] r.ipc.netty.tcp.BlockingNettyContext : Started HttpServer on /0:0:0:0:0:0:0:0:8080
2019-03-05 07:37:20.074 INFO 8284 --- [ main] o.s.b.web.embedded.netty.NettyWebServer : Netty started on port(s): 8080
2019-03-05 07:37:20.080 INFO 8284 --- [ main] org.Application : Started Application in 4.836 seconds (JVM running for 5.499)
7、进行访问
这样下来一个简单的高并发框架就完成啦
可能有小伙伴看博主之前写的博客都是关于机器学习的,还有salesforce的
其实吧,博主一直是做java的,两年半以来做过GE的NPI CRM项目,惠普的Solr搜素项目,还有GE的语言助手项目,还有赛诺菲的供应链语音助手项目
为什么不写java类的博客,主要是博客觉得java这个应该都算是基础知识,应该是程序猿最基本的知识。
感觉写出来也不显得吸引人眼球。所以没写。不过今天这个高并发确实很厉害。所以写了一下
Thanks
WeiZhen