Dubbo+Zookeeper(rest)

  • 什么是dubbo?
    DUBBO是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,是阿里巴巴SOA服务化治理方案的核心框架.
    winows环境下:
    先下载:zookeeper链接
    然后解压压缩包:
    Dubbo+Zookeeper(rest)_第1张图片
    在conf文件夹下新建zoo.cfg:
# The number of milliseconds of each tick

tickTime=2000

# The number of ticks that the initial

# synchronization phase can take

initLimit=10

# The number of ticks that can pass between

# sending a request and getting an acknowledgement

syncLimit=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\\data

dataLogDir=D:\\zookeeper-3.4.7\\log

# the port at which the clients will connect

clientPort=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 disab
le auto purge feature

#autopurge.purgeInterval=1

双击启动zkServer.cmd


新建maven项目(web项目)

        
        
        <dependency>
            <groupId>org.apache.zookeepergroupId>
            <artifactId>zookeeperartifactId>
            <version>3.4.10version>
        dependency>


        <dependency>
            <groupId>com.github.sgroschupfgroupId>
            <artifactId>zkclientartifactId>
            <version>0.1version>
        dependency>

        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>fastjsonartifactId>
            <version>1.2.32version>
        dependency>


        
        <dependency>
            <groupId>org.jboss.resteasygroupId>
            <artifactId>resteasy-jaxrsartifactId>
            <version>3.0.7.Finalversion>
        dependency>
        <dependency>
            <groupId>org.jboss.resteasygroupId>
            <artifactId>resteasy-clientartifactId>
            <version>3.0.7.Finalversion>
        dependency>
        
        
        <dependency>
            <groupId>org.jboss.resteasygroupId>
            <artifactId>resteasy-jettison-providerartifactId>
            <version>2.3.4.Finalversion>
            <scope>testscope>
        dependency>
        
        <dependency>
            <groupId>javax.ws.rsgroupId>
            <artifactId>javax.ws.rs-apiartifactId>
            <version>2.0.1version>
        dependency>
        
        <dependency>
            <groupId>org.apache.httpcomponentsgroupId>
            <artifactId>httpcoreartifactId>
            <version>4.4.5version>
        dependency>
        
        <dependency>
            <groupId>org.glassfish.webgroupId>
            <artifactId>el-implartifactId>
            <version>2.2version>
        dependency>
        
        <dependency>
            <groupId>javax.elgroupId>
            <artifactId>javax.el-apiartifactId>
            <version>3.0.0version>
        dependency>
        
        <dependency>
            <groupId>org.javassistgroupId>
            <artifactId>javassistartifactId>
            <version>3.20.0-GAversion>
        dependency>
        <dependency>
            <groupId>javax.validationgroupId>
            <artifactId>validation-apiartifactId>
            <version>1.0.0.GAversion>
        dependency>

        

        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>dubboartifactId>
            <version>2.8.4version>
            <exclusions>
                <exclusion>
                    <artifactId>spring-expressionartifactId>
                    <groupId>org.springframeworkgroupId>
                exclusion>
                <exclusion>
                    <artifactId>spring-beansartifactId>
                    <groupId>org.springframeworkgroupId>
                exclusion>
                <exclusion>
                    <artifactId>spring-aopartifactId>
                    <groupId>org.springframeworkgroupId>
                exclusion>
            exclusions>
        dependency>

        
        
        <dependency>
            <groupId>org.apache.tomcat.embedgroupId>
            <artifactId>tomcat-embed-coreartifactId>
            <version>8.5.14version>
        dependency>
        
        <dependency>
            <groupId>org.apache.tomcat.embedgroupId>
            <artifactId>tomcat-embed-logging-juliartifactId>
            <version>8.5.0version>
        dependency>

Spring-dubbo.xml



<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>

     
    
     
    <dubbo:protocol name="rest" server="tomcat" port="9090" serialization="fastjson"/>  

      
      
    <dubbo:registry address="zookeeper://172.16.71.165:2181" > dubbo:registry>

    <bean id="userService" class="com.chinaedu.http.UserServiceImpl" />

     
    <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
Dubbo+Zookeeper(rest)_第2张图片

你可能感兴趣的:(发布服务,dubbo)