赞
踩
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
</plugin>
mvn -Pnative native:compile
plugins {
id 'java'
id 'org.springframework.boot' version '3.2.2'
id 'io.spring.dependency-management' version '1.1.4'
id 'org.graalvm.buildtools.native' version '0.9.28'
}
gradle nativeCompile
nativeCompile
build/native/nativeCompile
下生成一个本地可执行文件,可以双击运行mvn -Pnative spring-boot:build-image
gradle bootBuildImage
bootBuildImage
paketobuildpacks/run-jammy-tiny
paketobuildpacks/builder-jammy-tiny
# tptpbfysrt会每次不一样
pack.local/builder/tptpbfysrt
pack.local/builder/...
启动一个container用来创建镜像,会下载很多东西,如果执行失败,再次点击start运行即可,不会重新下载已经下载过的文件,不要重新执行gradle bootBuildImage
命令,不然会全部从头开始执行一遍下载依赖的过程,非常耗费时间。# myproject:0.0.1-SNAPSHOT换成你自己的项目名称和版本
docker run --rm -p 8080:8080 docker.io/library/myproject:0.0.1-SNAPSHOT
spring.threads.virtual.enabled=true
tomcat-embed-core-10.1.19.jar!\org\apache\tomcat\util\net\AbstractEndpoint.class
中 public void createExecutor() {
internalExecutor = true;
if (getUseVirtualThreads()) {
executor = new VirtualThreadExecutor(getName() + "-virt-");
} else {
TaskQueue taskqueue = new TaskQueue();
TaskThreadFactory tf = new TaskThreadFactory(getName() + "-exec-", daemon, getThreadPriority());
executor = new ThreadPoolExecutor(getMinSpareThreads(), getMaxThreads(), 60, TimeUnit.SECONDS,taskqueue, tf);
taskqueue.setParent( (ThreadPoolExecutor) executor);
}
}
2024-02-24T16:41:15.778+08:00 INFO 14252 --- [omcat-handler-0] com.example.demo.Controller : [Controller][hello] VirtualThread[#46,tomcat-handler-0]/runnable@ForkJoinPool-1-worker-1
VirtualThread[#46,tomcat-handler-0]
可以看出使用的是虚拟线程2024-02-24T16:43:14.715+08:00 INFO 15844 --- [nio-8080-exec-1] com.example.demo.Controller : [Controller][hello] Thread[#39,http-nio-8080-exec-1,5,main]
Thread[#39,http-nio-8080-exec-1,5,main]
可以看出使用的是平台线程# 100000个请求,500个并发
plow http://localhost:8080/hello -c 500 -n 100000
将 HTTP 服务定义为带有 @HttpExchange 方法的接口,并将这样的接口传递给 HttpServiceProxyFactory,创建一个代理,通过 HTTP 客户端(如 RestClient 或 WebClient)执行请求。类似于Feign使用声明式的方式访问Http服务。可以参考https://docs.spring.io/spring-framework/reference/integration/rest-clients.html#rest-http-interface
创建一个http interface
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.service.annotation.GetExchange;
import org.springframework.web.service.annotation.HttpExchange;
@HttpExchange("/api")
public interface HelloClient {
@GetExchange("/hello")
String hello(@RequestParam String msg);
}
import com.example.springboot3.client.HelloClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestClient;
import org.springframework.web.client.support.RestClientAdapter;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
@Configuration
public class AppConfig {
@Bean
public HelloClient toClient() {
RestClient restClient = RestClient.builder().baseUrl("http://localhost:80/").build();
HttpServiceProxyFactory httpServiceProxyFactory = HttpServiceProxyFactory.builderFor(RestClientAdapter.create(restClient)).build();
return httpServiceProxyFactory.createClient(HelloClient.class);
}
}
import com.example.springboot3.client.HelloClient;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Resource
private HelloClient client;
@GetMapping("hello")
public String hello(@RequestParam String msg) {
return client.hello(msg);
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。