首页 > 代码库 > dubbox2.8.4例子教程一

dubbox2.8.4例子教程一

简介

Dubbo是一个来自阿里巴巴的开源分布式服务框架,当当根据自身的需求,为Dubbo实现了一些新的功能,包括REST风格远程调用、Kryo/FST序列化等等。并将其命名为Dubbox(即Dubbo eXtensions),教程一就写生产者,教程二就写消费者

一、生产者工程结构

技术分享 技术分享

 

二、Simple.java(测试普通dubbo功能)

package bhz.entity;

import java.io.Serializable;
import java.util.Map;

public class Simple  implements Serializable
{  
     private static final long serialVersionUID = -4914434736682797743L;  
     private String name;  
     private int age;  
     private Map<String,Integer> map;  
     public Simple(){  
  
     }  
     public Simple(String name,int age,Map<String,Integer> map){  
         this.name = name;  
         this.age = age;  
         this.map = map;  
     }  
  
     public String getName() {  
       return name;  
     }  
  
     public void setName(String name) {  
        this.name = name;  
     }  
  
     public int getAge() {  
        return age;  
     }  
  
     public void setAge(int age) {  
        this.age = age;  
     }  
  
     public Map<String, Integer> getMap() {  
        return map;  
     }  
  
     public void setMap(Map<String, Integer> map) {  
        this.map = map;  
     }  
  
  
}  

2、User.java(测试dubbox特有的rest服务)

package bhz.entity;

import java.io.Serializable;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;

import org.codehaus.jackson.annotate.JsonProperty;

@XmlRootElement
public class User implements Serializable
{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @NotNull
    private String id;

    @NotNull
    @Size(min = 6, max = 50)
    private String name;
    
    public User() {
    }
    
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    
}

3、dubbo-provider.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"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
    
    <!-- 引入properties配置文件 -->
    <util:properties id="zcparams" location="classpath:params.properties"></util:properties>
    
    <!-- 指定dubbox生产者名称 -->
    <dubbo:application name="provider" owner="programmer" organization="dubbox"/>

    <!-- zookeeper注册中心 -->
    <dubbo:registry address="zookeeper://192.168.0.4:2181?backup=192.168.0.5:2181,192.168.0.6:2181"/>

    <dubbo:annotation package="bhz.service" />
    <!-- kryo实现序列化-->
    <dubbo:protocol name="dubbo"  serialization="kryo" optimizer="bhz.utils.SerializationOptimizerImpl" /> 

    <!--指定 rest服务 -->
    <dubbo:protocol name="rest" server="tomcat" port="8888" contextpath="provider" accepts="500" />

</beans>

4、SimpleService.java

package bhz.service;

import bhz.entity.Simple;
/**
 * 测试普通dubbo服务
 * @author Administrator
 *
 */
public interface SimpleService {
    
    public String sayHello(String name);
 
    public Simple getSimple();
}

5、UserService.java

package bhz.service;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.alibaba.dubbo.rpc.protocol.rest.support.ContentType;

import bhz.entity.User;
/**
 * 功能:本类测试dubbox的rest服务
 * @author Administrator
 *
 */
@Path("/userService")
@Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_XML})    // @Consumes 注释代表的是一个资源可以接受的 MIME 类型
@Produces({ContentType.APPLICATION_JSON_UTF_8, ContentType.TEXT_XML_UTF_8})    //@Produces 注释代表的是一个资源可以返回的 MIME 类型
public interface UserService {
    @GET
    @Path("/testget")
    public void testget();
    
    @GET
    @Path("/getUser")
    public User getUser();
    
    @GET
    @Path("/get/{id : \\d+}")
    public User getUser(@PathParam(value = "http://www.mamicode.com/id") Integer id);
    
    @GET
    @Path("/get/{id : \\d+}/{name : [a-zA-Z][0-9]}")
    public User getUser(@PathParam(value = "http://www.mamicode.com/id") Integer id, @PathParam(value = "http://www.mamicode.com/name") String name);
    
    @POST
    @Path("/testpost")
    public void testpost();
    
    @POST
    @Path("/postUser")
    public User postUser(User user);
    
    @POST
    @Path("/post/{id}")
    public User postUser(@PathParam(value = "http://www.mamicode.com/id") String id);

}

6、SimpleServiceImpl.java

package bhz.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Service;

import bhz.entity.Simple;
import bhz.service.SimpleService;
//spring注解
//@Service("simpleService") 
/*
 * dubbo注解
 * interfaceClass 指定服务接口
 * protocol 指定协议
 * retries 重试策略
 */
@com.alibaba.dubbo.config.annotation.Service(interfaceClass=bhz.service.SimpleService.class, protocol={"dubbo"}, retries=0)
public class SimpleServiceImpl implements SimpleService{

    @Override
    public String sayHello(String name) {
        return "hello" + name;
    }

    @Override
    public Simple getSimple() {
        Map<String,Integer> map = new HashMap<String, Integer>(2);  
        map.put("zhang0", 1);  
        map.put("zhang1", 2);  
        return new Simple("zhang3", 21, map);
    }

}

7、UserServiceImpl.java

package bhz.service.impl;

import bhz.entity.User;
import bhz.service.UserService;


//这个是dubbo的注解(同时提供dubbo本地,和rest方式)
@com.alibaba.dubbo.config.annotation.Service(interfaceClass=bhz.service.UserService.class, protocol = {"rest", "dubbo"}, retries=0)
public class UserServiceImpl implements UserService{

    public void testget() {
        //http://localhost:8888/provider/userService/getUser
        System.out.println("测试...get");
    }
    

    public User getUser() {
        System.out.println("==========");
        User user = new User();
        user.setId("1001");
        user.setName("张三");
        return user;
    }


    public User getUser(Integer id) {
        System.out.println("测试传入int类型的id: " + id);
        User user = new User();
        user.setId("1001");
        user.setName("张三");
        return user;
    }
    
    

    public User getUser(Integer id, String name) {

        System.out.println("测试俩个参数:");
        System.out.println("id: " + id);
        System.out.println("name: " + name);
        User user = new User();
        user.setId("1001");
        user.setName("张三");
        return user;
    }
    

    public void testpost() {
        System.out.println("测试...post");
    }
    

    public User postUser(User user) {
        System.out.println(user.getName());
        System.out.println("测试...postUser");
        User user1 = new User();
        user1.setId("1001");
        user1.setName("张三");
        return user1;
    }


    public User postUser(String id) {
        System.out.println(id);
        System.out.println("测试...post");
        User user = new User();
        user.setId("1001");
        user.setName("张三");
        return user;
    }
    
    

}

8、FastJsonConvert.java(fastJson转换工具类)

package bhz.utils;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

/**
 * 功能:fastJson转换工具类
 * @author jacky
 *
 */
public class FastJsonConvert {


    private static final SerializerFeature[] featuresWithNullValue =http://www.mamicode.com/ { SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullBooleanAsFalse,
            SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteNullNumberAsZero, SerializerFeature.WriteNullStringAsEmpty };

    /**
     * JsonString 转换 Object
     * 
     * @param <T>
     * @param data
     * @param clzss
     * @return
     */
    public static <T> T convertJSONToObject(String data, Class<T> clzss) {
        try {
            T t = JSON.parseObject(data, clzss);
            return t;
        } catch (Exception e) {
            e.printStackTrace();
            return null;

        }
    }
    /**
     * JsonString 转换 List<Object>
     * 
     * @param <T>
     * @param data
     * @param clzss
     * @return
     */
    public static <T> List<T> convertJSONToArray(String data, Class<T> clzss) {
        try {
            List<T> t = JSON.parseArray(data, clzss);
            return t;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
            
        }
    }

    /**
     * 功能:把java对象序列化成json字符串
     * @param obj  java实体
     * @return   json字符串
     */
    public static String convertObjectToJSON(Object obj) {
        try {
            String text = JSON.toJSONString(obj);
            System.out.println(text);
            return text;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * Object to JsonString
     *
     * @param <T>
     * @param data
     * @param valueType
     * @return
     */
    public static String convertObjectToJSONWithNullValue(Object obj) {
        try {
            String text = JSON.toJSONString(obj, featuresWithNullValue);
            return text;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {
        System.err.println(System.getProperties());
    }
}

9、log4j.properties

log4j.rootLogger=INFO, console, file

log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d %p [%c] - %m%n

log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
#log4j.appender.file.File=D:/002_developer/workspace_001/zcmoni.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %p [%c] - %m%n

log4j.logger.org.springframework=WARN

10、params.properties

#HttpCaller
max_total_connections = 1600
max_route_connections = 1200
connect_timeout = 30000
read_timeout = 30000

11、Provider.java(启动dubbo服务类)

package bhz.test;


import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Provider {

    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] { "dubbo-provider.xml" });
        context.start();
        System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟
    }
}

12、HttpTest1.java(测试dubbox的rest服务类)

package bhz.test;

import bhz.entity.User;
import bhz.utils.FastJsonConvert;
import bhz.utils.HttpProxy;

public class HttpTest1 {

    public static void main(String[] args) throws Exception{
        User user = new User();
        user.setId("1001");
        user.setName("李四");
        String responseStr1 = HttpProxy.postJson("http://localhost:8888/provider/userService/postUser",
                FastJsonConvert.convertObjectToJSON(user));
        System.out.println("post请求返回的结果"+responseStr1);
        System.out.println("========================");
        String string = HttpProxy.get("http://localhost:8888/provider/userService/getUser");
        System.out.println("get请求返回的结果"+string);
        
    }
}

13、测试结果

========================
HTTP/1.1 200 OK
Response content length: -1
Response content: {"id":"1001","name":"张三"}
------------------------------------
get请求返回的结果{"id":"1001","name":"张三"}

参考资料地址

http://dangdangdotcom.github.io/dubbox/

http://www.open-open.com/lib/view/open1417426480618.html

dubbox2.8.4例子教程一