Dubbo+Zookeeper(rest)

来源:互联网 发布:pdf文字编辑器for mac 编辑:程序博客网 时间:2024/09/21 06:37
  • 什么是dubbo?
    DUBBO是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,是阿里巴巴SOA服务化治理方案的核心框架.
    winows环境下:
    先下载:zookeeper链接
    然后解压压缩包:
    这里写图片描述
    在conf文件夹下新建zoo.cfg:
# The number of milliseconds of each ticktickTime=2000# The number of ticks that the initial# synchronization phase can takeinitLimit=10# The number of ticks that can pass between# sending a request and getting an acknowledgementsyncLimit=5# the directory where the snapshot is stored.# do not use /tmp for storage, /tmp here is just# example sakes.dataDir=D:\\zookeeper-3.4.7\\datadataLogDir=D:\\zookeeper-3.4.7\\log# the port at which the clients will connectclientPort=2181# the maximum number of client connections.# increase this if you need to handle more clients#maxClientCnxns=60## Be sure to read the maintenance section of the# administrator guide before turning on autopurge.## http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance## The number of snapshots to retain in dataDir#autopurge.snapRetainCount=3# Purge task interval in hours# Set to "0" to disable auto purge feature#autopurge.purgeInterval=1

双击启动zkServer.cmd


新建maven项目(web项目)

        <!-- zookeeper -->        <!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->        <dependency>            <groupId>org.apache.zookeeper</groupId>            <artifactId>zookeeper</artifactId>            <version>3.4.10</version>        </dependency>        <dependency>            <groupId>com.github.sgroschupf</groupId>            <artifactId>zkclient</artifactId>            <version>0.1</version>        </dependency>        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->        <dependency>            <groupId>com.alibaba</groupId>            <artifactId>fastjson</artifactId>            <version>1.2.32</version>        </dependency>        <!-- dubbo -->        <dependency>            <groupId>org.jboss.resteasy</groupId>            <artifactId>resteasy-jaxrs</artifactId>            <version>3.0.7.Final</version>        </dependency>        <dependency>            <groupId>org.jboss.resteasy</groupId>            <artifactId>resteasy-client</artifactId>            <version>3.0.7.Final</version>        </dependency>        <!-- https://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-jettison-provider -->        <!-- https://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-jettison-provider -->        <dependency>            <groupId>org.jboss.resteasy</groupId>            <artifactId>resteasy-jettison-provider</artifactId>            <version>2.3.4.Final</version>            <scope>test</scope>        </dependency>        <!-- https://mvnrepository.com/artifact/javax.ws.rs/javax.ws.rs-api -->        <dependency>            <groupId>javax.ws.rs</groupId>            <artifactId>javax.ws.rs-api</artifactId>            <version>2.0.1</version>        </dependency>        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->        <dependency>            <groupId>org.apache.httpcomponents</groupId>            <artifactId>httpcore</artifactId>            <version>4.4.5</version>        </dependency>        <!-- https://mvnrepository.com/artifact/org.glassfish.web/el-impl -->        <dependency>            <groupId>org.glassfish.web</groupId>            <artifactId>el-impl</artifactId>            <version>2.2</version>        </dependency>        <!-- https://mvnrepository.com/artifact/javax.el/javax.el-api -->        <dependency>            <groupId>javax.el</groupId>            <artifactId>javax.el-api</artifactId>            <version>3.0.0</version>        </dependency>        <!-- https://mvnrepository.com/artifact/org.javassist/javassist -->        <dependency>            <groupId>org.javassist</groupId>            <artifactId>javassist</artifactId>            <version>3.20.0-GA</version>        </dependency>        <dependency>            <groupId>javax.validation</groupId>            <artifactId>validation-api</artifactId>            <version>1.0.0.GA</version>        </dependency>        <!-- <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId>             <version>2.5.3</version> </dependency> -->        <dependency>            <groupId>com.alibaba</groupId>            <artifactId>dubbo</artifactId>            <version>2.8.4</version>            <exclusions>                <exclusion>                    <artifactId>spring-expression</artifactId>                    <groupId>org.springframework</groupId>                </exclusion>                <exclusion>                    <artifactId>spring-beans</artifactId>                    <groupId>org.springframework</groupId>                </exclusion>                <exclusion>                    <artifactId>spring-aop</artifactId>                    <groupId>org.springframework</groupId>                </exclusion>            </exclusions>        </dependency>        <!-- 如果要使用tomcat server -->        <!-- https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-core -->        <dependency>            <groupId>org.apache.tomcat.embed</groupId>            <artifactId>tomcat-embed-core</artifactId>            <version>8.5.14</version>        </dependency>        <!-- https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-logging-juli -->        <dependency>            <groupId>org.apache.tomcat.embed</groupId>            <artifactId>tomcat-embed-logging-juli</artifactId>            <version>8.5.0</version>        </dependency>

Spring-dubbo.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="hjy_consumer"></dubbo:application>     <!-- 用rest协议在8080端口发布服务 -->    <!-- <dubbo:protocol name="rest" port="8888" threads="500" server="tomcat" accepts="500"                     extension="com.alibaba.dubbo.rpc.protocol.rest.support.LoggingFilter"></dubbo:protocol> -->     <!-- 可以指定fastjson    serialization="fastjson" -->    <dubbo:protocol name="rest" server="tomcat" port="9090" serialization="fastjson"/>      <!-- 使用zookeeper注册中心暴露服务地址 -->      <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->      <dubbo:registry address="zookeeper://172.16.71.165:2181" > </dubbo:registry>    <bean id="userService" class="com.chinaedu.http.UserServiceImpl" />    <!-- 生成远程服务代理,可以像使用本地bean一样使用demoService -->     <dubbo:service interface="com.chinaedu.http.UserService" ref="userService"> </dubbo:service></beans>  

UserService.java:

public interface UserService {    String registerUser(String name);    void regist(User user);    User findyById(String id);}

UserServiceImpl.java:

@Path("users")public class UserServiceImpl implements UserService {    @GET    @Path("regist")    @Consumes(MediaType.APPLICATION_JSON)    @Produces({"application/json; charset=UTF-8", "text/xml; charset=UTF-8"})    @Override    public String registerUser(@QueryParam("name") String name) {//@PathParam("id")        // TODO Auto-generated method stub        System.out.println("1");        System.out.println(name);        User user = new User();        user.setAge(1);        user.setName("ll");        user.setSex("男");        String jsonString = JSON.toJSONString(user);        System.out.println(jsonString);        return jsonString;    }    @POST    @Path("reg")    @Consumes(MediaType.APPLICATION_JSON)    @Produces({MediaType.APPLICATION_JSON})    @Override    public void regist(User user) {        // TODO Auto-generated method stub        System.out.println(user);    }    @GET    @Path("findyById/{id}")    @Consumes(MediaType.APPLICATION_JSON)   //REST框架会自动将JSON数据反序列化为对象    @Produces({MediaType.APPLICATION_JSON})//输出JSON格式的数据。框架会自动将对象序列化为JSON数据    @Override    public User findyById(@PathParam("id")String id) {   //如果将零散参数拼成url形式者,则格式为:  findById/{id}    接收参数时需要加注解: @PathParam("id") String id           // TODO Auto-generated method stub        System.out.println(id);        User user = new User();        user.setAge(1);        user.setName("ll");        user.setSex("男");        return user;    }}

最后启动tomcat:
访问: http://localhost:9090/users/findyById/hys
这里写图片描述

原创粉丝点击