Dubbox小案例

一。安装zookeeper

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

  2. 复制网址

  3. 下载镜像

[root@ay120tum0is24xe ~]# docker image list
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               5.6                 27e29668a08a        6 weeks ago         256MB
[root@ay120tum0is24xe ~]# docker image pull registry.cn-hangzhou.aliyuncs.com/zuowenbo/zookeeper
Using default tag: latest
latest: Pulling from zuowenbo/zookeeper
8e3ba11ec2a2: Pull complete 
311ad0da4533: Pull complete 
391a6a6b3651: Pull complete 
1e5180f6c6f9: Pull complete 
Digest: sha256:d7531d8bec4b2f2a298fceaaaaeac8f5c7d223d4a9ea2136cf0af23110c83ae8
Status: Downloaded newer image for registry.cn-hangzhou.aliyuncs.com/zuowenbo/zookeeper:latest
[root@ay120tum0is24xe ~]# docker image list -a
REPOSITORY                                             TAG                 IMAGE ID            CREATED             SIZE
mysql                                                  5.6                 27e29668a08a        6 weeks ago         256MB
registry.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 -a
REPOSITORY                                             TAG                 IMAGE ID            CREATED             SIZE
mysql                                                  5.6                 27e29668a08a        6 weeks ago         256MB
registry.cn-hangzhou.aliyuncs.com/zuowenbo/zookeeper   latest              64e049ee9478        6 months ago        148MB
zookeeper                                              latest              64e049ee9478        6 months ago        148MB
[root@ay120tum0is24xe ~]# docker image rm registry.cn-hangzhou.aliyuncs.com/zuowenbo/zookeeper:latest
Untagged: registry.cn-hangzhou.aliyuncs.com/zuowenbo/zookeeper:latest
Untagged: registry.cn-hangzhou.aliyuncs.com/zuowenbo/zookeeper@sha256:d7531d8bec4b2f2a298fceaaaaeac8f5c7d223d4a9ea2136cf0af23110c83ae8
[root@ay120tum0is24xe ~]# docker image list -a
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               5.6                 27e29668a08a        6 weeks ago         256MB
zookeeper           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:latest
df4357e92486490918c5a63ab7adec038eb5beddb8d1d42a04a8c9041df9fadf
[root@ay120tum0is24xe ~]# docker container list
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                             PORTS                                        NAMES
df4357e92486        zookeeper:latest    "/opt/zookeeper/bin/…"   19 seconds ago      Up 18 seconds (health: starting)   2888/tcp, 3888/tcp, 0.0.0.0:2182->2181/tcp   zookeeper002
838d94a2d1c7        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 # ls
LICENSE.txt                build.xml                  ivy.xml                    zookeeper-3.4.13.jar
NOTICE.txt                 conf                       ivysettings.xml            zookeeper-3.4.13.jar.asc
README.md                  contrib                    lib                        zookeeper-3.4.13.jar.md5
README_packaging.txt       dist-maven                 recipes                    zookeeper-3.4.13.jar.sha1
bin                        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 list
REPOSITORY                                                TAG                 IMAGE ID            CREATED             SIZE
registry.cn-hangzhou.aliyuncs.com/zhaozhiwu/dubbo-admin   latest              4b43bc0f997e        3 weeks ago         360MB
mysql                                                     5.6                 27e29668a08a        6 weeks ago         256MB
zookeeper                                                 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-admin
Untagged: registry.cn-hangzhou.aliyuncs.com/zhaozhiwu/dubbo-admin:latest
Untagged: registry.cn-hangzhou.aliyuncs.com/zhaozhiwu/dubbo-admin@sha256:f23d8276e94ccfc5a6e5565ba6744c07f432869667fd08420d58148baf863910
[root@ay120tum0is24xe ~]# docker image list
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
dubbo-admin         latest              4b43bc0f997e        3 weeks ago         360MB
mysql               5.6                 27e29668a08a        6 weeks ago         256MB
zookeeper           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-admin
2e164333536a5b3c15260270e3939e9064cc11087824aaeac8bfbe379fea882c
[root@192 ~]# 

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

三。新建项目

  1. 建立空项目

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

  3. 修改 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.zgh</groupId>
    <artifactId>parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <!-- 集中定义依赖版本号 -->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <jackson.version>2.8.5</jackson.version>
        <junit.version>4.12</junit.version>
        <spring.version>4.2.4.RELEASE</spring.version>
        <pagehelper.version>4.0.0</pagehelper.version>
        <servlet-api.version>2.5</servlet-api.version>
        <dubbo.version>2.8.4</dubbo.version>
        <zookeeper.version>3.4.7</zookeeper.version>
        <zkclient.version>0.1</zkclient.version>
        <mybatis.version>3.2.8</mybatis.version>
        <mybatis.spring.version>1.2.2</mybatis.spring.version>
        <mybatis.paginator.version>1.2.15</mybatis.paginator.version>
        <mysql.version>5.1.32</mysql.version>
        <druid.version>1.0.9</druid.version>
        <commons-fileupload.version>1.3.1</commons-fileupload.version>
        <freemarker.version>2.3.23</freemarker.version>
        <activemq.version>5.11.2</activemq.version>
        <security.version>3.2.3.RELEASE</security.version>
        <solrj.version>4.10.3</solrj.version>
        <ik.version>2012_u6</ik.version>
        <mybatis.generator>1.3.5</mybatis.generator>
    </properties>

    <dependencies>
        <!--对象转换成json的转换器-->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>${jackson.version}</version>
        </dependency>

        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- dubbo相关 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>${dubbo.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>${zookeeper.version}</version>
        </dependency>
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>${zkclient.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.9</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.28</version>
        </dependency>
        <dependency>
            <groupId>javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.11.0.GA</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.10</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>${pagehelper.version}</version>
        </dependency>
        <!-- Mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>${mybatis.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>${mybatis.spring.version}</version>
        </dependency>
        <dependency>
            <groupId>com.github.miemiedev</groupId>
            <artifactId>mybatis-paginator</artifactId>
            <version>${mybatis.paginator.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>${mybatis.generator}</version>
        </dependency>
        <!-- MySql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>
        <!-- 连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>${druid.version}</version>
        </dependency>
        <dependency>
            <groupId>org.csource.fastdfs</groupId>
            <artifactId>fastdfs</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- 文件上传组件 -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>${commons-fileupload.version}</version>
        </dependency>
        <!-- 缓存 -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.8.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.7.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>${freemarker.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-all</artifactId>
            <version>${activemq.version}</version>
        </dependency>
        <!-- 身份验证 -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>4.1.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>4.1.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.github.penggle</groupId>
            <artifactId>kaptcha</artifactId>
            <version>2.3.2</version>
            <exclusions>
                <exclusion>
                    <groupId>javax.servlet</groupId>
                    <artifactId>javax.servlet-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-cas</artifactId>
            <version>4.1.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.jasig.cas.client</groupId>
            <artifactId>cas-client-core</artifactId>
            <version>3.3.3</version>
            <!-- 排除log4j包冲突 -->
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>log4j-over-slf4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- solr客户端 -->
        <dependency>
            <groupId>org.apache.solr</groupId>
            <artifactId>solr-solrj</artifactId>
            <version>${solrj.version}</version>
        </dependency>
        <dependency>
            <groupId>com.janeluo</groupId>
            <artifactId>ikanalyzer</artifactId>
            <version>${ik.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.4.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.3</version>
        </dependency>
        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>
        <dependency>
            <groupId>xml-apis</groupId>
            <artifactId>xml-apis</artifactId>
            <version>1.4.01</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>${project.artifactId}</finalName>

        <pluginManagement>
            <plugins>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.2.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
            </plugins>
        </pluginManagement>


        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>

</project>

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

  5. pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>parent</artifactId>
        <groupId>com.zgh</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../parent/pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubboxDemo-Service</artifactId>
    <packaging>war</packaging>

    <name>dubboxDemo-Service Maven Webapp</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <build>
        <finalName>${artifactId}</finalName>
        <plugins>
            <!-- 配置Tomcat插件 -->
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <!--服务启动端口从9000开始-->
                    <port>9000</port>
                    <!--发布路径,发布到Tomcat的根目录,不用写项目(虚拟路径)名访问-->
                    <path>/</path>
                    <uriEncoding>UTF-8</uriEncoding>
                    <!--插件里显示的名称-->
                    <server>tomcat7</server>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

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

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>parent</artifactId>
        <groupId>com.zgh</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../parent/pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubboxDemo-web</artifactId>
    <packaging>war</packaging>

    <name>dubboxDemo-web Maven Webapp</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <build>
        <finalName>${artifactId}</finalName>
        <plugins>
            <!-- 配置Tomcat插件 -->
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <!--服务启动端口从9000开始-->
                    <port>9001</port>
                    <!--发布路径,发布到Tomcat的根目录,不用写项目(虚拟路径)名访问-->
                    <path>/</path>
                    <uriEncoding>UTF-8</uriEncoding>
                    <!--插件里显示的名称-->
                    <server>tomcat7</server>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

 四。配置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;

@Service
public class UserServiceImpl implements UserService {
    @Override
    public String getName() {
        return "我的 dubboDemo";
    }
}

  3. 配置applicationContext-service.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!--服务名称,可修改,默认为工程名-->
    <dubbo:application name="dubboxdemo-service"/>
    <!--注册地址-->
    <dubbo:registry address="zookeeper://10.20.0.129:2181"/>
    <!--实现接口的包名称-->
    <dubbo:annotation package="com.dubboxDemo.service.impl"/>
    <!--<dubbo:protocol name="dubbo" host="192.168.1.4" port="20883" />-->
</beans>

  4. 配置web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>

  <!-- 加载spring容器 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <!--配置路径-->
    <param-value>classpath:applicationContext*.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
</web-app>

五。配置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

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!--mvc 转换器-->
    <mvc:annotation-driven >
        <mvc:message-converters register-defaults="false">
            <!--转换成字符串-->
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <constructor-arg value="UTF-8" />
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

    <!-- 引用dubbo 服务 -->
    <dubbo:application name="dubboxdemo-web" />
    <!--注册地址-->
    <dubbo:registry address="zookeeper://10.20.0.129:2181"/>
    <!--调用服务的包路径-->
    <dubbo:annotation package="com.dubboxDemo.controller"/>
</beans>

  4. 配置web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>

  <!-- 解决post乱码 -->
  <filter>
    <filter-name>CharacterEncodingFilter</filter-name>
      <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>utf-8</param-value>
    </init-param>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <!--符合url的才会处理乱码-->
  <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <!--所有请求由SpringMVC管理,即交给DispatcherServlet-->
  <servlet>
  <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <!--springMVC 配置文件-->
    <param-value>classpath:applicationContext-web.xml</param-value>
  </init-param>
  </servlet>

  <!--符合url的才会转交-->
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>

六。测试

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

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

 

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

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

 

posted @ 2019-02-15 23:47  Python++  阅读(369)  评论(0编辑  收藏  举报