NoSQLキー/バリューストア、分散キャッシュなど、さまざまな用途で使われるInfinispan。まずは基本となるクライアントサーバの環境を構築して動作させてみました。
環境) Infinispan server 13.0.15 / maven 3.9.0 / openjdk 17 / Ubuntu22.04
インストール)
apt install openjdk-17-jdk
wget https://dlcdn.apache.org/maven/maven-3/3.9.0/binaries/apache-maven-3.9.0-bin.tar.gz
tar zxcvf apache-maven-3.9.0-bin.tar.gz
mv apache-maven-3.9.0-bin /opt/apache-maven
export JAVA_HOME=$(readlink -f /usr/bin/java | sed “s:bin/java::”)
export PATH=$PATH:/opt/apache-maven/bin
サンプルコード生成・実行
mvn archetype:generate \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DinteractiveMode=false \
-DgroupId=com.sample \
-DartifactId=hello
cd hello
mvn compile
java -cp target/classes com.sample.App
この環境をベースにコーディングします。まずはサーバを使わずローカルでキーバリューストア機能の確認です。
src/main/java/com/sample/App.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package com.sample; import org.infinispan.manager.DefaultCacheManager; import org.infinispan.manager.EmbeddedCacheManager; import org.infinispan.configuration.cache.ConfigurationBuilder; import org.infinispan.Cache; public class App { public static void main( String[] args ) { System.out.println( "Hello!" ); EmbeddedCacheManager manager = new DefaultCacheManager(); manager.defineConfiguration("mycache", new ConfigurationBuilder().build()); Cache<String, String> c = manager.getCache("mycache"); c.put("key", "value"); System.out.println(c.get("key")); System.out.println("Cache size is " + c.size()); } } |
ビルド・実行
mvn clean compile assembly:single
java -jar target/hello-1.0-SNAPSHOT-jar-with-dependencies.jar
結果
n@n10:~/infinispan-server-13.0.15.Final/_work/hello$ java -jar target/hello-1.0-SNAPSHOT-jar-with-dependencies.jar
Hello!
Mar 13, 2023 6:01:40 AM org.infinispan.marshall.core.impl.DelegatingUserMarshaller start
INFO: ISPN000556: Starting user marshaller ‘org.infinispan.commons.marshall.ImmutableProtoStreamMarshaller’
value
Cache size is 1
(これはversion14.0.6でも動作確認)
次にサーバを使います。
インストール)
wget https://downloads.jboss.org/infinispan/13.0.15.Final/infinispan-server-13.0.15.Final.zip
unzip infinispan-server-13.0.15.Final.zip
cd infinispan-server-13.0.15.Final
Infinispan Server起動
bin/server.sh -b 0.0.0.0
ユーザ、キャッシュの作成
n@n10:~/infinispan-server-13.0.15.Final$ bin/cli.sh user create admin -p password
n@n10:~/infinispan-server-13.0.15.Final$ bin/cli.sh
[disconnected]> connect
Username: admin
Password: ********
[n10-3469@cluster//containers/default]> create cache myCache -t org.infinispan.DIST_SYNC
[n10-3469@cluster//containers/default]> ls caches
___script_cache
myCache
src/main/java/com/sample/App.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package com.sample; import org.infinispan.client.hotrod.RemoteCache; import org.infinispan.client.hotrod.RemoteCacheManager; import org.infinispan.client.hotrod.configuration.ConfigurationBuilder; public class App { public static void main( String[] args ) { System.out.println( "Hello! 2" ); ConfigurationBuilder cb = new ConfigurationBuilder(); cb.addServer().host("127.0.0.1").port(11222) .security().authentication().saslMechanism("PLAIN") .username("admin").password("password"); RemoteCacheManager manager = new RemoteCacheManager(cb.build()); RemoteCache<String, String> c = manager.getCache("myCache"); c.put("key", "value"); System.out.println(c.get("key")); System.out.println("Cache size is " + c.size()); } } |
ビルド・実行
mvn clean compile assembly:single
java -jar target/hello2-1.0-SNAPSHOT-jar-with-dependencies.jar
結果
n@n10:~/infinispan-server-13.0.15.Final/_work/hello2$ java -jar target/hello2-1.0-SNAPSHOT-jar-with-dependencies.jar
Hello! 2
SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”.
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Mar 13, 2023 6:06:22 AM org.infinispan.client.hotrod.RemoteCacheManager actualStart
INFO: ISPN004021: Infinispan version: Infinispan ‘Triskaidekaphobia’ 13.0.15.Final
Mar 13, 2023 6:06:22 AM org.infinispan.client.hotrod.impl.transport.netty.ChannelFactory receiveTopology
INFO: ISPN004006: Server sent new topology view (id=1, age=0) containing 1 addresses: [127.0.0.1/:11222]
value
Cache size is 1
どちらも設定したキーが取得できることを確認しました。
まだいろいろと不備があると思いますが、まずは動かしてから掘り下げていきたいと思います。
下記、pom.xmlとserver/conf/infinispan.xmlには不要な設定があるかもしれませんが、参考まで。
参考)
「Infinispan をクライアントサーバモードで使ってみる」
https://b.chiroito.dev/entry/2020/03/17/194635
「Infinispan Server(Hot Rod)で、認証・認可設定を行う」
https://kazuhira-r.hatenablog.com/entry/2020/03/01/224038
pom.xml(サーバアクセスの方。hotrodがversion14では対応していなかったため、ver13を使用した。サーバはver14でも可)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.sample</groupId> <artifactId>hello2</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>hello2</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.infinispan</groupId> <artifactId>infinispan-core</artifactId> <version>13.0.15.Final</version> </dependency> <dependency> <groupId>org.infinispan</groupId> <artifactId>infinispan-client-hotrod</artifactId> <version>13.0.15.Final</version> </dependency> </dependencies> <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.3</version> <configuration> <archive> <manifest> <mainClass> com.sample.App </mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </pluginManagement> </build> </project> |
server/conf/infinispan.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
<infinispan xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:infinispan:config:13.0 https://infinispan.org/schemas/infinispan-config-13.0.xsd urn:infinispan:server:13.0 https://infinispan.org/schemas/infinispan-server-13.0.xsd" xmlns="urn:infinispan:config:13.0" xmlns:server="urn:infinispan:server:13.0"> <cache-container name="default" statistics="true"> <transport cluster="${infinispan.cluster.name:cluster}" stack="${infinispan.cluster.stack:tcp}" node-name="${infinispan.node.name:}"/> <metrics gauges="true" histograms="true"/> <security> <authorization> <identity-role-mapper/> <role name="admin" permissions="ALL"/> <role name="reader-writer" permissions="READ WRITE"/> <role name="writer" permissions="WRITE"/> <role name="reader" permissions="READ"/> </authorization> </security> <distributed-cache name="plainCache"/> <!-- <distributed-cache name="securedCache"> <security> <authorization roles="admin reader-writer writer reader"/> </security> </distributed-cache> --> </cache-container> <server xmlns="urn:infinispan:server:13.0"> <interfaces> <interface name="public"> <inet-address value="${infinispan.bind.address:127.0.0.1}"/> </interface> </interfaces> <socket-bindings default-interface="public" port-offset="${infinispan.socket.binding.port-offset:0}"> <socket-binding name="default" port="${infinispan.bind.port:11222}"/> <socket-binding name="memcached" port="11221"/> </socket-bindings> <security> <credential-stores> <credential-store name="credentials" path="credentials.pfx"> <clear-text-credential clear-text="secret"/> </credential-store> </credential-stores> <security-realms> <security-realm name="default"> <!-- Uncomment to enable TLS on the realm --> <!-- server-identities> <ssl> <keystore path="application.keystore" password="password" alias="server" generate-self-signed-certificate-host="localhost"/> </ssl> </server-identities--> <properties-realm groups-attribute="Roles"> <user-properties path="users.properties"/> <group-properties path="groups.properties"/> </properties-realm> </security-realm> </security-realms> </security> <endpoints socket-binding="default" security-realm="default"> <endpoint> <hotrod-connector> <authentication> <sasl mechanisms="SCRAM-SHA-512 SCRAM-SHA-384 SCRAM-SHA-256 SCRAM-SHA-1 DIGEST-SHA-512 DIGEST-SHA-384 DIGEST-SHA-256 DIGEST-SHA DIGEST-MD5 PLAIN" server-name="infinispan" qop="auth"/> </authentication> </hotrod-connector> <rest-connector> <authentication mechanisms="DIGEST BASIC"/> </rest-connector> </endpoint> </endpoints> </server> </infinispan> |