Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9a4cb61

Browse files
committed
add examples
0 parents commit 9a4cb61

27 files changed

+1575
-0
lines changed

‎.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/vendor/
2+
/.idea
3+
/keys
4+
composer.lock
5+
src/jc
6+
docker-compose.jc.yml

‎README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# PHP Kafka Examples
2+
This repository has PHP examples for Kafka consumers / producers for:
3+
- [php-rdkafka](https://github.com/arnaud-lb/php-rdkafka): Examples just using the PHP extension
4+
- [php-kafka-lib](https://github.com/jobcloud/php-kafka-lib): PHP library that relies on [php-rdkafka](https://github.com/arnaud-lb/php-rdkafka) and supports [avro](https://github.com/flix-tech/avro-serde-php)
5+
6+
## Examples
7+
- [php-rdkafka](src/ext-php-rdkafka/pure-php)
8+
- [php-kafka-lib](src/ext-php-rdkafka/php-kafka-lib)
9+
10+
## Start containers for examples
11+
Be sure to start the docker containers.
12+
To do so, run this in the project root:
13+
```bash
14+
docker-compose up -d
15+
docker-compose exec php bash
16+
```
17+
Then follow the instructions in the example folders.
18+
19+
## Customize to fit your setup
20+
If you wan't to test / debug something that is closer to your setup,
21+
you can modify the following arguments in `docker-compose.yml`:
22+
```
23+
PHP_IMAGE_TAG: 7.4-cli-alpine3.11
24+
LIBRDKAFKA_VERSION: v1.4.0
25+
PHP_RDKAFKA_VERSION: 4.0.3
26+
```
27+
Adjust those, to reflect your setup and afterwards run:
28+
```
29+
docker-compose up --build -d
30+
```

‎composer.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"require": {
3+
"ext-json": "*",
4+
"flix-tech/avro-serde-php": "^1.4",
5+
"jobcloud/php-console-kafka-schema-registry": "^1.1",
6+
"jobcloud/php-kafka-lib": "^1.0",
7+
"ramsey/uuid": "^4.0"
8+
},
9+
"require-dev": {
10+
"kwn/php-rdkafka-stubs": "^1.2.0"
11+
}
12+
}

‎docker-compose.centos.yml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
version: '3.7'
2+
services:
3+
php:
4+
build:
5+
context: ./docker/php
6+
dockerfile: Dockerfile.centos
7+
args:
8+
CENTOS_VERSION: 7
9+
PHP_VERSION: 74
10+
LIBRDKAFKA_VERSION: v1.5.0
11+
PHP_RDKAFKA_VERSION: 4.0.3
12+
tty: true
13+
working_dir: /app
14+
volumes:
15+
- ./:/app
16+
17+
zookeeper:
18+
image: confluentinc/cp-zookeeper:5.5.0
19+
environment:
20+
ZOOKEEPER_CLIENT_PORT: 2182
21+
ZOOKEEPER_TICK_TIME: 2000
22+
23+
kafka:
24+
image: confluentinc/cp-kafka:5.5.0
25+
depends_on:
26+
- zookeeper
27+
ports:
28+
- 9096:9096
29+
environment:
30+
KAFKA_BROKER_ID: 1
31+
KAFKA_ZOOKEEPER_CONNECT: 'zookeeper:2182'
32+
KAFKA_ADVERTISED_LISTENERS: 'PLAINTEXT://kafka:9096'
33+
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
34+
KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0
35+
KAFKA_NUM_PARTITIONS: 1
36+
KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1
37+
KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1
38+
39+
kafka-schema-registry:
40+
image: confluentinc/cp-schema-registry:5.5.0
41+
depends_on:
42+
- zookeeper
43+
- kafka
44+
ports:
45+
- "9083:9083"
46+
environment:
47+
SCHEMA_REGISTRY_HOST_NAME: kafka-schema-registry
48+
SCHEMA_REGISTRY_LISTENERS: "http://0.0.0.0:9083"
49+
SCHEMA_REGISTRY_KAFKASTORE_CONNECTION_URL: 'zookeeper:2182'
50+
SCHEMA_REGISTRY_AVRO_COMPATIBILITY_LEVEL: 'full_transitive'

‎docker-compose.yml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
version: '3.7'
2+
services:
3+
php:
4+
build:
5+
context: ./docker/php
6+
dockerfile: Dockerfile.alpine
7+
args:
8+
PHP_IMAGE_TAG: 7.4-cli-alpine3.12
9+
LIBRDKAFKA_VERSION: v1.5.0
10+
PHP_RDKAFKA_VERSION: 4.0.3
11+
tty: true
12+
working_dir: /app
13+
volumes:
14+
- ./:/app
15+
16+
zookeeper:
17+
image: confluentinc/cp-zookeeper:5.5.0
18+
environment:
19+
ZOOKEEPER_CLIENT_PORT: 2182
20+
ZOOKEEPER_TICK_TIME: 2000
21+
22+
kafka:
23+
image: confluentinc/cp-kafka:5.5.0
24+
depends_on:
25+
- zookeeper
26+
ports:
27+
- 9096:9096
28+
environment:
29+
KAFKA_BROKER_ID: 1
30+
KAFKA_ZOOKEEPER_CONNECT: 'zookeeper:2182'
31+
KAFKA_ADVERTISED_LISTENERS: 'PLAINTEXT://kafka:9096'
32+
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
33+
KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0
34+
KAFKA_NUM_PARTITIONS: 1
35+
KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1
36+
KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1
37+
38+
kafka-schema-registry:
39+
image: confluentinc/cp-schema-registry:5.5.0
40+
depends_on:
41+
- zookeeper
42+
- kafka
43+
ports:
44+
- "9083:9083"
45+
environment:
46+
SCHEMA_REGISTRY_HOST_NAME: kafka-schema-registry
47+
SCHEMA_REGISTRY_LISTENERS: "http://0.0.0.0:9083"
48+
SCHEMA_REGISTRY_KAFKASTORE_CONNECTION_URL: 'zookeeper:2182'
49+
SCHEMA_REGISTRY_AVRO_COMPATIBILITY_LEVEL: 'full_transitive'

‎docker/php/Dockerfile.alpine

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
ARG PHP_IMAGE_TAG
2+
3+
FROM php:${PHP_IMAGE_TAG}
4+
5+
ARG LIBRDKAFKA_VERSION
6+
ARG PHP_RDKAFKA_VERSION
7+
8+
# Install packages
9+
RUN apk --no-cache upgrade && \
10+
apk --no-cache add bash openssh sudo git gcc g++ make autoconf \
11+
icu libssl1.1 openssl-dev pcre-dev zlib-dev icu-dev wget gettext valgrind
12+
13+
# Install librdkafka and ext-rdkafka
14+
RUN git clone --depth 1 --branch ${LIBRDKAFKA_VERSION} https://github.com/edenhill/librdkafka.git \
15+
&& cd librdkafka \
16+
&& ./configure \
17+
&& make \
18+
&& make install \
19+
&& git clone --depth 1 --branch ${PHP_RDKAFKA_VERSION} https://github.com/arnaud-lb/php-rdkafka.git \
20+
&& cd php-rdkafka \
21+
&& phpize \
22+
&& ./configure \
23+
&& make all -j 5 \
24+
&& make install \
25+
&& cd ../..;rm -rf librdkafka
26+
27+
# Install php extensions
28+
RUN docker-php-ext-install pcntl && \
29+
docker-php-ext-enable rdkafka pcntl > /dev/null 2>&1
30+
31+
# Install composer and prestissimo
32+
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer \
33+
&& composer global require hirak/prestissimo

‎docker/php/Dockerfile.centos

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
ARG CENTOS_VERSION
2+
3+
FROM centos:${CENTOS_VERSION}
4+
5+
ARG LIBRDKAFKA_VERSION
6+
ARG PHP_RDKAFKA_VERSION
7+
ARG PHP_VERSION
8+
9+
RUN yum install -y http://rpms.remirepo.net/enterprise/remi-release-7.rpm \
10+
&& yum install -y yum-utils \
11+
&& yum-config-manager --enable remi-php${PHP_VERSION} \
12+
&& yum install -y php php-cli php-devel php-pear zlib-devel
13+
14+
RUN yum install -y git gcc gcc-c++ automake autoconf libtool make
15+
16+
# Install librdkafka and ext-rdkafka
17+
RUN git clone --depth 1 --branch ${LIBRDKAFKA_VERSION} https://github.com/edenhill/librdkafka.git \
18+
&& cd librdkafka \
19+
&& ./configure \
20+
&& make \
21+
&& make install \
22+
&& git clone --depth 1 --branch ${PHP_RDKAFKA_VERSION} https://github.com/arnaud-lb/php-rdkafka.git \
23+
&& cd php-rdkafka \
24+
&& phpize \
25+
&& ./configure \
26+
&& make all -j 5 \
27+
&& make install \
28+
&& cd ../..;rm -rf librdkafka \
29+
&& echo "extension=rdkafka" >> /etc/php.d/rdkafka.ini \
30+
&& cp /usr/lib64/php/modules/rdkafka.so /usr/lib64/php/modules/rdkafka
31+
32+
RUN echo $'#!/bin/sh \n\
33+
set -e \n\
34+
exec "$@" \n\
35+
' >> /usr/local/bin/php-entrypoint \
36+
&& chmod +x /usr/local/bin/php-entrypoint
37+
38+
ENTRYPOINT ["php-entrypoint"]
39+
CMD ["php", "-a"]

‎docker/php/Dockerfile.master

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
FROM alpine:3.12
2+
3+
ENV PHP_VERSION nightly
4+
ENV PHP_INI_DIR /usr/local/etc/php
5+
ARG LIBRDKAFKA_VERSION
6+
7+
# Install packages
8+
RUN apk --no-cache upgrade && \
9+
apk --no-cache add bash openssh sudo git gcc g++ make autoconf \
10+
icu libssl1.1 openssl-dev pcre-dev zlib-dev icu-dev wget gettext valgrind
11+
12+
RUN set -xe \
13+
&& apk add --no-cache --virtual .persistent-deps \
14+
ca-certificates \
15+
curl \
16+
tar \
17+
xz \
18+
git
19+
20+
RUN set -xe \
21+
&& apk add --no-cache --virtual .build-deps \
22+
autoconf \
23+
file \
24+
g++ \
25+
gcc \
26+
libc-dev \
27+
make \
28+
pkgconf \
29+
re2c \
30+
curl-dev \
31+
libedit-dev \
32+
libxml2-dev \
33+
openssl-dev \
34+
oniguruma-dev \
35+
sqlite-dev \
36+
bison \
37+
libbz2 \
38+
bzip2-dev \
39+
&& mkdir -p $PHP_INI_DIR/conf.d \
40+
&& git clone https://github.com/php/php-src.git /usr/src/php \
41+
&& cd /usr/src/php \
42+
&& git checkout 67f8e938fd03398b19f6e6fa012f65e74c8ec82a \
43+
&& ./buildconf --force \
44+
&& ./configure \
45+
--with-config-file-path="$PHP_INI_DIR" \
46+
--with-config-file-scan-dir="$PHP_INI_DIR/conf.d" \
47+
--disable-cgi \
48+
--enable-ftp \
49+
--enable-debug \
50+
--enable-mbstring \
51+
--enable-mysqlnd \
52+
--with-curl \
53+
--with-libedit \
54+
--with-openssl \
55+
--with-zlib \
56+
--with-bz2 \
57+
--without-pear \
58+
&& make -j"$(getconf _NPROCESSORS_ONLN)" \
59+
&& make install \
60+
&& rm -rf /usr/src/php \
61+
&& runDeps="$( \
62+
scanelf --needed --nobanner --recursive /usr/local \
63+
| awk '{ gsub(/,/, "\nso:", 2ドル); print "so:" 2ドル }' \
64+
| sort -u \
65+
| xargs -r apk info --installed \
66+
| sort -u \
67+
)" \
68+
&& apk add --no-cache --virtual .php-rundeps $runDeps \
69+
&& apk del .build-deps
70+
71+
# Install librdkafka
72+
RUN git clone --depth 1 --branch ${LIBRDKAFKA_VERSION} https://github.com/edenhill/librdkafka.git \
73+
&& cd librdkafka \
74+
&& ./configure \
75+
&& make \
76+
&& make install
77+
78+
CMD ["php", "-a"]
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Register test schema
2+
Run the following to register the test schema:
3+
```bash
4+
cd src/ext-php-rdkafka/php-kafka-lib
5+
./console kafka-schema-registry:register:changed avroSchema
6+
```
7+
8+
# Running consumer / producer
9+
## Prerequisites
10+
Be sure to do this first: [Start containers](./../../../README.md#start-containers-for-examples)
11+
and to also have registered the test schema (see above).
12+
13+
## Avro producer
14+
Will per default produce 10 avro messages:
15+
```bash
16+
php avroProducer.php
17+
```
18+
19+
## Avro high level consumer
20+
Will consume all messages available:
21+
```bash
22+
php avroHighLevelConsumer.php
23+
```
24+
25+
## Producer
26+
Will per default produce 10 messages:
27+
```bash
28+
php producer.php
29+
```
30+
31+
## Low level consumer
32+
Will consume all messages available for partition 0:
33+
```bash
34+
php lowLevelConsumer.php
35+
```
36+
37+
## High level consumer
38+
Will consume all messages available:
39+
```bash
40+
php highLevelConsumer.php
41+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /