A simple demo application for building scalable applications using Microservices, Database Sharding Proxies, Flexible Transactions, APM tools, and deploying to Docker and Kubernetes.
- Dubbo: A high-performance, java based open source RPC framework.
Dubbo
employs a client based, decentralized load balance mechanism. Consumers fetch providers from registry servers to client at startup, create long living TCP connections and comunicate directly to providers (fordubbo/thrift
protocols), with variant configurable load balance and fail over algorithms. Availability of providers is detected by heartbeat, and new provider registration events are notified by registry servers to all consumers.
Dubbo
provides better performance thanSpring Cloud
, it can be deployed and scaled inKubernetes
, using its own service discovery, load balance and fail over mechanisms, but it's difficult to work withIstio
. - Nacos: A naming and config service, providing more enhancements on service discovery and flow control than Zookeeper.
- ShardingProxy, Mycat: Both are database sharding proxies, providing a transparent database sharding solution.
The internals are very similar, both implementMySQL
protocol to comunicate with cross platform applications, intercept SQL queries and route to backendMySQL
servers based on sharding keys and configurable sharding rules. Although it's not recommemded but both of them support cross-sharding queries (without sharding keys in SQL), rewrite SQL if necessary, dispatch queries to all backendMySQL
servers, gather results and do aggregation, sorting, pagination in proxy memory, and return result to client. - Seata: A flexible transaction framework for distributed applications.
Seata
implements three transaction modes: AT, TCC and SAGA. The demo application usesAT
mode, it's transparent for application code. - ZipKin, PinPoint, SkyWalking: APM tools for microservices,
ZipKin
andSkyWalking
can work withIstio
.
- OS: Linux, Mac, or Windows with a bash shell, such as git bash;
In Mac OSX
gnu-sed
is required:brew install gnu-sed
- JDK 8+ and apache maven;
Docker
, 6GB memory forDocker Desktop
is recommended;
Use package.sh to compile and package the demo application.
Usage:
- Options to enable database sharding:
-mycat
,-shardingproxy
- Options to enable global transaction management:
-seata
- Options to enable APM:
-skywalking
,-pinpoint
,-zipkin
Example:
package.sh -mycat -zipkin
package.sh -shardingproxy -pinpoint -seata
MySQL
andNacos
must be installed, see MySQL scripts and Nacos quickstart.- Install and start 3-party components as you wanted, go to project home for help, and Dockerfiles and scripts in demo application are also references for you.
- Edit parent pom.xml and config for your local environment.
- Use
package.sh
to compile and package demo application. - Start demo application as following steps: \
- Neither
SkyWalking
norPinPoint
is enabled:java -jar item-service\target\item-service-0.0.1-SNAPSHOT.jar java -jar stock-service\target\stock-service-0.0.1-SNAPSHOT.jar java -jar user-service\target\user-service-0.0.1-SNAPSHOT.jar java -jar order-service\target\order-service-0.0.1-SNAPSHOT.jar java -jar shop-web\target\shop-web-0.0.1-SNAPSHOT.jar
SkyWalking
is enabled:java -javaagent:F:\sw\agent\skywalking-agent.jar -Dskywalking.agent.service_name=svc-item -jar item-service\target\item-service-0.0.1-SNAPSHOT.jar java -javaagent:F:\sw\agent\skywalking-agent.jar -Dskywalking.agent.service_name=svc-stock -jar stock-service\target\stock-service-0.0.1-SNAPSHOT.jar java -javaagent:F:\sw\agent\skywalking-agent.jar -Dskywalking.agent.service_name=svc-user -jar user-service\target\user-service-0.0.1-SNAPSHOT.jar java -javaagent:F:\sw\agent\skywalking-agent.jar -Dskywalking.agent.service_name=svc-order -jar order-service\target\order-service-0.0.1-SNAPSHOT.jar java -javaagent:F:\sw\agent\skywalking-agent.jar -Dskywalking.agent.service_name=shop-web -jar shop-web\target\shop-web-0.0.1-SNAPSHOT.jar
PinPoint
is enabled:java -javaagent:F:\pinpoint\agent\pinpoint-bootstrap-1.8.5.jar -Dpinpoint.agentId=svc-item-1 -Dpinpoint.applicationName=svc-item -jar item-service\target\item-service-0.0.1-SNAPSHOT.jar java -javaagent:F:\pinpoint\agent\pinpoint-bootstrap-1.8.5.jar -Dpinpoint.agentId=svc-stock-1 -Dpinpoint.applicationName=svc-stock -jar stock-service\target\stock-service-0.0.1-SNAPSHOT.jar java -javaagent:F:\pinpoint\agent\pinpoint-bootstrap-1.8.5.jar -Dpinpoint.agentId=svc-user-1 -Dpinpoint.applicationName=svc-user -jar user-service\target\user-service-0.0.1-SNAPSHOT.jar java -javaagent:F:\pinpoint\agent\pinpoint-bootstrap-1.8.5.jar -Dpinpoint.agentId=svc-order-1 -Dpinpoint.applicationName=svc-order -jar order-service\target\order-service-0.0.1-SNAPSHOT.jar java -javaagent:F:\pinpoint\agent\pinpoint-bootstrap-1.8.5.jar -Dpinpoint.agentId=shop-web-1 -Dpinpoint.applicationName=shop-web -jar shop-web\target\shop-web-0.0.1-SNAPSHOT.jar
- Neither
Entrypoints:
- Demo Application: localhost:8090/shop
Nacos
: localhost:8848/nacos, user/password:nacos/nacos
ZipKin
: localhost:9411/zipkinSkyWalking
: localhost:8080PinPoint
:localhost:{your-port}
Mycat
: data port8066
, management port is9066
, usemysql
to connect, user/password:mydemo/mydemo
In Mac OSX,
--protocol
must be specified:mysql -h localhost -P 8066 -umydemo -pmydemo --protocol=TCP
ShardingProxy
:3307
, usemysql
to connect, user/password:mydemo/mydemo
- Build Docker images for all 3-party components used in demo application.
ATTENTION: Docker support for
PinPoint
is not provided in demo application,-pinpoint
option cann't be used inpackage.sh
if you run the demo application in Docker.docker/build-basis.sh
- Run Docker containers for all 3-party components. It's recommended for you that take
docker/deploy-basis.sh
as a reference, and only run those containers you wanted, to avoid Docker hungs because of memory pressure.All containers run in adocker/deploy-basis.sh
Docker Network
mydemo
created indocker/deploy-basis.sh
,docker-compose
is not used. - Use docker/deploy-mydemo.sh to build images and run containers for demo application.
Usage:-build
: Build images foritem
,stock
,user
,order
services andshop-web
app.-run
: Run containers foritem
,stock
,user
,order
services andshop-web
app.-stop
: Stop containers foritem
,stock
,user
,order
services andshop-web
app.-rm
: Remove containers foritem
,stock
,user
,order
services andshop-web
app.-rmi
: Remove images foritem
,stock
,user
,order
services andshop-web
app.
Example:
docker/build-basis.sh
docker/deploy-basis.sh
# Run demo application with Mycat, Seata and ZipKin
package.sh -mycat -seata -zipkin
docker/deploy-mydemo.sh -build -run
# Run demo application with ShardingProxy and SkyWalking
package.sh -shardingproxy -skywaling
docker/deploy-mydemo.sh -stop -rm -rmi -build -run
Entrypoints:
- Demo Application: localhost:18090/shop
Nacos
: localhost:18848/nacos, user/password:nacos/nacos
ZipKin
: localhost:19411/zipkinSkyWalking
: localhost:18080Mycat
: data port18066
, management port19066
, usemysql
to connect, user/password:mydemo/mydemo
In Mac OSX,
--protocol
must be specified:mysql -h localhost -P 18066 -umydemo -pmydemo --protocol=TCP
ShardingProxy
:13307
, usemysql
to connect, user/password:mydemo/mydemo
MySQL
:13306
, usemysql
to connect, user/password:root/123
The YAML and script files in k8s/ run the demo application with Mycat
and ZipKin
in Kubernetes
.
- Build Docker images for all 3-party components used in the demo application.
docker/build-basis.sh
- Deploy demo application in
Kubernetes
:k8s/deploy-k8s.sh
Entrypoints:
- Demo Application: localhost:30090/shop
Nacos
: localhost:30048/nacos, user/password:nacos/nacos
ZipKin
: localhost:30041/zipkinMycat
: data port is30066
, management port is30067
, usemysql
to connect, user/password:mydemo/mydemo
In Mac OSX,
--protocol
must be specified:mysql -h localhost -P 30066 -umydemo -pmydemo --protocol=TCP
MySQL
:30006
, usemysql
to connect, user/password:root/123
Dubbo in Kubernetes
Dubbo
services are deployed by Kubernetes
Deployment
, and not registered as Kubernetes
Service
. They use Dubbo's own service descovery, load balance, providers take POD IP
to register to Nacos
, consumers fetch providers from Nacos
and comunicate with all providers by POD IP
. Dubbo
services can be managed by Deployment
, the following example scripts show how to scale svc-user
to 3 PODs. New POD ready and existing POD terminated events can be discovered by Dubbo
.
# Scale user-service to 3 PODs
kubectl scale Deployment svc-user --replicas=3
# Open 3 terminals to watch user-service logs
# 1. Find user-service PODs
kubectl get pods | grep svc-user
# 2. Watch logs for each svc-user POD
kubectl logs svc-user-68ff844499-9zqf8 -c svc-user -f
kubectl logs svc-user-68ff844499-dgsnx -c svc-user -f
...
# 3. Open http://localhost:30090/shop, Click "Run a Full TestCase" button and watch which user-service instance is used.
The YAML and script files in istio/ is a simple example for running shop-web
with Istio
, YAML files are the same with those in k8s/ except web-shop-deployment.yaml
. It's tested in Docker Desktop
, with Istio
installed and default
namespace enabled for istio-injection
as following:
istioctl manifest apply --set profile=demo
kubectl label ns default istio-injection=enabled --overwrite
Run demo application:
docker/build-basis.sh
istio/deploy-istio.sh
Bind myshop.com
to local machine IP in hosts
file, and use http://myshop.com/hello/YourName to visit.
shop-web
is deployed with two versions: 2 PODs for v1
and 1 POD for v2
, default route to v1
, use URL query param version=v2
to route to v2
. This is achieved by VirtualService
and DestinationRule
in istio/deployment/web-shop-deployment.yaml.
Dubbo
services using dubbo/thrift/rmi
protocols cann't work with Istio
, if use http/rest/webservice
protocols, Spring Boot
is a better choice rather than Dubbo
.