Skip to content

Commit

Permalink
Merge pull request #58 from daizhenyu/database-write-prohibition-demo
Browse files Browse the repository at this point in the history
database-write-prohibition plugin demo
  • Loading branch information
lilai23 authored Apr 1, 2024
2 parents 0daa232 + 919e70c commit 795b25d
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 1 deletion.
10 changes: 10 additions & 0 deletions .github/workflows/create_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,16 @@ jobs:
asset_path: ${{ github.workspace }}/package/result/sermant-examples-mq-consume-prohibition-demo-${{ env.version }}.tar.gz
asset_name: sermant-examples-mq-consume-prohibition-demo-${{ env.version }}.tar.gz
asset_content_type: application/tar
- name: Upload Release database-write-prohibition-demo # database-write-prohibition-demo release包
id: upload-release-asset-databse-write-prohibition
uses: actions/upload-release-asset@v1.0.2
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ${{ github.workspace }}/package/result/sermant-examples-database-write-prohibition-demo-${{ env.version }}.tar.gz
asset_name: sermant-examples-database-write-prohibition-demo-${{ env.version }}.tar.gz
asset_content_type: application/tar
- name: Upload Release first-plugin-demo # first-plugin-demo release包
id: upload-release-asset-first-plugin
uses: actions/upload-release-asset@v1.0.2
Expand Down
43 changes: 43 additions & 0 deletions database-write-prohibition-demo/mongodb-demo/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?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">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.15</version>
<relativePath/>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>mongodb-demo</artifactId>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<mongodb.version>4.7.0</mongodb.version>
</properties>

<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>${mongodb.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (C) 2024-2024 Huawei Technologies Co., Ltd. All rights reserved.
*
* Licensed 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 com.huaweicloud.sermant.mongodb;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* springboot startup class
*
* @author daizhenyu
* @since 2024-03-05
**/
@SpringBootApplication
public class MongoDbApplication {
/**
* startup class
*
* @param args process startup input parameters
*/
public static void main(String[] args) {
SpringApplication.run(MongoDbApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (C) 2024-2024 Huawei Technologies Co., Ltd. All rights reserved.
*
* Licensed 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 com.huaweicloud.sermant.mongodb.controller;

import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.MongoIterable;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* operate mongodb
*
* @author daizhenyu
* @since 2024-03-05
**/
@RestController
public class MongoDbController {
private static final int STRING_LENGTH = 21;

@Value("${mongodb.address}")
private String mongoDbAddress;

@RequestMapping("createCollection")
public String createCollection(String databaseName, String collectionName) {
try (MongoClient mongoClient = MongoClients.create(mongoDbAddress)) {
MongoDatabase database = mongoClient.getDatabase(databaseName);
database.createCollection(collectionName);
} catch (Exception e) {
return "Collection " + collectionName + " failed to be created";
}
return "Collection " + collectionName + " successfully created";
}

@RequestMapping("queryCollection")
public String queryCollection(String databaseName) {
try (MongoClient mongoClient = MongoClients.create(mongoDbAddress)) {
MongoDatabase database = mongoClient.getDatabase(databaseName);
MongoIterable<String> strings = database.listCollectionNames();
return buildCollectionsString(strings);
}
}

private String buildCollectionsString(MongoIterable<String> strings) {
StringBuilder builder = new StringBuilder();
builder.append("Current Collection: [");
for (String string : strings) {
builder.append(string + ", ");
}
if (builder.length() != STRING_LENGTH) {
builder.deleteCharAt(builder.length() - 1);
builder.deleteCharAt(builder.length() - 1);
}
builder.append("]");
return builder.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mongodb.address=mongodb://localhost:27017
server.port=30110
21 changes: 21 additions & 0 deletions database-write-prohibition-demo/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?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">
<parent>
<artifactId>sermant-examples</artifactId>
<groupId>com.huaweicloud.sermant.examples</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<modules>
<module>mongodb-demo</module>
</modules>
<artifactId>database-write-prohibition-demo</artifactId>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<module>monitor-demo</module>
<module>mq-consume-prohibition-demo</module>
<module>loadbalancer-demo</module>
<module>database-write-prohibition-demo</module>
</modules>

<properties>
Expand Down
6 changes: 5 additions & 1 deletion scripts/copy_jar.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ mkdir -p package/springboot-registry-demo
mkdir -p package/visibility-demo
mkdir -p package/removal-demo
mkdir -p package/mq-consume-prohibition-demo
mkdir -p package/database-write-prohibition-demo
mkdir -p package/first-plugin-demo

# 按照文件名模式将对应的jar文件复制到对应目录
Expand Down Expand Up @@ -62,6 +63,8 @@ find . -type f -name "dubbo-integration-provider.jar" -exec cp -v {} package/vis
find . -type f -name "dubbo-integration-consumer.jar" -exec cp -v {} package/visibility-demo/ \;
# 消息队列禁止消费
find . -type f -name "kafka-demo.jar" -exec cp -v {} package/mq-consume-prohibition-demo \;
# 数据库禁消费
find . -type f -name "mongodb-demo.jar" -exec cp -v {} package/database-write-prohibition-demo \;
# 离群实例摘除
find . -type f -name "rest-consumer.jar" -exec cp -v {} package/removal-demo/ \;
find . -type f -name "rest-provider.jar" -exec cp -v {} package/removal-demo/ \;
Expand All @@ -80,4 +83,5 @@ tar -czvf package/result/sermant-examples-registry-demo-$*.tar.gz -C package/reg
tar -czvf package/result/sermant-examples-springboot-registry-demo-$*.tar.gz -C package/springboot-registry-demo/ .
tar -czvf package/result/sermant-examples-visibility-demo-$*.tar.gz -C package/visibility-demo/ .
tar -czvf package/result/sermant-examples-removal-demo-$*.tar.gz -C package/removal-demo/ .
tar -czvf package/result/sermant-examples-mq-consume-prohibition-demo-$*.tar.gz -C package/mq-consume-prohibition-demo/ .
tar -czvf package/result/sermant-examples-mq-consume-prohibition-demo-$*.tar.gz -C package/mq-consume-prohibition-demo/ .
tar -czvf package/result/sermant-examples-database-write-prohibition-demo-$*.tar.gz -C package/database-write-prohibition-demo/ .

0 comments on commit 795b25d

Please sign in to comment.