|
| 1 | +# Compose sample application |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +This sample demonstrates a WebAssembly (Wasm) microservice written in Rust. It subscribes to a Kafka queue topic on a Redpanda server, and then transforms and saves each message into a MySQL (MariaDB) database table. The microservice is compiled into Wasm and runs in the WasmEdge runtime, which is a secure and lightweight alternative to natively compiled Rust apps in Linux containers. |
| 6 | + |
| 7 | +## Use with Docker Development Environments |
| 8 | + |
| 9 | +You will need a version of Docker Desktop or Docker CLI with Wasm support. |
| 10 | + |
| 11 | +* [Install Docker Desktop + Wasm (Beta)](https://docs.docker.com/desktop/wasm/) |
| 12 | +* [Install Docker CLI + Wasm](https://github.com/chris-crone/wasm-day-na-22/tree/main/server) |
| 13 | + |
| 14 | +## WasmEdge server with Redpanda and MySQL database |
| 15 | + |
| 16 | +Project structure: |
| 17 | + |
| 18 | +``` |
| 19 | +. |
| 20 | ++-- compose.yml |
| 21 | +|-- etl |
| 22 | + |-- Dockerfile |
| 23 | + |-- Cargo.toml |
| 24 | + +-- src |
| 25 | + |-- main.rs |
| 26 | +|-- kafka |
| 27 | + |-- order.json |
| 28 | +|-- db |
| 29 | + |-- db-password.txt |
| 30 | +``` |
| 31 | + |
| 32 | +The [compose.yml](compose.yml) is as follows. |
| 33 | + |
| 34 | +```yaml |
| 35 | +services: |
| 36 | + redpanda: |
| 37 | + image: docker.redpanda.com/vectorized/redpanda:v22.2.2 |
| 38 | + command: |
| 39 | + - redpanda start |
| 40 | + - --smp 1 |
| 41 | + - --overprovisioned |
| 42 | + - --node-id 0 |
| 43 | + - --kafka-addr PLAINTEXT://0.0.0.0:29092,OUTSIDE://0.0.0.0:9092 |
| 44 | + - --advertise-kafka-addr PLAINTEXT://redpanda:29092,OUTSIDE://redpanda:9092 |
| 45 | + - --pandaproxy-addr 0.0.0.0:8082 |
| 46 | + - --advertise-pandaproxy-addr localhost:8082 |
| 47 | + ports: |
| 48 | + - 8081:8081 |
| 49 | + - 8082:8082 |
| 50 | + - 9092:9092 |
| 51 | + - 9644:9644 |
| 52 | + - 29092:29092 |
| 53 | + volumes: |
| 54 | + - ./kafka:/app |
| 55 | + |
| 56 | + etl: |
| 57 | + image: etl-kafka |
| 58 | + build: |
| 59 | + context: etl |
| 60 | + platforms: |
| 61 | + - wasi/wasm32 |
| 62 | + environment: |
| 63 | + DATABASE_URL: mysql://root:whalehello@db:3306/mysql |
| 64 | + KAFKA_URL: kafka://redpanda:9092/order |
| 65 | + RUST_BACKTRACE: full |
| 66 | + RUST_LOG: info |
| 67 | + restart: unless-stopped |
| 68 | + runtime: io.containerd.wasmedge.v1 |
| 69 | + |
| 70 | + db: |
| 71 | + image: mariadb:10.9 |
| 72 | + environment: |
| 73 | + MYSQL_ROOT_PASSWORD: whalehello |
| 74 | +``` |
| 75 | +
|
| 76 | +The compose file defines an application with three services `redpanda`, `etl` and `db`. The `redpanda` service is a Kafka-compatible messaging server that produces messages in a queue topic. The `etl` service, in the WasmEdge container that subscribes to the queue topic and receives incoming messages. Each incoming message is parsed and stored in the `db` MySQL (MariaDB) database server. |
| 77 | + |
| 78 | +## Deploy with docker compose |
| 79 | + |
| 80 | +```bash |
| 81 | +$ docker compose up -d |
| 82 | +... |
| 83 | + ⠿ Network wasmedge-kafka-mysql_default Created 0.1s |
| 84 | + ⠿ Container wasmedge-kafka-mysql-redpanda-1 Created 0.3s |
| 85 | + ⠿ Container wasmedge-kafka-mysql-etl-1 Created 0.3s |
| 86 | + ⠿ Container wasmedge-kafka-mysql-db-1 Created 0.3s |
| 87 | +``` |
| 88 | + |
| 89 | +## Expected result |
| 90 | + |
| 91 | +```bash |
| 92 | +$ docker compose ps |
| 93 | +NAME COMMAND SERVICE STATUS PORTS |
| 94 | +wasmedge-kafka-mysql-db-1 "docker-entrypoint.s…" db running 3306/tcp |
| 95 | +wasmedge-kafka-mysql-etl-1 "kafka.wasm" etl running |
| 96 | +wasmedge-kafka-mysql-redpanda-1 "/entrypoint.sh 'red…" redpanda running 0.0.0.0:8081-8082->8081-8082/tcp, :::8081-8082->8081-8082/tcp, 0.0.0.0:9092->9092/tcp, :::9092->9092/tcp, 0.0.0.0:9644->9644/tcp, :::9644->9644/tcp, 0.0.0.0:29092->29092/tcp, :::29092->29092/tcp |
| 97 | +``` |
| 98 | + |
| 99 | +After the application starts, |
| 100 | +log into the Redpanda container and send a message to the queue topic `order` as follows. |
| 101 | + |
| 102 | +```bash |
| 103 | +$ docker compose exec redpanda /bin/bash |
| 104 | +redpanda@1add2615774b:/$ cd /app |
| 105 | +redpanda@1add2615774b:/app$ cat order.json | rpk topic produce order |
| 106 | +Produced to partition 0 at offset 0 with timestamp 1667922788523. |
| 107 | +``` |
| 108 | + |
| 109 | +To see the data in the database container, you can use the following commands. |
| 110 | + |
| 111 | +```bash |
| 112 | +$ docker compose exec db /bin/bash |
| 113 | +root@c97c472db02e:/# mysql -u root -pwhalehello mysql |
| 114 | +mysql> select * from orders; |
| 115 | +... ... |
| 116 | +``` |
| 117 | + |
0 commit comments