博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Dubbox小案例
阅读量:5747 次
发布时间:2019-06-18

本文共 22526 字,大约阅读时间需要 75 分钟。

一。安装zookeeper

  1. 进入阿里云的容器服务,搜索zookeeper:https://cr.console.aliyun.com/cn-hangzhou/images

  2. 复制网址

  3. 下载镜像

[root@ay120tum0is24xe ~]# docker image listREPOSITORY          TAG                 IMAGE ID            CREATED             SIZEmysql               5.6                 27e29668a08a        6 weeks ago         256MB[root@ay120tum0is24xe ~]# docker image pull registry.cn-hangzhou.aliyuncs.com/zuowenbo/zookeeperUsing default tag: latestlatest: Pulling from zuowenbo/zookeeper8e3ba11ec2a2: Pull complete 311ad0da4533: Pull complete 391a6a6b3651: Pull complete 1e5180f6c6f9: Pull complete Digest: sha256:d7531d8bec4b2f2a298fceaaaaeac8f5c7d223d4a9ea2136cf0af23110c83ae8Status: Downloaded newer image for registry.cn-hangzhou.aliyuncs.com/zuowenbo/zookeeper:latest[root@ay120tum0is24xe ~]# docker image list -aREPOSITORY                                             TAG                 IMAGE ID            CREATED             SIZEmysql                                                  5.6                 27e29668a08a        6 weeks ago         256MBregistry.cn-hangzhou.aliyuncs.com/zuowenbo/zookeeper   latest              64e049ee9478        6 months ago        148MB[root@ay120tum0is24xe ~]#

  4. 修改镜像名称

[root@ay120tum0is24xe ~]# docker image tag registry.cn-hangzhou.aliyuncs.com/zuowenbo/zookeeper:latest zookeeper:latest[root@ay120tum0is24xe ~]# docker image list -aREPOSITORY                                             TAG                 IMAGE ID            CREATED             SIZEmysql                                                  5.6                 27e29668a08a        6 weeks ago         256MBregistry.cn-hangzhou.aliyuncs.com/zuowenbo/zookeeper   latest              64e049ee9478        6 months ago        148MBzookeeper                                              latest              64e049ee9478        6 months ago        148MB[root@ay120tum0is24xe ~]# docker image rm registry.cn-hangzhou.aliyuncs.com/zuowenbo/zookeeper:latestUntagged: registry.cn-hangzhou.aliyuncs.com/zuowenbo/zookeeper:latestUntagged: registry.cn-hangzhou.aliyuncs.com/zuowenbo/zookeeper@sha256:d7531d8bec4b2f2a298fceaaaaeac8f5c7d223d4a9ea2136cf0af23110c83ae8[root@ay120tum0is24xe ~]# docker image list -aREPOSITORY          TAG                 IMAGE ID            CREATED             SIZEmysql               5.6                 27e29668a08a        6 weeks ago         256MBzookeeper           latest              64e049ee9478        6 months ago        148MB[root@ay120tum0is24xe ~]#

  5. 启动容器

[root@ay120tum0is24xe ~]# docker container run --name zookeeper001 -d -p 2181:2181 zookeeper:latest    // 后面的为容器里的端口838d94a2d1c7120639c4d8219d26e718330d11a93e3e3989027a4ce61c7f32d4[root@ay120tum0is24xe ~]# docker container run --name zookeeper002 -d -p 2182:2181 zookeeper:latestdf4357e92486490918c5a63ab7adec038eb5beddb8d1d42a04a8c9041df9fadf[root@ay120tum0is24xe ~]# docker container listCONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                             PORTS                                        NAMESdf4357e92486        zookeeper:latest    "/opt/zookeeper/bin/…"   19 seconds ago      Up 18 seconds (health: starting)   2888/tcp, 3888/tcp, 0.0.0.0:2182->2181/tcp   zookeeper002838d94a2d1c7        zookeeper:latest    "/opt/zookeeper/bin/…"   38 seconds ago      Up 37 seconds (healthy)            2888/tcp, 0.0.0.0:2181->2181/tcp, 3888/tcp   zookeeper001[root@ay120tum0is24xe ~]#

--name:容器名称

-d:后台运行

-p:端口映射,冒号左边为主机端口,右边为容器端口  

  6. 进人容器zookeeper002

[root@ay120tum0is24xe ~]# docker container exec -it zookeeper002 /bin/sh/opt/zookeeper # lsLICENSE.txt                build.xml                  ivy.xml                    zookeeper-3.4.13.jarNOTICE.txt                 conf                       ivysettings.xml            zookeeper-3.4.13.jar.ascREADME.md                  contrib                    lib                        zookeeper-3.4.13.jar.md5README_packaging.txt       dist-maven                 recipes                    zookeeper-3.4.13.jar.sha1bin                        docs                       src/opt/zookeeper #

  7.通过主机 登录容器zookeeper001

/opt/zookeeper # ./bin/zkCli.sh -server 10.20.0.128:2181[zk: 10.20.0.128:2181(CONNECTED) 0]

  8. quit退出zookeeper001,exit 退出容器zookeeper002

 二。安装dubbo-admin

  1. 下载镜像

[root@ay120tum0is24xe ~]# docker image pull registry.cn-hangzhou.aliyuncs.com/zhaozhiwu/dubbo-admin[root@ay120tum0is24xe ~]# docker image listREPOSITORY                                                TAG                 IMAGE ID            CREATED             SIZEregistry.cn-hangzhou.aliyuncs.com/zhaozhiwu/dubbo-admin   latest              4b43bc0f997e        3 weeks ago         360MBmysql                                                     5.6                 27e29668a08a        6 weeks ago         256MBzookeeper                                                 latest              64e049ee9478        6 months ago        148MB[root@ay120tum0is24xe ~]# docker image tag registry.cn-hangzhou.aliyuncs.com/zhaozhiwu/dubbo-admin dubbo-admin:latest[root@ay120tum0is24xe ~]# docker image rm registry.cn-hangzhou.aliyuncs.com/zhaozhiwu/dubbo-adminUntagged: registry.cn-hangzhou.aliyuncs.com/zhaozhiwu/dubbo-admin:latestUntagged: registry.cn-hangzhou.aliyuncs.com/zhaozhiwu/dubbo-admin@sha256:f23d8276e94ccfc5a6e5565ba6744c07f432869667fd08420d58148baf863910[root@ay120tum0is24xe ~]# docker image listREPOSITORY          TAG                 IMAGE ID            CREATED             SIZEdubbo-admin         latest              4b43bc0f997e        3 weeks ago         360MBmysql               5.6                 27e29668a08a        6 weeks ago         256MBzookeeper           latest              64e049ee9478        6 months ago        148MB[root@ay120tum0is24xe ~]#

  2. 运行容器

[root@192 ~]# docker run -d --add-host zk:10.20.0.129 -p 8090:8080 --name dubbo dubbo-admin2e164333536a5b3c15260270e3939e9064cc11087824aaeac8bfbe379fea882c[root@192 ~]#

  3. 浏览器登录:http://10.20.0.129:8090/ ,用户:root ,密码:root

三。新建项目

  1. 建立空项目

  2. 添加maven父工程,不用模板,工程名:parent

  3. 修改 pom.xml

4.0.0
com.zgh
parent
1.0-SNAPSHOT
pom
UTF-8
1.7
1.7
2.8.5
4.12
4.2.4.RELEASE
4.0.0
2.5
2.8.4
3.4.7
0.1
3.2.8
1.2.2
1.2.15
5.1.32
1.0.9
1.3.1
2.3.23
5.11.2
3.2.3.RELEASE
4.10.3
2012_u6
1.3.5
com.fasterxml.jackson.core
jackson-core
${jackson.version}
com.fasterxml.jackson.core
jackson-databind
${jackson.version}
com.fasterxml.jackson.core
jackson-annotations
${jackson.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-aspects
${spring.version}
org.springframework
spring-jms
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-test
${spring.version}
com.alibaba
dubbo
${dubbo.version}
org.apache.zookeeper
zookeeper
${zookeeper.version}
com.github.sgroschupf
zkclient
${zkclient.version}
junit
junit
4.9
com.alibaba
fastjson
1.2.28
javassist
javassist
3.11.0.GA
commons-codec
commons-codec
1.10
javax.servlet
servlet-api
2.5
provided
com.github.pagehelper
pagehelper
${pagehelper.version}
org.mybatis
mybatis
${mybatis.version}
org.mybatis
mybatis-spring
${mybatis.spring.version}
com.github.miemiedev
mybatis-paginator
${mybatis.paginator.version}
org.mybatis.generator
mybatis-generator-core
${mybatis.generator}
mysql
mysql-connector-java
${mysql.version}
com.alibaba
druid
${druid.version}
org.csource.fastdfs
fastdfs
1.2
commons-fileupload
commons-fileupload
${commons-fileupload.version}
redis.clients
jedis
2.8.1
org.springframework.data
spring-data-redis
1.7.2.RELEASE
org.freemarker
freemarker
${freemarker.version}
org.apache.activemq
activemq-all
${activemq.version}
org.springframework.security
spring-security-web
4.1.0.RELEASE
org.springframework.security
spring-security-config
4.1.0.RELEASE
com.github.penggle
kaptcha
2.3.2
javax.servlet
javax.servlet-api
org.springframework.security
spring-security-cas
4.1.0.RELEASE
org.jasig.cas.client
cas-client-core
3.3.3
org.slf4j
log4j-over-slf4j
org.apache.solr
solr-solrj
${solrj.version}
com.janeluo
ikanalyzer
${ik.version}
org.apache.httpcomponents
httpcore
4.4.4
org.apache.httpcomponents
httpclient
4.5.3
dom4j
dom4j
1.6.1
xml-apis
xml-apis
1.4.01
${project.artifactId}
maven-clean-plugin
3.1.0
maven-resources-plugin
3.0.2
maven-compiler-plugin
3.8.0
maven-surefire-plugin
2.22.1
maven-war-plugin
3.2.2
maven-install-plugin
2.5.2
maven-deploy-plugin
2.8.2
src/main/java
**/*.properties
**/*.xml
src/main/resources
**/*.properties
**/*.xml

  4. 先点击library,然后点击File-->new-->module,建立maven web工程:dubboxDemo-Service (服务提供者),并继承父工程

  5. pom.xml

parent
com.zgh
1.0-SNAPSHOT
../parent/pom.xml
4.0.0
dubboxDemo-Service
war
dubboxDemo-Service Maven Webapp
http://www.example.com
${artifactId}
org.apache.tomcat.maven
tomcat7-maven-plugin
2.2
9000
/
UTF-8
tomcat7

  6. 同理建立工程:dubboxDemo-web (服务消费者)

parent
com.zgh
1.0-SNAPSHOT
../parent/pom.xml
4.0.0
dubboxDemo-web
war
dubboxDemo-web Maven Webapp
http://www.example.com
${artifactId}
org.apache.tomcat.maven
tomcat7-maven-plugin
2.2
9001
/
UTF-8
tomcat7

 四。配置dubboxDemo-Service

  1. dubboxDemo-Service 工程新建接口: UserService

package com.dubboxDemo.service;public interface UserService {    public String getName();}

  2. 实现这个接口

package com.dubboxDemo.service.impl;import com.alibaba.dubbo.config.annotation.Service;import com.dubboxDemo.service.UserService;@Servicepublic class UserServiceImpl implements UserService {    @Override    public String getName() {        return "我的 dubboDemo";    }}

  3. 配置applicationContext-service.xml

  4. 配置web.xml

Archetype Created Web Application
contextConfigLocation
classpath:applicationContext*.xml
org.springframework.web.context.ContextLoaderListener

五。配置dubboxDemo-web

  1. 把工程dubboxDemo-Service里的接口 UserService 复制下来,包路径要两者一样

  2. 实现控制器

package com.dubboxDemo.controller;import com.alibaba.dubbo.config.annotation.Reference;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import com.dubboxDemo.service.UserService;@Controller@RequestMapping("/user")public class UserController {    @Reference    private UserService userService;    @RequestMapping("/showName")    @ResponseBody    public String showName(){        return userService.getName();    }}

  3. 配置applicationContext-web.xml

  4. 配置web.xml

Archetype Created Web Application
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
forceEncoding
true
CharacterEncodingFilter
/*
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:applicationContext-web.xml
springmvc
*.do

六。测试

  1. 运行dubboxDemo-Service(服务提供者)

  2. dubbo-admin查看,服务提供者的信息(ip地址和端口(默认20880)等)存储到了zookeepe

 

  3. 同理启动dubboxDemo-web(服务消费者),然后浏览器输入:http://localhost:9001/user/showName.do ,服务消费者会去注册中心zookeepe拿到服务提供者的ip地址和端口(默认20880),然后到该地址和端口调用

  4. dubbo-admin查看,已经被消费了

 

转载于:https://www.cnblogs.com/GH-123/p/10385426.html

你可能感兴趣的文章
java版两图对比相似度
查看>>
我的友情链接
查看>>
SharePoint 2013实例1—构建三层服务器场5—配置DB层3—模拟存储
查看>>
iOS培训教程——NSArray和NSMutableArray章节
查看>>
apache tomcat ajp mod jk
查看>>
我的友情链接
查看>>
Ubuntu 14.0.4 如何设置 VirtualBox 识别 USB 设备
查看>>
zabbix API基本使用方法介绍
查看>>
mysql 用户登录密码和远程登录权限问题
查看>>
mongodb配置文件的英文翻译
查看>>
静态路由和默认路由的配置实战及其区别--很详细哦
查看>>
织梦模板网站后台数据库设定详细解释说明
查看>>
QTP中weblist以及webcheckbox自动选择的方法推荐
查看>>
第九节 字符串的比较
查看>>
Python爬虫爬数据写入到文件
查看>>
获取所有根路径(磁盘分区)的空间详情
查看>>
nginx 配置thinkphp,隐藏index.php
查看>>
jforum 分页功能
查看>>
我的友情链接
查看>>
left join , right join ,inner join 的区别
查看>>