[test/plugin] immigrate test cassandra scenario. (#4040)

* [test/plugin] immigrate test cassandra scenario.

* update support list doc.

* delete space error.

* disable jmx report.

* remove jmx dependence.

* Disable jmx report.

* Update support list doc.
This commit is contained in:
aderm 2019-12-12 12:52:00 +08:00 committed by 吴晟 Wu Sheng
parent 5a07d27b66
commit 4ec2cb07ed
12 changed files with 1012 additions and 2 deletions

View File

@ -157,7 +157,7 @@ jobs:
run: bash test/plugin/run.sh armeria-0.96plus-scenario
Hystrix_SofaRPC_Spring30x:
Cassandra_Hystrix_SofaRPC_Spring30x:
runs-on: ubuntu-18.04
timeout-minutes: 90
strategy:
@ -179,6 +179,8 @@ jobs:
run: ./mvnw clean package -DskipTests -Pagent >/dev/null
- name: Build the Docker image
run: ./mvnw -f test/plugin/pom.xml clean package -DskipTests docker:build -DBUILD_NO=local >/dev/null
- name: Run cassandra 3.7.0-3.7.2 (3)
run: bash test/plugin/run.sh cassandra-java-driver-3.x-scenario
- name: Run hystrix 1.4.20-1.5.12 (20)
run: bash test/plugin/run.sh hystrix-scenario
- name: Run sofarpc 5.4.0-5.6.2 (23)

View File

@ -63,7 +63,7 @@
* [Solr](https://github.com/apache/lucene-solr/)
* [SolrJ](https://github.com/apache/lucene-solr/tree/master/solr/solrj) 7.x
* [Cassandra](https://github.com/apache/cassandra) 3.x
* [cassandra-java-driver](https://github.com/datastax/java-driver) 3.6.0-3.7.2
* [cassandra-java-driver](https://github.com/datastax/java-driver) 3.7.0-3.7.2
* Service Discovery
* [Netflix Eureka](https://github.com/Netflix/eureka)
* Distributed Coordination

View File

@ -0,0 +1,21 @@
#!/bin/bash
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
home="$(cd "$(dirname $0)"; pwd)"
java -jar ${agent_opts} ${home}/../libs/cassandra-java-driver-3.x-scenario.jar &

View File

@ -0,0 +1,495 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
registryItems:
applications:
- {cassandra-java-driver-3.x-scenario: 2}
instances:
- {cassandra-java-driver-3.x-scenario: 1}
operationNames:
- cassandra-java-driver-3.x-scenario: [Cassandra/execute, Cassandra/executeAsync, /cassandra-java-driver-3.x-scenario/case/cassandra]
heartbeat: []
segmentItems:
- applicationCode: cassandra-java-driver-3.x-scenario
segmentSize: ge 1
segments:
- segmentId: not null
spans:
- operationName: Cassandra/getUninterruptibly
operationId: 0
parentSpanId: 1
spanId: 2
spanLayer: Database
startTime: nq 0
endTime: nq 0
componentId: 69
componentName: ''
isError: false
spanType: Local
peer: ''
peerId: 0
tags:
- {key: db.type, value: cassandra}
- operationName: Cassandra/execute
operationId: 0
parentSpanId: 0
spanId: 1
spanLayer: Database
startTime: nq 0
endTime: nq 0
componentId: 69
componentName: ''
isError: false
spanType: Exit
peer: cassandra-server:9042
peerId: 0
tags:
- {key: db.type, value: cassandra}
- {key: db.instance, value: ''}
- {key: db.statement, value: 'CREATE KEYSPACE IF NOT EXISTS demo WITH replication = {''class'': ''SimpleStrategy'', ''replication_factor'': 1}'}
- operationName: Cassandra/getUninterruptibly
operationId: 0
parentSpanId: 3
spanId: 4
spanLayer: Database
startTime: nq 0
endTime: nq 0
componentId: 69
componentName: ''
isError: false
spanType: Local
peer: ''
peerId: 0
tags:
- {key: db.type, value: cassandra}
- operationName: Cassandra/execute
operationId: 0
parentSpanId: 0
spanId: 3
spanLayer: Database
startTime: nq 0
endTime: nq 0
componentId: 69
componentName: ''
isError: false
spanType: Exit
peer: cassandra-server:9042
peerId: 0
tags:
- {key: db.type, value: cassandra}
- {key: db.instance, value: ''}
- {key: db.statement, value: 'CREATE TABLE IF NOT EXISTS demo.test(id TEXT PRIMARY KEY, value TEXT)'}
- operationName: Cassandra/getUninterruptibly
operationId: 0
parentSpanId: 5
spanId: 6
spanLayer: Database
startTime: nq 0
endTime: nq 0
componentId: 69
componentName: ''
isError: false
spanType: Local
peer: ''
peerId: 0
tags:
- {key: db.type, value: cassandra}
- operationName: Cassandra/execute
operationId: 0
parentSpanId: 0
spanId: 5
spanLayer: Database
startTime: nq 0
endTime: nq 0
componentId: 69
componentName: ''
isError: false
spanType: Exit
peer: cassandra-server:9042
peerId: 0
tags:
- {key: db.type, value: cassandra}
- {key: db.instance, value: demo}
- {key: db.statement, value: 'INSERT INTO demo.test(id, value) VALUES(?,?)'}
- operationName: Cassandra/getUninterruptibly
operationId: 0
parentSpanId: 7
spanId: 8
spanLayer: Database
startTime: nq 0
endTime: nq 0
componentId: 69
componentName: ''
isError: false
spanType: Local
peer: ''
peerId: 0
tags:
- {key: db.type, value: cassandra}
- operationName: Cassandra/execute
operationId: 0
parentSpanId: 0
spanId: 7
spanLayer: Database
startTime: nq 0
endTime: nq 0
componentId: 69
componentName: ''
isError: false
spanType: Exit
peer: cassandra-server:9042
peerId: 0
tags:
- {key: db.type, value: cassandra}
- {key: db.instance, value: demo}
- {key: db.statement, value: 'SELECT * FROM demo.test WHERE id = ?'}
- operationName: Cassandra/getUninterruptibly
operationId: 0
parentSpanId: 9
spanId: 10
spanLayer: Database
startTime: nq 0
endTime: nq 0
componentId: 69
componentName: ''
isError: false
spanType: Local
peer: ''
peerId: 0
tags:
- {key: db.type, value: cassandra}
- operationName: Cassandra/execute
operationId: 0
parentSpanId: 0
spanId: 9
spanLayer: Database
startTime: nq 0
endTime: nq 0
componentId: 69
componentName: ''
isError: false
spanType: Exit
peer: cassandra-server:9042
peerId: 0
tags:
- {key: db.type, value: cassandra}
- {key: db.instance, value: demo}
- {key: db.statement, value: 'DELETE FROM demo.test WHERE id = ?'}
- operationName: Cassandra/getUninterruptibly
operationId: 0
parentSpanId: 11
spanId: 12
spanLayer: Database
startTime: nq 0
endTime: nq 0
componentId: 69
componentName: ''
isError: false
spanType: Local
peer: ''
peerId: 0
tags:
- {key: db.type, value: cassandra}
- operationName: Cassandra/execute
operationId: 0
parentSpanId: 0
spanId: 11
spanLayer: Database
startTime: nq 0
endTime: nq 0
componentId: 69
componentName: ''
isError: false
spanType: Exit
peer: cassandra-server:9042
peerId: 0
tags:
- {key: db.type, value: cassandra}
- {key: db.instance, value: ''}
- {key: db.statement, value: DROP TABLE IF EXISTS demo.test}
- operationName: Cassandra/getUninterruptibly
operationId: 0
parentSpanId: 13
spanId: 14
spanLayer: Database
startTime: nq 0
endTime: nq 0
componentId: 69
componentName: ''
isError: false
spanType: Local
peer: ''
peerId: 0
tags:
- {key: db.type, value: cassandra}
- operationName: Cassandra/execute
operationId: 0
parentSpanId: 0
spanId: 13
spanLayer: Database
startTime: nq 0
endTime: nq 0
componentId: 69
componentName: ''
isError: false
spanType: Exit
peer: cassandra-server:9042
peerId: 0
tags:
- {key: db.type, value: cassandra}
- {key: db.instance, value: ''}
- {key: db.statement, value: DROP KEYSPACE IF EXISTS demo}
- operationName: Cassandra/executeAsync
operationId: 0
parentSpanId: 0
spanId: 15
spanLayer: Database
startTime: nq 0
endTime: nq 0
componentId: 69
componentName: ''
isError: false
spanType: Exit
peer: cassandra-server:9042
peerId: 0
tags:
- {key: db.type, value: cassandra}
- {key: db.instance, value: ''}
- {key: db.statement, value: 'CREATE KEYSPACE IF NOT EXISTS demo WITH replication
= {''class'': ''SimpleStrategy'', ''replication_factor'': 1}'}
- operationName: Cassandra/getUninterruptibly
operationId: 0
parentSpanId: 0
spanId: 16
spanLayer: Database
startTime: nq 0
endTime: nq 0
componentId: 69
componentName: ''
isError: false
spanType: Local
peer: ''
peerId: 0
tags:
- {key: db.type, value: cassandra}
- operationName: Cassandra/executeAsync
operationId: 0
parentSpanId: 0
spanId: 17
spanLayer: Database
startTime: nq 0
endTime: nq 0
componentId: 69
componentName: ''
isError: false
spanType: Exit
peer: cassandra-server:9042
peerId: 0
tags:
- {key: db.type, value: cassandra}
- {key: db.instance, value: ''}
- {key: db.statement, value: 'CREATE TABLE IF NOT EXISTS demo.test(id TEXT PRIMARY
KEY, value TEXT)'}
- operationName: Cassandra/getUninterruptibly
operationId: 0
parentSpanId: 0
spanId: 18
spanLayer: Database
startTime: nq 0
endTime: nq 0
componentId: 69
componentName: ''
isError: false
spanType: Local
peer: ''
peerId: 0
tags:
- {key: db.type, value: cassandra}
- operationName: Cassandra/executeAsync
operationId: 0
parentSpanId: 0
spanId: 19
spanLayer: Database
startTime: nq 0
endTime: nq 0
componentId: 69
componentName: ''
isError: false
spanType: Exit
peer: cassandra-server:9042
peerId: 0
tags:
- {key: db.type, value: cassandra}
- {key: db.instance, value: demo}
- {key: db.statement, value: 'INSERT INTO demo.test(id, value) VALUES(?,?)'}
- operationName: Cassandra/getUninterruptibly
operationId: 0
parentSpanId: 0
spanId: 20
spanLayer: Database
startTime: nq 0
endTime: nq 0
componentId: 69
componentName: ''
isError: false
spanType: Local
peer: ''
peerId: 0
tags:
- {key: db.type, value: cassandra}
- operationName: Cassandra/executeAsync
operationId: 0
parentSpanId: 0
spanId: 21
spanLayer: Database
startTime: nq 0
endTime: nq 0
componentId: 69
componentName: ''
isError: false
spanType: Exit
peer: cassandra-server:9042
peerId: 0
tags:
- {key: db.type, value: cassandra}
- {key: db.instance, value: demo}
- {key: db.statement, value: 'SELECT * FROM demo.test WHERE id = ?'}
- operationName: Cassandra/getUninterruptibly
operationId: 0
parentSpanId: 0
spanId: 22
spanLayer: Database
startTime: nq 0
endTime: nq 0
componentId: 69
componentName: ''
isError: false
spanType: Local
peer: ''
peerId: 0
tags:
- {key: db.type, value: cassandra}
- operationName: Cassandra/executeAsync
operationId: 0
parentSpanId: 0
spanId: 23
spanLayer: Database
startTime: nq 0
endTime: nq 0
componentId: 69
componentName: ''
isError: false
spanType: Exit
peer: cassandra-server:9042
peerId: 0
tags:
- {key: db.type, value: cassandra}
- {key: db.instance, value: demo}
- {key: db.statement, value: 'DELETE FROM demo.test WHERE id = ?'}
- operationName: Cassandra/getUninterruptibly
operationId: 0
parentSpanId: 0
spanId: 24
spanLayer: Database
startTime: nq 0
endTime: nq 0
componentId: 69
componentName: ''
isError: false
spanType: Local
peer: ''
peerId: 0
tags:
- {key: db.type, value: cassandra}
- operationName: Cassandra/executeAsync
operationId: 0
parentSpanId: 0
spanId: 25
spanLayer: Database
startTime: nq 0
endTime: nq 0
componentId: 69
componentName: ''
isError: false
spanType: Exit
peer: cassandra-server:9042
peerId: 0
tags:
- {key: db.type, value: cassandra}
- {key: db.instance, value: ''}
- {key: db.statement, value: DROP TABLE IF EXISTS demo.test}
- operationName: Cassandra/getUninterruptibly
operationId: 0
parentSpanId: 0
spanId: 26
spanLayer: Database
startTime: nq 0
endTime: nq 0
componentId: 69
componentName: ''
isError: false
spanType: Local
peer: ''
peerId: 0
tags:
- {key: db.type, value: cassandra}
- operationName: Cassandra/executeAsync
operationId: 0
parentSpanId: 0
spanId: 27
spanLayer: Database
startTime: nq 0
endTime: nq 0
componentId: 69
componentName: ''
isError: false
spanType: Exit
peer: cassandra-server:9042
peerId: 0
tags:
- {key: db.type, value: cassandra}
- {key: db.instance, value: ''}
- {key: db.statement, value: DROP KEYSPACE IF EXISTS demo}
- operationName: Cassandra/getUninterruptibly
operationId: 0
parentSpanId: 0
spanId: 28
spanLayer: Database
startTime: nq 0
endTime: nq 0
componentId: 69
componentName: ''
isError: false
spanType: Local
peer: ''
peerId: 0
tags:
- {key: db.type, value: cassandra}
- operationName: /cassandra-java-driver-3.x-scenario/case/cassandra
operationId: 0
parentSpanId: -1
spanId: 0
spanLayer: Http
startTime: nq 0
endTime: nq 0
componentId: 1
componentName: ''
isError: false
spanType: Entry
peer: ''
peerId: 0
tags:
- {key: url, value: 'http://localhost:8080/cassandra-java-driver-3.x-scenario/case/cassandra'}
- {key: http.method, value: GET}

View File

@ -0,0 +1,30 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
type: jvm
entryService: http://localhost:8080/cassandra-java-driver-3.x-scenario/case/cassandra
healthCheck: http://localhost:8080/cassandra-java-driver-3.x-scenario/case/healthCheck
startScript: ./bin/startup.sh
framework: cassandra-java-driver-3.x-scenario
environment:
- cassandra.host=cassandra-server
- cassandra.port=9042
dependencies:
cassandra-server:
image: cassandra:3.11.4
hostname: cassandra-server
expose:
- 9042

View File

@ -0,0 +1,140 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to You under the Apache License, Version 2.0
~ (the "License"); you may not use this file except in compliance with
~ the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
~
-->
<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">
<groupId>org.apache.skywalking.apm.testcase</groupId>
<artifactId>cassandra-java-driver-3.x-scenario</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<modelVersion>4.0.0</modelVersion>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<compiler.version>1.8</compiler.version>
<test.framework.version>3.7.2</test.framework.version>
<spring-boot-version>2.1.6.RELEASE</spring-boot-version>
</properties>
<name>skywalking-cassandra-java-driver-3.x-scenario</name>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot-version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-core</artifactId>
<version>${test.framework.version}</version>
</dependency>
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-mapping</artifactId>
<version>${test.framework.version}</version>
<exclusions>
<exclusion>
<artifactId>cassandra-driver-core</artifactId>
<groupId>com.datastax.cassandra</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-extras</artifactId>
<version>${test.framework.version}</version>
<exclusions>
<exclusion>
<artifactId>cassandra-driver-core</artifactId>
<groupId>com.datastax.cassandra</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
</dependencies>
<build>
<finalName>cassandra-java-driver-3.x-scenario</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${compiler.version}</source>
<target>${compiler.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>assemble</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
<outputDirectory>./target/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to You under the Apache License, Version 2.0
~ (the "License"); you may not use this file except in compliance with
~ the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
~
-->
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>./bin</directory>
<fileMode>0775</fileMode>
</fileSet>
</fileSets>
<files>
<file>
<source>${project.build.directory}/cassandra-java-driver-3.x-scenario.jar</source>
<outputDirectory>./libs</outputDirectory>
<fileMode>0775</fileMode>
</file>
</files>
</assembly>

View File

@ -0,0 +1,37 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.skywalking.apm.testcase.cassandra;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author aderm
*/
@SpringBootApplication
public class Application {
public static void main(String[] args) {
try {
SpringApplication.run(Application.class, args);
} catch (Exception e) {
// Never do this
}
}
}

View File

@ -0,0 +1,172 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.skywalking.apm.testcase.cassandra.controller;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.ResultSetFuture;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/case")
public class CaseController {
@Value("${cassandra.host}")
private String host;
@Value("${cassandra.port}")
private int port;
private static final String TEST_EXIST_SQL = "SELECT now() FROM system.local";
private static final String CREATE_KEYSPACE_SQL = "CREATE KEYSPACE IF NOT EXISTS demo WITH replication = " +
"{'class': 'SimpleStrategy', 'replication_factor': 1}";
private static final String CREATE_TABLE_SQL = "CREATE TABLE IF NOT EXISTS demo.test(id TEXT PRIMARY KEY, value TEXT)";
private static final String INSERT_DATA_SQL = "INSERT INTO demo.test(id, value) VALUES(?,?)";
private static final String SELECT_DATA_SQL = "SELECT * FROM demo.test WHERE id = ?";
private static final String DELETE_DATA_SQL = "DELETE FROM demo.test WHERE id = ?";
private static final String DROP_TABLE_SQL = "DROP TABLE IF EXISTS demo.test";
private static final String DROP_KEYSPACE = "DROP KEYSPACE IF EXISTS demo";
private static final Logger logger = LogManager.getLogger(CaseController.class);
private static final String SUCCESS = "Success";
@RequestMapping("/cassandra")
@ResponseBody
public String testcase() {
logger.info("cassandra contact points: {}:{}", host, port);
Cluster cluster = null;
Session session = null;
try {
cluster = Cluster.builder().addContactPoint(host).withPort(port).withoutJMXReporting().build();
session = cluster.connect();
logger.info("cassandra connection open");
execute(session);
executeAsync(session);
return SUCCESS;
} finally {
if (session != null) {
session.close();
}
if (cluster != null) {
cluster.close();
}
logger.info("cassandra connection close");
}
}
private void execute(Session session) {
logger.info("execute in sync");
ResultSet createKeyspaceDataResultSet = session.execute(CREATE_KEYSPACE_SQL);
logger.info("CREATE KEYSPACE result: " + createKeyspaceDataResultSet.toString());
ResultSet createTableDataResultSet = session.execute(CREATE_TABLE_SQL);
logger.info("CREATE TABLE result: " + createTableDataResultSet.toString());
PreparedStatement insertDataPreparedStatement = session.prepare(INSERT_DATA_SQL);
ResultSet insertDataResultSet = session.execute(insertDataPreparedStatement.bind("101", "foobar"));
logger.info("INSERT result: " + insertDataResultSet.toString());
PreparedStatement selectDataPreparedStatement = session.prepare(SELECT_DATA_SQL);
ResultSet resultSet = session.execute(selectDataPreparedStatement.bind("101"));
Row row = resultSet.one();
logger.info("SELECT result: id: {}, value: {}", row.getString("id"), row.getString("value"));
PreparedStatement deleteDataPreparedStatement = session.prepare(DELETE_DATA_SQL);
ResultSet deleteDataResultSet = session.execute(deleteDataPreparedStatement.bind("101"));
logger.info("DELETE result: " + deleteDataResultSet.toString());
ResultSet dropTableDataResultSet = session.execute(DROP_TABLE_SQL);
logger.info("DROP TABLE result: " + dropTableDataResultSet.toString());
ResultSet dropKeyspaceDataResultSet = session.execute(DROP_KEYSPACE);
logger.info("DROP KEYSPACE result: " + dropKeyspaceDataResultSet.toString());
}
private void executeAsync(Session session) {
logger.info("execute in async");
ResultSetFuture createKeyspaceDataResultSetFuture = session.executeAsync(CREATE_KEYSPACE_SQL);
ResultSet createKeyspaceDataResultSet = createKeyspaceDataResultSetFuture.getUninterruptibly();
logger.info("CREATE KEYSPACE result: " + createKeyspaceDataResultSet.toString());
ResultSetFuture createTableDataResultSetFuture = session.executeAsync(CREATE_TABLE_SQL);
ResultSet createTableDataResultSet = createTableDataResultSetFuture.getUninterruptibly();
logger.info("CREATE TABLE result: " + createTableDataResultSet.toString());
PreparedStatement insertDataPreparedStatement = session.prepare(INSERT_DATA_SQL);
ResultSetFuture insertDataResultSetFuture = session.executeAsync(insertDataPreparedStatement.bind("101", "foobar"));
ResultSet insertDataResultSet = insertDataResultSetFuture.getUninterruptibly();
logger.info("INSERT result: " + insertDataResultSet.toString());
PreparedStatement selectDataPreparedStatement = session.prepare(SELECT_DATA_SQL);
ResultSetFuture resultSetFuture = session.executeAsync(selectDataPreparedStatement.bind("101"));
ResultSet resultSet = resultSetFuture.getUninterruptibly();
Row row = resultSet.one();
logger.info("SELECT result: id: {}, value: {}", row.getString("id"), row.getString("value"));
PreparedStatement deleteDataPreparedStatement = session.prepare(DELETE_DATA_SQL);
ResultSetFuture deleteDataResultSetFuture = session.executeAsync(deleteDataPreparedStatement.bind("101"));
ResultSet deleteDataResultSet = deleteDataResultSetFuture.getUninterruptibly();
logger.info("DELETE result: " + deleteDataResultSet.toString());
ResultSetFuture dropTableDataResultSetFuture = session.executeAsync(DROP_TABLE_SQL);
ResultSet dropTableDataResultSet = dropTableDataResultSetFuture.getUninterruptibly();
logger.info("DROP TABLE result: " + dropTableDataResultSet.toString());
ResultSetFuture dropKeyspaceDataResultSetFuture = session.executeAsync(DROP_KEYSPACE);
ResultSet dropKeyspaceDataResultSet = dropKeyspaceDataResultSetFuture.getUninterruptibly();
logger.info("DROP KEYSPACE result: " + dropKeyspaceDataResultSet.toString());
}
@RequestMapping("/healthCheck")
@ResponseBody
public String healthCheck() {
Cluster cluster = null;
Session session = null;
try {
cluster = Cluster.builder().addContactPoint(host).withPort(port).withoutJMXReporting().build();
session = cluster.connect();
logger.info("cassandra connection open");
PreparedStatement testPreparedStatement = session.prepare(TEST_EXIST_SQL);
logger.info("Test result:" + testPreparedStatement.toString());
} finally {
if (session != null) {
session.close();
}
if (cluster != null) {
cluster.close();
}
logger.info("cassandra connection close");
}
return SUCCESS;
}
}

View File

@ -0,0 +1,23 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
server:
port: 8080
servlet:
context-path: /cassandra-java-driver-3.x-scenario
logging:
config: classpath:log4j2.xml

View File

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to You under the Apache License, Version 2.0
~ (the "License"); you may not use this file except in compliance with
~ the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
~
-->
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_ERR">
<PatternLayout charset="UTF-8" pattern="[%d{yyyy-MM-dd HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="WARN">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>

View File

@ -0,0 +1,19 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
3.7.0
3.7.1
3.7.2