dubbo的初级例子搭建

dubbo的初级例子搭建


一、服务端

首先新建一个工程,这里采用微服务规范,新建一个user服务

目录格式大概如上。


其中api是暴露给客户端的,接口包含一个传递string方法和一个传递对象(dto)方法。

同样dto暴露给客户端


实现是两个接口的实现

package com.dubbo.demo.impl;

import com.dubbo.demo.api.UserService;
import com.dubbo.demo.dto.UserDTO;
import org.springframework.stereotype.Service;

/**
 * Created by Administrator on 2016-12-12.
 */
@Service("userService")
public class UserServiceImpl implements UserService {

    /**
     * @return
     */
    public UserDTO getUserDTO() {
        UserDTO userDTO = new UserDTO();
        userDTO.setName("一头猪");
        userDTO.setPassword("yitouzhu");
        userDTO.setAge(16);
        return userDTO;
    }
    
    /**
     * @return
     */
    public String getName() {
        return "String name hhhhhhhh";
    }
    
}

启动服务的main方法:

package com.dubbo.demo.startup;

/**
 * Created by Administrator on 2016-12-12.
 */

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 *  启动dubbo 服务
 *  
 */
public class DubboStart {
    public static void main(String[] args) {
        try {
            // 初始化Spring
            ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
            System.out.println("dubbo provider is running...");
            System.in.read();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

配置文件:

<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xsi:schemaLocation="
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
     http://code.alibabatech.com/schema/dubbo
	 http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
	
	
	<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
	
    <context:component-scan base-package="com.dubbo.demo.*"/>

	<!-- enable autowire -->
	<context:annotation-config />


	<!-- 将服务service封装成可以对外开放的服务, 同时提供负载均衡算法,loadbalance可选有random,roundrobin(轮询) -->
	<!--service中加入   mock="return null"当service所有都挂掉以后,client调用时自动获取到return null -->
	<!--service中加入 actives="10" 表示限制所有服务在每个客户端调用都不能同时超过10个 -->
	<!--service中加入  executes="10" 表示限制所有服务在每个服务器端被调用都不能同时超过10个 -->
	<!-- 可以在service中加入内部标签   <dubbo:method name="sayHello" actives="10" />来控制每个方法的执行并发个数 -->
	<!-- timeout="300" retry="2" 超时时间300    重试2次 -->
	<!-- owner=”WangHeping,Guoyong”该服务的负责人 -->
	<dubbo:service interface="com.dubbo.demo.api.UserService" ref="userService" loadbalance="roundrobin" />
	<!-- 提供方应用信息,用于计算依赖关系,不要与消费方一样 -->
	<dubbo:application name="dubbo-service-demo" />
	<!-- 使用multicast广播注册中心暴露服务地址 <dubbo:registry address="multicast://224.5.6.7:1234" 
		/> -->
	<!-- 使用zookeeper注册中心暴露服务地址 -->
	<dubbo:registry address="zookeeper://127.0.0.1:2181" />
	<!-- 用dubbo协议在20880端口暴露服务 ,注意不能与其他服务端口相同  -->
	<!-- dispatcher all所有请求都发到线程池处理,threadpool fixed固定线程池大小,初始化后不进行伸缩,threads 线程池内线程个数 -->
	<!-- <dubbo:protocol accesslog="true" />开启访问日志记录 -->
	<!-- <dubbo:protocol accesslog="http://10.20.160.198/wiki/display/dubbo/foo/bar.log" />规定访问日志的路径 -->
	<!-- <dubbo:protocol name="dubbo" connections="2" accepts="1000"/> dubbo协议使用长连接和nio,这里connections=2表示同时建立两个长连接(要在provier和consumer同时写上)
		accepts=1000  表示为了防止同时过来大量连接而被干掉,限制最大为1000-->
	<dubbo:protocol name="dubbo" port="9090" server="netty" client="netty" 
					serialization="hessian2" charset="UTF-8" dispatcher="all" threadpool="fixed" threads="100" accesslog="true"/>
</beans>


这里注册中心使用的是zk,windows本地安装,做一个假集群可以。很方便。

netty协议,序列化使用hession2 别的好像没什么,dubbo的配置简介方便是真的。


贴一下pom记录吧。

<?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>dubbo</artifactId>
        <groupId>com.cl.dubbo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo-service-impl</artifactId>

    <properties>
        <spring.version>3.0.5.RELEASE</spring.version>
        <slf4j.version>1.6.1</slf4j.version>
        <log4j.version>1.2.16</log4j.version>
    </properties>
    
    
    <dependencies>

        <dependency>
            <groupId>com.cl.dubbo</groupId>
            <artifactId>dubbo-service-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <!-- spring  begin-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</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-context</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>


        <!--spring end -->

        <!-- dubbo -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.3.6</version>
            <exclusions>
                <exclusion>
                    <groupId>com.sun.jmx</groupId>
                    <artifactId>jmxri</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.jdmk</groupId>
                    <artifactId>jmxtools</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>javax.jms</groupId>
                    <artifactId>jms</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>
        <dependency>
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.18.1-GA</version>
        </dependency>
        <!--dubbo end-->

        <!-- slf4j -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <!-- log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>dubbo-web</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

父pom

<?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.cl.dubbo</groupId>
    <artifactId>dubbo</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    
    <modules>
        <module>dubbo-service-api</module>
        <module>dubbo-service-impl</module>
    </modules>


    
    
</project>



下面是客户端


客户端创建一个maven的简单web工程即可,maven是习惯,其他是测别的用的,我这有ssh velocity 等,都可以忽略。

目录如下:



其中action调用服务端接口,获取数据对象

代码如下

package com.dubbo.demo.action;

import javax.annotation.Resource;

import com.dubbo.demo.dto.UserDTO;
import com.dubbo.demo.service.RemoteService;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;


/**
 * cltest
 */
@ParentPackage("default")
@Namespace("/")
public class UserAction extends UserBaseAction {
    
    @Resource
    RemoteService remoteService;

    @Action(value = "index", results = {
            @Result(name = "success", type = "velocity", location = "/content/index.vm")
    })
    public String index(){

        request = getRequest();
        UserDTO userDTO = remoteService.getTestName();
        request.setAttribute("name",userDTO.getName()+" "+userDTO.getPassword()+" "+ userDTO.getAge());
        
        
        return "success";
    }
    
    
}
package com.dubbo.demo.service;

import com.dubbo.demo.api.UserService;
import com.dubbo.demo.dto.UserDTO;
import org.springframework.stereotype.Service;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;

import static org.apache.struts2.ServletActionContext.getServletContext;

/**
 * Created by Administrator on 2016-12-12.
 */
@Service("remoteService")
public class RemoteService {


    /**
     * 调用服务获取name
     * @return
     */
    public UserDTO getTestName(){

        ServletContext context = getServletContext();
        WebApplicationContext applicationContext  = WebApplicationContextUtils.getWebApplicationContext(context);
        UserService userService = (UserService) applicationContext.getBean("userService"); // 获取远程服务代理
        UserDTO userDTO = userService.getUserDTO(); // 执行远程方法
        return userDTO;
    }
    
}

<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xsi:schemaLocation="
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
	 http://code.alibabatech.com/schema/dubbo
	 http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
	
	
	<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
	
    <context:component-scan base-package="com.dubbo.demo.service"/>

	<!-- enable autowire -->
	<context:annotation-config />

	<!--名称-->
	<dubbo:application name="dubbo-service-demo" />
	<dubbo:registry address="zookeeper://127.0.0.1:2181" />
	<!-- 生成远程服务代理,可以像使用本地bean一样使用demoService -->
	<dubbo:reference interface="com.dubbo.demo.api.UserService" id="userService">
	</dubbo:reference>


	<dubbo:protocol name="dubbo" port="9090" server="netty" client="netty" 
					serialization="hessian2" charset="UTF-8" dispatcher="all" threadpool="fixed" threads="100" accesslog="true"/>

</beans>


配置仍然很简单,服务端端口,协议,接口路径 id zk地址 这些基础……



其实初级应用的角度,到这就可以实现一些东西去使用了

但是学习的角度,还需要深入了解dubbo具体服务如何注册的,如何获取,如何建立连接,维护连接池,序列化等等。

后续继续吧,上个月的例子,做个备份。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值