 
本文档将向您展示如何将 Vert.x Web 会话存储在 Infinispan 中。
您将构建一个 Vert.x Web 应用程序,该应用程序会显示:
会话 ID 和会话创建时间
页面生成时间
 
该应用程序包含在一个 ServerVerticle 类中。
文本编辑器或 IDE
Java 11 或更高版本
Maven 或 Gradle
Infinispan 集群(或 Docker)
此项目的代码包含功能等效的 Maven 和 Gradle 构建文件。
以下是您应该使用的 pom.xml 文件的内容
pom.xml<?xml version="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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>io.vertx.howtos</groupId>
  <artifactId>web-session-infinispan-howto</artifactId>
  <version>1.0-SNAPSHOT</version>
  <properties>
    <vertx.version>5.0.0.CR2</vertx.version>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
  </properties>
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>io.vertx</groupId>
        <artifactId>vertx-stack-depchain</artifactId>
        <version>${vertx.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>io.vertx</groupId>
      <artifactId>vertx-web-sstore-infinispan</artifactId>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.13.0</version>
        <configuration>
          <release>11</release>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>3.5.0</version>
        <configuration>
          <mainClass>io.vertx.howtos.web.sessions.ServerVerticle</mainClass>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>假设您使用带有 Kotlin DSL 的 Gradle,您的 build.gradle.kts 文件应如下所示
build.gradle.ktsplugins {
  java
  application
}
repositories {
  mavenCentral()
}
java {
  toolchain {
    languageVersion.set(JavaLanguageVersion.of(11))
  }
}
dependencies {
  implementation(platform("io.vertx:vertx-stack-depchain:5.0.0.CR2"))
  implementation("io.vertx:vertx-web-sstore-infinispan")
}
application {
  mainClass = "io.vertx.howtos.web.sessions.ServerVerticle"
}Vert.x Web 的 SessionStore 接口提供了读取和修改会话数据的方法。
InfinispanSessionStore 实现可在 io.vertx:vertx-web-sstore-infinispan artifact 中找到。
以下是 ServerVerticle 类的代码
package io.vertx.howtos.web.sessions;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.Session;
import io.vertx.ext.web.handler.SessionHandler;
import io.vertx.ext.web.sstore.SessionStore;
import io.vertx.ext.web.sstore.infinispan.InfinispanSessionStore;
import java.util.Date;
public class ServerVerticle extends VerticleBase {
  public static final String TEMPLATE = ""
    + "Session [%s] created on %s%n"
    + "%n"
    + "Page generated on %s%n";
  @Override
  public Future<?> start() {
    Router router = Router.router(vertx);
    JsonObject options = new JsonObject()
      .put("servers", new JsonArray()
        .add(new JsonObject()
          .put("host", "localhost")
          .put("port", 11222)
          .put("username", "admin")
          .put("password", "bar"))
      );
    SessionStore store = InfinispanSessionStore.create(vertx, options); // (1)
    router.route().handler(SessionHandler.create(store)); // (2)
    router.get("/").handler(ctx -> {
      Session session = ctx.session();
      session.computeIfAbsent("createdOn", s -> System.currentTimeMillis()); // (3)
      String sessionId = session.id();
      Date createdOn = new Date(session.<Long>get("createdOn"));
      Date now = new Date();
      ctx.end(String.format(TEMPLATE, sessionId, createdOn, now)); // (4)
    });
    return vertx.createHttpServer()
      .requestHandler(router)
      .listen(8080);
  }
  public static void main(String[] args) {
    Vertx vertx = Vertx.vertx();
    vertx.deployVerticle(new ServerVerticle()).await();
  }
}使用您的 Infinispan 集群配置(例如,主机、端口和凭据)创建一个 InfinispanSessionStore。
注册管理 Vert.x Web 会话的 SessionHandler。
将会话创建时间戳放入会话数据中。
创建响应正文并将其发送给客户端。
如果您不熟悉 Infinispan,请查看其简介。
要在您的机器上使用 Docker 运行 Infinispan 服务器,请打开终端并执行以下命令:
docker run -p 11222:11222 -e USER="admin" -e PASS="bar" infinispan/server:14.0| 注意 | 如果您已经有一个 Infinispan 集群正在运行,请不要忘记更新 ServerVerticle中的配置(主机、端口和凭据)。 | 
ServerVerticle 已经包含 main 方法,因此可以直接用于:
创建一个 Vertx 上下文,然后
部署 ServerVerticle。
您可以从以下位置运行应用程序
或者在您的 IDE 中,通过运行 ServerVerticle 类的 main 方法。
使用 Maven:mvn compile exec:java,或者
使用 Gradle:./gradlew run(Linux、macOS)或 gradle run(Windows)。
浏览至 https://:8080。如果您是首次请求此页面,会话创建时间应与页面生成时间相同。
现在,我们通过停止应用程序并重新启动来模拟故障转移到另一个 Web 服务器。
新的 Web 服务器启动后,再次浏览至 https://:8080。会话 ID 和会话创建时间不应发生变化(因为会话数据已持久化到 Infinispan 中)。
本文档涵盖了
创建 InfinispanSessionStore 实例,
注册 Vert.x Web SessionHandler,
修改会话数据。
最后发布:2025-02-03 01:15:19 +0000。