DAFramework采用docker作为开发环境,采用docker-compose作为服务编排工具,采用gradle作为java编译工具,所以在使用此框架之前必须先安装docker环境和gradle编译工具,请参照
-《01-windows7 下面安装docker环境》:http://www.jianshu.com/p/6853394555b1
-《02-windows下面安装gradle》:http://www.jianshu.com/p/5af53df61cc7
clone github源码
- DAFramework项目地址:https://github.com/DataAgg/DAFramework
- 使用git clone命令克隆代码到本地目录
F:\danew>git clone https://github.com/DataAgg/DAFramework.git
Cloning into 'DAFramework'...
remote: Counting objects: 738, done.
remote: Compressing objects: 100% (384/384), done.
emote: Total 738 (delta 211), reused 738 (delta 211), pack-reused 0
Receiving objects: 100% (738/738), 1.26 MiB | 7.00 KiB/s, done.
Resolving deltas: 100% (211/211), done.
Checking connectivity... done.
F:\danew>
-
mysql配置
修改account服务的mysql配置(F:\danew\DAFramework\account\src\main\resources\application.yml文件)
datasource: driver-class-name: com.mysql.jdbc.Driver url: "jdbc:mysql://192.168.31.226:3307/dataagg?useUnicode = true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull" username: dataagg password: EdIyNje2GsRyzPDXQfp1
修改security服务mysql配置(F:\danew\DAFramework\security\src\main\resources\application.yml文件)
datasource: driver-class-name: com.mysql.jdbc.Driver url: "jdbc:mysql://192.168.31.226:3307/dataagg?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull" username: dataagg password: EdIyNje2GsRyzPDXQfp1
编译DAFramework代码
进入项目目录如:F:\danew\DAFramework执行gradle bootrepackage命令编译项目
F:\danew\DAFramework>gradle bootrepackage
Starting a Gradle Daemon (subsequent builds will be faster)
:account:compileJava
:account:processResources
:account:classes
:account:findMainClass
:account:jar
:account:bootRepackage
:apigateway:compileJava
:apigateway:processResources
:apigateway:classes
:apigateway:findMainClass
:apigateway:jar
:apigateway:bootRepackage
:security:compileJava
:security:processResources
:security:classes
:security:findMainClass
:security:jar
:security:bootRepackage
:service-center:compileJava
:service-center:processResources
:service-center:classes
:service-center:findMainClass
:service-center:jar
:service-center:bootRepackage
BUILD SUCCESSFUL
Total time: 1 mins 28.42 secs
F:\danew\DAFramework>
编译成功之后,在每个服务下面会生成一个build文件夹,在libs目录下面会生成一个app.jar的可执行文件
docker-compose文件介绍
- docker-compose.base.yml文件定义了一个rabbitmq消息队列服务:
version: '2' #采用docker-compose v2版本语法定义docker-compose文件
services:
rabbitmq: #定义rabbitmq消息队列服务
image: rabbitmq:3-management #消息队列容器启动时使用的镜像
restart: always #当docker重启之后,docker容器自动重启
ports:
- 15672:15672 #映射宿主机端口
logging:
options:
max-size: "10m"
max-file: "10"
- docker-compose.yml文件定义了一个 service-center、 service、 apigateway和account四个微服务:
- service-center
service-center: #服务名
environment:
CONFIG_SERVICE_PASSWORD: YFzCAfocMInyJ5YaO805 #定义环境变量
image: java:8-jre #依赖java:8-jre镜像
volumes:
- ../service-center/build/libs/:/app/ #挂载jar包到容器里面的/app/目录下面
ports: ['8761:8761'] #定义主机端口号映射端口
expose: [8761] #暴露主机端口供其他微服务使用
# depends_on: [config]
# links: [config]
command: ["java", "-Djava.security.egd=file:/dev/./urandom", "-Xmx200m", "-jar", "/app/app.jar"] #容器启动时执行的java命令
- service
security:
environment:
CONFIG_SERVICE_PASSWORD: YFzCAfocMInyJ5YaO805
ACCOUNT_SERVICE_PASSWORD: YFzCAfocMInyJ5YaO805
image: java:8-jre
volumes:
- ../security/build/libs/:/app/
ports: ['5000:5000']
expose: [5000]
# depends_on: [service-center]
# links: [config, service-center]
command: ["java", "-Djava.security.egd=file:/dev/./urandom", "-Xmx200m", "-jar", "/app/app.jar"]
- apigateway
security:
environment:
CONFIG_SERVICE_PASSWORD: YFzCAfocMInyJ5YaO805
ACCOUNT_SERVICE_PASSWORD: YFzCAfocMInyJ5YaO805
image: java:8-jre
volumes:
- ../security/build/libs/:/app/
ports: ['5000:5000']
expose: [5000]
# depends_on: [service-center]
# links: [config, service-center]
command: ["java", "-Djava.security.egd=file:/dev/./urandom", "-Xmx200m", "-jar", "/app/app.jar"]
- account
account:
environment:
CONFIG_SERVICE_PASSWORD: YFzCAfocMInyJ5YaO805
ACCOUNT_SERVICE_PASSWORD: YFzCAfocMInyJ5YaO805
image: java:8-jre
volumes:
- ../account/build/libs/:/app/
ports: ['6000:6000']
expose: [6000]
depends_on: [apigateway] #依赖apigateway服务
# links: [config, service-center, apigateway]
command: ["java", "-Djava.security.egd=file:/dev/./urandom", "-Xmx200m", "-jar", "/app/app.jar"]
mysql数据库配置
本地安装mysql数据库,并执行F:\danew\DAFramework\codegen\sql目录下面的create.sql文件
CREATE TABLE `sys_users` (
`id` BIGINT(32) NOT NULL AUTO_INCREMENT
COMMENT '主键id',
`user_name` VARCHAR(64) NULL DEFAULT NULL
COMMENT '用户名',
`password` VARCHAR(64) NULL DEFAULT NULL
COMMENT '密码',
PRIMARY KEY (`id`)
)
ENGINE = InnoDB
DEFAULT CHARSET = utf8
COLLATE = utf8_bin
COMMENT = '用户表';
CREATE TABLE `da_account` (
`id` BIGINT(32) NOT NULL AUTO_INCREMENT
COMMENT '主键id',
`user_id` BIGINT(32) NOT NULL
COMMENT 'user id',
`full_name` VARCHAR(64) NULL DEFAULT NULL
COMMENT '全名',
`mobile` VARCHAR(64) NULL DEFAULT NULL
COMMENT '手机号码',
`address` VARCHAR(64) NULL DEFAULT NULL
COMMENT '地址',
`comment` VARCHAR(64) NULL DEFAULT NULL
COMMENT '备注',
PRIMARY KEY (`id`)
)
ENGINE = InnoDB
DEFAULT CHARSET = utf8
COLLATE = utf8_bin
COMMENT = '用户信息表';
docer-compose启动微服务项目
- 启动windows下面的docker,cmd进入项目目录执行命令:
F:\danew\DAFramework>docker-machine ls #查看本地docker虚拟机,本次使用的是dataagg虚拟机
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
dataagg - virtualbox Saved Unknown
default - virtualbox Saved Unknown
F:\danew\DAFramework>docker-machine start dataagg #启动dataagg虚拟机
Starting "dataagg"...
(dataagg) Waiting for an IP...
Machine "dataagg" was started.
Waiting for SSH to be available...
Detecting the provisioner...
Started machines may have new IP addresses. You may need to re-run the `docker-machine env` command.
#下面命令是设置环境变量,目的是让cmd命令行和docker虚拟机通信,能方便的在cmd命令行执行docker命令,docker-compose命令
F:\danew\DAFramework>docker-machine env dataagg
SET DOCKER_TLS_VERIFY=1
SET DOCKER_HOST=tcp://192.168.99.100:2376
SET DOCKER_CERT_PATH=C:\Users\Administrator\.docker\machine\machines\dataagg
SET DOCKER_MACHINE_NAME=dataagg
SET COMPOSE_CONVERT_WINDOWS_PATHS=true
REM Run this command to configure your shell:
REM @FOR /f "tokens=*" %i IN ('docker-machine env dataagg') DO @%i
F:\danew\DAFramework>SET DOCKER_TLS_VERIFY=1
F:\danew\DAFramework>SET DOCKER_HOST=tcp://192.168.99.100:2376
F:\danew\DAFramework>SET DOCKER_CERT_PATH=C:\Users\Administrator\.docker\machine\machines\dataagg
F:\danew\DAFramework>SET DOCKER_MACHINE_NAME=dataagg
F:\danew\DAFramework>SET COMPOSE_CONVERT_WINDOWS_PATHS=true
F:\danew\DAFramework>REM Run this command to configure your shell:
F:\danew\DAFramework>@FOR /f "tokens=*" %i IN ('docker-machine env dataagg') DO @%i
F:\danew\DAFramework>
- 启动docker-compose 基础服务(消息队列)
F:\danew\DAFramework>cd codegen
F:\danew\DAFramework\codegen>docker-compose -f docker-compose.base.yml up -d
Creating network "codegen_default" with the default driver
Pulling rabbitmq (rabbitmq:3-management)...
ERROR: Get https://registry-1.docker.io/v2/: net/http: TLS handshake timeout
F:\danew\DAFramework\codegen>
上图所示,由于rabbitmq定义了容器启动的镜像为:rabbitmq:3-management,默认使用docker官网的镜像源,速度比较慢。请参照《常见问题》->修改docker镜像仓库地址为阿里云镜像库.
修改完docker镜像地址重启docker之后,重新执行启动消息队列服务命令
F:\danew\DAFramework\codegen>docker-compose -f docker-compose.base.yml up -d # -f 指定docker-compose文件位置,up启动,-d参数后台启动服务
Pulling rabbitmq (rabbitmq:3-management)...
3-management: Pulling from library/rabbitmq
5040bd298390: Pull complete
7e7b82d81984: Pull complete
30ab6dd6c03b: Pull complete
f6050e233007: Pull complete
eb5a4ffc5983: Pull complete
d92026084ce6: Pull complete
8fda19207973: Pull complete
7b8b3646e05e: Pull complete
ed2dd1208708: Pull complete
a2c415542c01: Pull complete
7a03442472f0: Pull complete
079759849dae: Pull complete
ca9467c0f6f3: Pull complete
e17a6fe72ed5: Pull complete
06f288cff519: Pull complete #由于本地没有rabbitmq:3-management镜像,pull镜像过程
Digest: sha256:84f30ff7e45816519b19922deaa552298556dafd13dfec6ebf6b6af2bbc78d79
Status: Downloaded newer image for rabbitmq:3-management
Creating codegen_rabbitmq_1 #启动容器名为codegen_rabbitmq_1的消息队列服务
查看docker镜像和容器
F:\danew\DAFramework\codegen>docker images
time="2017-02-21T23:01:42+08:00" level=info msg="Unable to use system certificate pool: crypto/x509: system root pool is not available on Windows"
REPOSITORY TAG IMAGE ID CREATED SIZE
rabbitmq 3-management cda8025c010b 4 weeks ago 179 MB
F:\danew\DAFramework\codegen>docker ps
time="2017-02-21T23:01:46+08:00" level=info msg="Unable to use system certificate pool: crypto/x509: system root pool is not available on Windows"
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5ff07acd442c rabbitmq:3-management "docker-entrypoint..." 3 minutes ago Up 3 minutes 4369/tcp, 5671-5672/tcp, 15671/tcp, 25672/tcp, 0.0.0.0:15672->15672/tcp codegen_rabbitmq_1
F:\danew\DAFramework\codegen>
- 启动service-center、service、account和apigatway四个微服务
F:\danew\DAFramework\codegen>docker-compose -f docker-compose.yml up -d #采用第二个docker-compose文件部署四个微服务
WARNING: Found orphan containers (codegen_rabbitmq_1) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up.
Pulling security (java:8-jre)...
8-jre: Pulling from library/java
5040bd298390: Already exists
fce5728aad85: Pull complete
c42794440453: Pull complete
0c0da797ba48: Pull complete
7c9b17433752: Pull complete
114e02586e63: Pull complete
e4c663802e9a: Pull complete
Digest: sha256:b91008e234402fc87e7889d6af1f36b6ece844c05989236d83d1f658a6f329b0
Status: Downloaded newer image for java:8-jre #pull 服务使用的镜像java:8-jre
Creating codegen_security_1 #创建security服务
Creating codegen_service-center_1 #创建security-center服务
Creating codegen_apigateway_1 #创建apigatway服务
Creating codegen_account_1 #创建account服务
F:\danew\DAFramework\codegen>docker-compose -f docker-compose.yml ps #查看服务状态
Name Command State Ports
--------------------------------------------------------------------------
codegen_account_1 java -Djava.security.egd=f ... Exit 1
codegen_apigateway_1 java -Djava.security.egd=f ... Exit 1
codegen_security_1 java -Djava.security.egd=f ... Exit 1
codegen_service-center_1 java -Djava.security.egd=f ... Exit 1
F:\danew\DAFramework\codegen>docker images #查看docker镜像
time="2017-02-21T23:06:43+08:00" level=info msg="Unable to use system certificate pool: crypto/x509: system root pool is not available on Windows"
REPOSITORY TAG IMAGE ID CREATED SIZE
rabbitmq 3-management cda8025c010b 4 weeks ago 179 MB
java 8-jre e44d62cf8862 5 weeks ago 311 MB
F:\danew\DAFramework\codegen>
通过上图的命令,我们发现,服务的状态都为Exit,证明容器没有很好的运行,使用docker-compose 查看服务logs
F:\danew\DAFramework\codegen>docker-compose -f docker-compose.yml logs
Attaching to codegen_account_1, codegen_apigateway_1, codegen_service-center_1, codegen_security_1
account_1 | Error: Unable to access jarfile /app/app.jar
service-center_1 | Error: Unable to access jarfile /app/app.jar
security_1 | Error: Unable to access jarfile /app/app.jar
apigateway_1 | Error: Unable to access jarfile /app/app.jar
F:\danew\DAFramework\codegen>
日志告诉我们,服务启动时容器找不到/app/app.jar,证明app.jar包文件没有挂载到容器里面。《常见问题汇总》->windows目录共享到docker虚拟机
- 修改docker-compse.yml文件的volumes
service-center:
environment:
CONFIG_SERVICE_PASSWORD: YFzCAfocMInyJ5YaO805
image: java:8-jre
volumes:
- /F/danew/DAFramework/service-center/build/libs/:/app/
ports: ['8761:8761']
expose: [8761]
# depends_on: [config]
# links: [config]
command: ["java", "-Djava.security.egd=file:/dev/./urandom", "-Xmx200m", "-jar", "/app/app.jar"]
security:
environment:
CONFIG_SERVICE_PASSWORD: YFzCAfocMInyJ5YaO805
ACCOUNT_SERVICE_PASSWORD: YFzCAfocMInyJ5YaO805
image: java:8-jre
volumes:
- /F/danew/DAFramework/security/build/libs/:/app/
ports: ['5000:5000']
expose: [5000]
# depends_on: [service-center]
# links: [config, service-center]
command: ["java", "-Djava.security.egd=file:/dev/./urandom", "-Xmx200m", "-jar", "/app/app.jar"]
apigateway:
environment:
CONFIG_SERVICE_PASSWORD: YFzCAfocMInyJ5YaO805
image: java:8-jre
volumes:
- /F/danew/DAFramework/apigateway/build/libs/:/app/
ports: ['4000:4000']
expose: [80]
depends_on: [security]
# links: [config, service-center]
command: ["java", "-Djava.security.egd=file:/dev/./urandom", "-Xmx200m", "-jar", "/app/app.jar"]
account:
environment:
CONFIG_SERVICE_PASSWORD: YFzCAfocMInyJ5YaO805
ACCOUNT_SERVICE_PASSWORD: YFzCAfocMInyJ5YaO805
image: java:8-jre
volumes:
- /F/danew/DAFramework/account/build/libs/:/app/
ports: ['6000:6000']
expose: [6000]
depends_on: [apigateway]
# links: [config, service-center, apigateway]
command: ["java", "-Djava.security.egd=file:/dev/./urandom", "-Xmx200m", "-jar", "/app/app.jar"]
- 重新执行启动命令
F:\danew\DAFramework\codegen>docker-compose -f docker-compose.yml down #移除服务
WARNING: Found orphan containers (codegen_rabbitmq_1) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up.
Removing codegen_account_1 ... done
Removing codegen_apigateway_1 ... done
Removing codegen_service-center_1 ... done
Removing codegen_security_1 ... done
Removing network codegen_default
ERROR: network codegen_default has active endpoints
F:\danew\DAFramework\codegen>docker-compose -f docker-compose.yml up -d #重新执行启动
WARNING: Found orphan containers (codegen_rabbitmq_1) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up.
Creating codegen_security_1
Creating codegen_service-center_1
Creating codegen_apigateway_1
Creating codegen_account_1
F:\danew\DAFramework\codegen>docker-compose -f docker-compose.yml ps #服务状态都为up,成功启动
Name Command State Ports
--------------------------------------------------------------------------------------------------
codegen_account_1 java -Djava.security.egd=f ... Up 0.0.0.0:6000->6000/tcp
codegen_apigateway_1 java -Djava.security.egd=f ... Up 0.0.0.0:4000->4000/tcp, 80/tcp
codegen_security_1 java -Djava.security.egd=f ... Up 0.0.0.0:5000->5000/tcp
codegen_service-center_1 java -Djava.security.egd=f ... Up 0.0.0.0:8761->8761/tcp
F:\danew\DAFramework\codegen>
验证服务
- 消息队列服务:http://192.168.99.100:15672 用户名和密码:guest guest
- apigatway网关服务 http://192.168.99.100:4000
- security-center服务:http://192.168.99.100:8761
常见问题汇总
修改docker镜像仓库为阿里云仓库
docker-machine ssh dataagg "echo 'EXTRA_ARGS=\"--registry-mirror=https://pee6w651.mirror.aliyuncs.com\"' | sudo tee -a /var/lib/boot2docker/profile"
#修改docker 镜像地址
docker-machine restart dataagg #重启dataagg docker虚拟机
F:\danew\DAFramework\codegen>docker-machine ssh dataagg "echo 'EXTRA_ARGS=\"--registry-mirror=https://pee6w651.mirror.aliyuncs.com\"' | sudo tee -a /var/lib/boot2docker/profile"
EXTRA_ARGS="--registry-mirror=https://pee6w651.mirror.aliyuncs.com"
F:\danew\DAFramework\codegen>docker-machine restart dataagg
Restarting "dataagg"...
(dataagg) Check network to re-create if needed...
(dataagg) Waiting for an IP...
Waiting for SSH to be available...
Detecting the provisioner...
Restarted machines may have new IP addresses. You may need to re-run the `docker-machine env` command.
F:\danew\DAFramework\codegen>
参考地址:http://blog.csdn.net/bwlab/article/details/50542261
windows目录共享到docker虚拟机
为了方便调试,本次把F盘直接共享给docker虚拟机
- 打开oracle vm 虚拟机,找到dataagg虚拟机,设置->共享文件夹
- 重启虚拟机验证F盘是否在docker虚拟机内
docker@default:/$ cd /
docker@default:/$ ls -la
total 20
drwxr-xr-x 19 tc staff 460 Feb 21 01:35 ./
drwxr-xr-x 19 tc staff 460 Feb 21 01:35 ../
drwxrwxrwx 1 docker staff 16384 Feb 21 08:14 F/ #已经把windows的目录挂载docker虚拟机里面了
drwxr-xr-x 2 root root 1500 Feb 21 00:31 bin/
drwxr-xr-x 3 root root 60 Feb 21 00:31 c/
drwxrwxr-x 14 root staff 4420 Feb 21 00:31 dev/
drwxr-xr-x 11 root root 1080 Feb 21 00:31 etc/
drwxr-xr-x 3 root root 60 Feb 21 01:35 f/
drwxrwxr-x 4 root staff 80 Feb 21 00:31 home/
-rwxr-xr-x 1 root root 496 Dec 15 17:56 init
drwxr-xr-x 5 root root 860 Feb 21 00:31 lib/
lrwxrwxrwx 1 root root 3 Feb 21 00:31 lib64 -> lib/
lrwxrwxrwx 1 root root 11 Feb 21 00:31 linuxrc -> bin/busybox
drwxr-xr-x 4 root root 80 Feb 21 00:31 mnt/
drwxrwsr-x 2 root staff 180 Feb 21 00:31 opt/
dr-xr-xr-x 176 root root 0 Feb 21 00:30 proc/
drwxrwxr-x 2 root staff 80 Feb 21 00:31 root/
drwxrwxr-x 6 root staff 120 Feb 21 00:33 run/
drwxr-xr-x 2 root root 1400 Feb 21 00:31 sbin/
dr-xr-xr-x 13 root root 0 Feb 21 00:31 sys/
lrwxrwxrwx 1 root root 13 Feb 21 00:31 tmp -> /mnt/sda1/tmp/
drwxr-xr-x 7 root root 140 Sep 10 14:26 usr/
drwxrwxr-x 8 root staff 180 Feb 21 00:31 var/
docker@default:/$