dubbo使用

 

dubbo是阿里开源的一个分布式服务调用框架

 

分布式通信:

① 服务自动注册与发现;
② 软负载均衡,容错机制; 
③ 远程通信;

dubbo使用_第1张图片
 
 
1. 添加dubbo依赖:
<!-- dubbo -->
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-context</artifactId>
           <version>3.2.4.RELEASE</version>
       </dependency>
       <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.3</version>
       </dependency>
       <dependency>
           <groupId>com.github.sgroschupf</groupId>
           <artifactId>zkclient</artifactId>
           <version>0.1</version>
       </dependency>
       <dependency>
           <groupId>com.netflix.curator</groupId>
           <artifactId>curator-framework</artifactId>
           <version>1.1.3</version>
       </dependency>
        <!-- ****** -->
 
2. 发布dubbo服务
dubbo-product.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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:applicationname="provider-v1"  />

    <!-- 使用multicast广播注册中心暴露服务地址 -->
    <!--<dubbo:registry address="multicast://224.5.6.7:1234" />-->
    <dubbo:registry  protocol="zookeeper"address="127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183"/>
    <!--<dubbo:registry address="zookeeper://10.11.152.10:2181"/>-->

    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocolname="dubbo"port="20880"/>

    <beanid="mengkaService"class="com.mengka.dubbo.service.impl.MengkaServiceImpl"/>
    <dubbo:serviceinterface="com.mengka.dubbo.service.MengkaService"ref="mengkaService"/>

</beans>
 
dubbo服务启动
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 *   dubbo服务启动
 *
 * User: mengka.hyy
 * Date: 14-11-16-下午3:08
 */
public class start_server {

    private static final Log log = LogFactory.getLog(start_server.class);

    public static void main(String[] args)throws Exception{
        System.out.println("start_server process.........");
        String serviceConfigXMLs[] = new String[] {"dubbo-product.xml"};
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(serviceConfigXMLs);
        context.start();

        System.in.read(); // 按任意键退
    }

}
 
3. 调用服务
dubbo-consumer.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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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="consumer-v1"/>

    <!-- 使用multicast广播注册中心暴露发现服务地址 -->
    <!--<dubbo:registry address="multicast://224.5.6.7:1234"/>-->
    <dubbo:registry  protocol="zookeeper" address="127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183" />
    <!--<dubbo:registry address="zookeeper://127.0.0.1:2181" />-->
    <!--<dubbo:registry address="zookeeper://10.11.152.10:2181" />-->

    <dubbo:reference id="mengkaService" interface="com.mengka.dubbo.service.MengkaService"/>


</beans>
 
applicationContext.xml
 <context:annotation-config/>
    <context:component-scan base-package="com.mengka.*"/>


    <import resource="dubbo-consumer.xml"/>
 
/**
 * http://127.0.0.1:8080/struts_02/mengka.action
 */
@Component
publicclass MengkaAction extends ActionSupport implements InitializingBean {

    privatestaticfinallongserialVersionUID = 6231765583948793060L;

    privatestaticfinal Log log = LogFactory.getLog(MengkaAction.class);

    private MengkaService mengkaService;


    @Override
    public String execute() throws Exception {

        /**
         *  获取bean
         */
        ServletContext servletContext = ServletActionContext.getServletContext();
        WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
        mengkaService = (MengkaService) context.getBean("mengkaService");

        log.info("---------------, mengkaAction execute...");



        /**
         *  调用dubbo服务
         */
        String baicai = mengkaService.baicai("111");
        log.info("---------------, dubbo mengkaService baicai = " + baicai);

        returnSUCCESS;
    }
 
 

你可能感兴趣的:(DUBBO)