windows搭建 zookeeper + dubbo + dubbo web admin

来源:互联网 发布:观察者模式 java 编辑:程序博客网 时间:2024/05/29 00:32

一.结构图


二.启动顺序

1.启动zookeeper

    配置:\zookeeper\conf\zoo.cfg

    执行 : zookeeper\bin\zkServer.cmd 

2.启动服务提供者(dubbo服务包含在内)

    启动tomcat即可

    

spring 配置文件

<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        ">    <!-- 具体的实现bean -->    <bean id="demoService" class="com.springapp.mvc.service.impl.DemoServiceImpl" />    <!-- 提供方应用信息,用于计算依赖关系 -->    <dubbo:application name="xs_provider" />    <!-- 使用multicast广播注册中心暴露服务地址 -->    <!--<dubbo:registry address="multicast://224.5.6.7:1234" /> -->    <!-- 使用zookeeper注册中心暴露服务地址 -即zookeeper的所在服务器ip地址和端口号 -->    <dubbo:registry address="zookeeper://127.0.0.1:2181" />    <!-- 用dubbo协议在20880端口暴露服务 -->    <dubbo:protocol name="dubbo" port="20880" />    <!-- 声明需要暴露的服务接口 -->    <dubbo:service interface="com.springapp.mvc.service.DemoService"                   ref="demoService" /></beans>
web.xml配置文件

<web-app version="2.4"xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><display-name>Spring MVC Application</display-name><welcome-file-list><welcome-file>test.html</welcome-file></welcome-file-list>    <!--<servlet>--><!--<servlet-name>mvc-dispatcher</servlet-name>--><!--<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>-->        <!--<load-on-startup>1</load-on-startup>--><!--</servlet>--><!--<servlet-mapping>--><!--<servlet-name>mvc-dispatcher</servlet-name>--><!--<url-pattern>/</url-pattern>--><!--</servlet-mapping>--><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/mvc-*.xml</param-value></context-param></web-app>
pom文件

<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/maven-v4_0_0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupId>com.springapp</groupId>    <artifactId>springMvc</artifactId>    <packaging>war</packaging>    <version>1.0-SNAPSHOT</version>    <name>springMvc</name>    <properties>        <spring.version>4.1.1.RELEASE</spring.version>    </properties>    <dependencies>        <dependency>            <groupId>com.github.sgroschupf</groupId>            <artifactId>zkclient</artifactId>            <version>0.1</version>        </dependency>        <dependency>            <groupId>com.alibaba</groupId>            <artifactId>dubbo</artifactId>            <version>2.5.3</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-core</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-web</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>javax.servlet</groupId>            <artifactId>servlet-api</artifactId>            <version>2.5</version>        </dependency>        <dependency>            <groupId>javax.servlet.jsp</groupId>            <artifactId>jsp-api</artifactId>            <version>2.1</version>            <scope>provided</scope>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-webmvc</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-test</artifactId>            <version>${spring.version}</version>            <scope>test</scope>        </dependency>        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <version>4.11</version>            <scope>test</scope>        </dependency>    </dependencies>    <build>        <finalName>springMvc</finalName>        <plugins>            <plugin>                <artifactId>maven-compiler-plugin</artifactId>                <configuration>                    <source>1.6</source>                    <target>1.6</target>                </configuration>            </plugin>            <plugin>                <artifactId>maven-surefire-plugin</artifactId>                <configuration>                    <includes>                        <include>**/*Tests.java</include>                    </includes>                </configuration>            </plugin>        </plugins>    </build></project>
java代码

package com.springapp.mvc.service;import java.util.List;public interface DemoService {    String sayHello(String name);    public List getUsers();}


package com.springapp.mvc.service.impl;import com.springapp.mvc.service.DemoService;import java.util.ArrayList;import java.util.List;public class DemoServiceImpl implements DemoService {    @Override    public String sayHello(String name) {        return "Hello " + name;    }    @Override    public List getUsers() {        return new ArrayList();    }}

3.启动dubbo_web_admin

    将war包放在tomcat 的webapp目录下即可

    访问地址:http://localhost:8080/     端口根据自己tomcat配置为准

    配置文件 \tomcat7\webapps\dubbo-admin-2.5.3\WEB-INF\dubbo.properties       root和guest为登录账号密码 zookeeper://127.0.0.1:2181 为zookeeper地址

dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest



4.启动服务消费者

    pom.xml同服务提供者

    项目结构

    

    spring配置文件

<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="hjy_consumer" />    <!-- 使用zookeeper注册中心暴露服务地址 -->    <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->    <dubbo:registry address="zookeeper://127.0.0.1:2181" />    <!-- 生成远程服务代理,可以像使用本地bean一样使用demoService -->    <dubbo:reference id="demoService"                     interface="com.springapp.mvc.service.DemoService" /></beans>

Java代码

package com.springapp.mvc.service;import java.util.List;public interface DemoService {    String sayHello(String name);    public List getUsers();}

直接获取spring上下文,调用服务提供者的服务

package com.springapp.mvc;import com.springapp.mvc.service.DemoService;import org.springframework.context.support.FileSystemXmlApplicationContext;import java.util.List;/** * Created by acer on 2017/3/2. */public class Consumer {    public static void main(String[] args) throws Exception {        FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("D:\\project\\druid-master\\consumer\\src\\main\\webapp\\WEB-INF\\mvc-dispatcher-servlet.xml");        context.start();        DemoService demoService = (DemoService) context.getBean("demoService");        String hello = demoService.sayHello("hejingyuan");        System.out.println(hello);        List list = demoService.getUsers();        if (list != null && list.size() > 0) {            for (int i = 0; i < list.size(); i++) {                System.out.println(list.get(i));            }        }        System.in.read();    }}

5.查看服务调用结果




所用到的安装包等资源

dubbo-admin-2.5.3.war:http://download.csdn.net/detail/tangyali516/9219461

zookeeper-3.3.6.tar.gz :http://download.csdn.net/download/zknxx/9629211

0 0