美文网首页计算机理论&架构
云原生Java框架Quarkus-集成Spring启动

云原生Java框架Quarkus-集成Spring启动

作者: Botter0728 | 来源:发表于2021-05-03 19:20 被阅读0次

    1、集成Spring启动类

    POM.xml

    <?xml version="1.0"?><project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <groupId>cn.antzu</groupId> <artifactId>quarkus-web</artifactId> <version>1.0.0-SNAPSHOT</version> <properties> <compiler-plugin.version>3.8.1</compiler-plugin.version> <maven.compiler.parameters>true</maven.compiler.parameters> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <quarkus-plugin.version>1.13.3.Final</quarkus-plugin.version> <quarkus.platform.artifact-id>quarkus-universe-bom</quarkus.platform.artifact-id> <quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id> <quarkus.platform.version>1.13.3.Final</quarkus.platform.version> <surefire-plugin.version>3.0.0-M5</surefire-plugin.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>${quarkus.platform.group-id}</groupId> <artifactId>${quarkus.platform.artifact-id}</artifactId> <version>${quarkus.platform.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-spring-web</artifactId> </dependency> <dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-spring-boot-properties</artifactId> </dependency> <dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-arc</artifactId> </dependency> <dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-junit5</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>io.rest-assured</groupId> <artifactId>rest-assured</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-resteasy</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>io.quarkus</groupId> <artifactId>quarkus-maven-plugin</artifactId> <version>${quarkus-plugin.version}</version> <extensions>true</extensions> <executions> <execution> <goals> <goal>build</goal> <goal>generate-code</goal> <goal>generate-code-tests</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>${compiler-plugin.version}</version> <configuration> <parameters>${maven.compiler.parameters}</parameters> </configuration> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>${surefire-plugin.version}</version> <configuration> <systemPropertyVariables> <java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager> <maven.home>${maven.home}</maven.home> </systemPropertyVariables> </configuration> </plugin> </plugins> </build> <profiles> <profile> <id>native</id> <activation> <property> <name>native</name> </property> </activation> <build> <plugins> <plugin> <artifactId>maven-failsafe-plugin</artifactId> <version>${surefire-plugin.version}</version> <executions> <execution> <goals> <goal>integration-test</goal> <goal>verify</goal> </goals> <configuration> <systemPropertyVariables> <native.image.path> ${project.build.directory}/${project.build.finalName}-runner </native.image.path> <java.util.logging.manager>org.jboss.logmanager.LogManager </java.util.logging.manager> <maven.home>${maven.home}</maven.home> </systemPropertyVariables> </configuration> </execution> </executions> </plugin> </plugins> </build> <properties> <quarkus.package.type>native</quarkus.package.type> </properties> </profile> </profiles></project> 

    2、监听启动和关闭

    package cn.antzu.quarkus;

    import javax.enterprise.context.ApplicationScoped;

    import io.quarkus.runtime.annotations.QuarkusMain;

    import io.quarkus.runtime.QuarkusApplication;

    import javax.enterprise.event.Observes;

    import io.quarkus.runtime.ShutdownEvent;

    import io.quarkus.runtime.StartupEvent;

    import org.jboss.logging.Logger;

    /** * Quarkus 监听启动入口 * @author xiaobao */

    @ApplicationScoped

    @QuarkusMain

    public class QuarkusWebApplication  implements QuarkusApplication {

    private static final Logger LOGGER = Logger.getLogger("QuarkusWebApplication");

    public void onStart(@Observes StartupEvent ev) { LOGGER.info("The QuarkusWeb application is starting..."); }

    public void onStop(@Observes ShutdownEvent ev) { LOGGER.info("The QuarkusWeb application is stopping..."); }} 

    public static void main(String[] args) { Quarkus.run(QuarkusWebApplication.class,args); }

    @Override

    public int run(String... args) throws Exception { Quarkus.waitForExit(); return 0; }

    3、弄一个实例 RestController,采用Spring注入的方式

    //RestController 

    package cn.antzu.quarkus.web;

    import cn.antzu.quarkus.service.GreetingService;

    import org.springframework.web.bind.annotation.GetMapping;

    import org.springframework.web.bind.annotation.RequestMapping;

    import org.springframework.web.bind.annotation.RestController;

    import javax.inject.Inject;

    /** * @author xiaobao */

    @RestController

    @RequestMapping("/quarkus")

    public class SpringGreetingController {

    @Inject private GreetingService greetingService;

    @GetMapping public String hello() { return greetingService.greeting("Spring"); }}

    //Service

    package cn.antzu.quarkus.service;

    import javax.enterprise.context.ApplicationScoped;

    /** * @author xiaobao */

    @ApplicationScopedpublic class GreetingService { public String greeting(String name) { return "hello " + name; }} 

    4、启动和打包

    就是简简单单打包:

     mvn package -DskipTests  

    按照环境打包

    mvn package quarkus:dev -DskipTests

    打个可执行jar包

    mvn package -DskipTests -Dquarkus.package.type=uber-jar

        查看启动日志(启动和停止:

    ➜  java -jar target/quarkus-web-1.0.0-SNAPSHOT-runner.jar

    __  ____  __  _____  ___  __ ____  ______

    --/ __ \/ / / / _ | / _ \/ //_/ / / / __/

    -/ /_/ / /_/ / __ |/ , _/ ,< / /_/ /\ \ 

    --\___\_\____/_/ |_/_/|_/_/|_|\____/___/ 

    2021-05-03 19:05:30,551 INFO  [QuarkusWebApplication] (main) The QuarkusWeb application is starting...

    2021-05-03 19:05:30,665 INFO  [io.quarkus] (main) quarkus-web 1.0.0-SNAPSHOT on JVM (powered by Quarkus 1.13.3.Final) started in 1.038s. Listening on: http://0.0.0.0:8080

    2021-05-03 19:05:30,667 INFO  [io.quarkus] (main) Profile prod activated.

    2021-05-03 19:05:30,667 INFO  [io.quarkus] (main) Installed features: [cdi, resteasy, resteasy-jackson, spring-boot-properties, spring-di, spring-web]

    ^C2021-05-03 19:16:43,785 INFO  [QuarkusWebApplication] (Shutdown thread) The  QuarkusWeb application is stopping...

    2021-05-03 19:16:43,811 INFO  [io.quarkus] (Shutdown thread) quarkus-web stopped in 0.069s

    相关文章

      网友评论

        本文标题:云原生Java框架Quarkus-集成Spring启动

        本文链接:https://www.haomeiwen.com/subject/fjtqdltx.html