Dubbo应用服务的配置文件部分
^.^ :接一下昨天的Dubbo服务的应用说下如何将昨天的配置工程给跑起来
1.首先是dubbo-api的依赖包,这边我用的是这些依赖
<dependencies> <!--mybatis-plus的依赖--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> </dependency> <!--swagger ui的依赖包--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> </dependency> <!--lombok的依赖包--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies>
2.Dubbo-provider工程的的依赖
<dependencies>
<!--引入dubbo-api工程的依赖-->
<dependency>
<groupId>cn.com.fuzhou</groupId>
<artifactId>dubbo-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--dubbo的依赖-->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
</dependency>
<!--zookeeper的依赖-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</dependency>
<!--postgres的依赖(一种数据库)-->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<!--mybatis-plus的依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
</dependencies>
3.dubbo-consumer工程的依赖包
<dependencies> <dependency> <groupId>cn.com.fuzhou</groupId> <artifactId>dubbo-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> </dependency> </dependencies>
4.dubbo-api的yml配置文件部分
server:
port: 20138
spring:
application:
name: dubbo-api
#MyBatis-plus的配置部分
mybatis-plus:
global-config:
db-config:
table-prefix: ct_
id-type: assign_id
configuration:
map-underscore-to-camel-case: true
mapper-locations: classpath*:mapper/*Mapper.xml
type-aliases-package: cn.com.dubbo.api.pojo
5.dubbo-provider的yml文件部分
server: port: 20198 #指定Dubbo的配置 dubbo: application: name: dubbo-provider registry: protocol: zookeeper address: localhost:2181 protocol: name: dubbo port: 20880 scan: base-packages: cn.com.dubbo.provider.impl spring: datasource: url: jdbc:postgresql://localhost:15432/postgres?useUnicode=true&characterEncoding=utf-8 username: postgres password: xxxxx(自己的密码) driver-class-name: org.postgresql.Driver
6.dubbo-consumer
server: port: 20199 dubbo: application: name: dubbo-consumer registry: protocol: zookeeper address: localhost:2181 protocol: name: dubbo port: 20880 spring: datasource: url: jdbc:postgresql://localhost:15432/postgres?useUnicode=true&characterEncoding=utf-8 username: postgres password: 自己的密码 driver-class-name: org.postgresql.Driver
7.然后是主pom文件
<modules> <!--dubbo的接口工程--> <module>dubbo-api</module> <!--dubbo的生产者工程--> <module>dubbo-provider</module> <!--dubbo的消费者工程--> <module>dubbo-consumer</module> </modules> <!--定义父类版本--> <properties> <!--定义JDK的版本部分--> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <swagger.version>2.8.0</swagger.version> <dubbo-spring-boot-starter.version>0.2.0</dubbo-spring-boot-starter.version> <curator-framework.version>2.12.0</curator-framework.version> <mybatis-plus.version>3.4.0</mybatis-plus.version> <lombok.version>1.18.6</lombok.version> <zookeeper.version>3.4.8</zookeeper.version> <postgresql.version>42.2.12</postgresql.version> </properties> <dependencyManagement> <dependencies> <!--Spring-boot的版本--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--swagger--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>${swagger.version}</version> </dependency> <!--swagger ui--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>${swagger.version}</version> </dependency> <!--添加阿里巴巴封装的Dubbo服务框架--> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>${dubbo-spring-boot-starter.version}</version> </dependency> <!--Dubbo的客户端--> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>${curator-framework.version}</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>${mybatis-plus.version}</version> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>${postgresql.version}</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>${zookeeper.version}</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> </exclusions> </dependency> </dependencies> </dependencyManagement>
Linux上安装zookeeper(Linux)
查看docker提供的zookeeper的镜像文件
docker search zookeeper
拉取zookeeper的镜像
docker pull zookeeper
查看docker 的镜像是否拉取成功
docker images
运行docker镜像
docker run --privileged=true -d --name zookeeper --publish 12181:2181 -d zookeeper:latest
查看镜像是否运行成功
docker ps (能看见zookpper的名字表示镜像启动成功)
ps:如果想进入到zookeeper的容器中
docker exec -it zk容器ID /bin/bash
ls:查看容器里面的目录
cd bin/ :进入到容器的bin目录中
./zkCli.sh :进入到zookeeper的客户端
ls / 查看全部的节点
quit :退出zk客户端
exit退出docker容器
这样zookeeper的安装就完成了
下边来说下如何能直观的看到dubbo的服务注册结果(安装dubbo-admin客户端)
这边提供一个dubbo-admin的下载地址网上也有很多这边是我自己用的:
链接:https://pan.baidu.com/s/1C_S-0uLt3FzBPT0Zvh5fxA
提取码:b62o
接下来说下dubbo-admin的客户端如何使用
ps:首选端口可以选择性修改,其次是zookeeper的服务地址要改成知己的端口地址,然后登录的用户密码可以选择修改root用户密码是root,guest用户密码是guest这边为了方便我统一修改为root
然后我们将该文件打一个包这边可以看下pom我们这边打的是jar包
运行命令
mvn clean package
能看多啊SUCCESS说明打包成功
ps:当然想吧java程序跑起来肯定是需要用到jdk的所以们可以选择在docker中安装一个JDK(docker安装JDK请自行百度谢谢)
想要将jar包变成可运行的docker容器咋办呢这边我们来编写一个Dockerfile
1.我们在linux中创建一个文件夹用来存放jar包和Dockerfile文件
cd /usr
mkdir jar_file
将jar包上传到jar_file文件中
cd jar_file/
vim Dockerfile
这边我们编写个Dockerfile文件试试#指定JDK的版本
FROM java:8 #创建人信息 MAINTAINER LingZsj #这里的 /tmp 目录就会在运行时自动挂载为匿名卷,任何向 /tmp 中写入的信息都不会记录进容器存储层。 VOLUME /tmp #添加文件中的dubbo-admin-0.0.1-SNAPSHOT.jar到docker容器中 ADD dubbo-admin-0.0.1-SNAPSHOT.jar dubbo-admin.jar #bash方式执行,使得ct-plat.jar可访问 #RUN新建立一层,在其上执行这些命令,执行结束后.commit这层修改,构成新镜像 RUN bash -c "touch /dubbo-admin.jar" #申明服务运行时容器提供服务端口,只做申明,并不会主动开启服务端口 EXPOSE 17001 #指定容器启动程序及参数 ENTRYPOINT ["java","-jar","dubbo-admin.ja"] ....好像写不下了感觉是不是我写的太啰嗦了...只能在这里追加了抱歉~QvQ~ 然后在jar_file文件夹目录中运行 docker build -t dubbo-admin . ps:后面的.别写掉了 然后docker images查看镜像时候构建成功 如成功了name我们就来启动容器了 docker run -d --name dubbo-admin -p 17001:17001 dubbo-admin 这边-p前面的端口是容器与外部映射的端口也就是你外部访问的端口:后面的端口是你application.properties中配置的程序的端口别写错了... 好了写完了写完了溜了溜了,若有错误欢迎指教谢谢^V^