程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> dubbo入門,dubbo

dubbo入門,dubbo

編輯:JAVA綜合教程

dubbo入門,dubbo


dubbo官網:dubbo.io

pom.xml引入

 1 <!-- dubbo -->
 2         <dependency>
 3             <groupId>com.alibaba</groupId>
 4             <artifactId>dubbo</artifactId>
 5             <version>2.0.13</version>
 6             <exclusions>
 7                 <exclusion>
 8                     <groupId>org.springframework</groupId>
 9                     <artifactId>spring</artifactId>
10                 </exclusion>
11             </exclusions>
12         </dependency>
13         <dependency>
14             <groupId>org.apache.zookeeper</groupId>
15             <artifactId>zookeeper</artifactId>
16             <version>3.3.6</version>
17             <exclusions>
18                 <exclusion>
19                     <groupId>log4j</groupId>
20                     <artifactId>log4j</artifactId>
21                 </exclusion>
22             </exclusions>
23         </dependency>

DubboService.java

package cn.tomcat.quickstart.dubbo.service;

import java.util.List;


public interface DubboService {
    
    String sayHello(String name);
    
    public List getUsers();
}

DubboServiceImpl.java

package cn.tomcat.quickstart.dubbo.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.tomcat.quickstart.dubbo.service.DubboService;
import cn.tomcat.quickstart.user.model.User;

public class DubboServiceImpl implements DubboService {

    public String sayHello(String name) {
        return "say hello:" + name;
    }

    public List getUsers() {
        List list = new ArrayList();

        User u1 = new User();
        u1.setUserName("jack");

        User u2 = new User();
        u1.setUserName("tom");

        User u3 = new User();
        u1.setUserName("ccc");

        list.add(u1);
        list.add(u2);
        list.add(u3);

        return list;
    }

}

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: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-dubbo-qucikstart" />
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />
    <dubbo:protocol name="dubbo" port="20880" />
    <dubbo:reference interface="cn.tomcat.quickstart.dubbo.service.DubboService"
        id="demoService" />
</beans>

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"
    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="dubbo-qucikstart" />
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />
    <dubbo:protocol name="dubbo" port="20880" />
    <dubbo:service ref="dubboService"
        interface="cn.tomcat.quickstart.dubbo.service.DubboService" />
    <bean id="dubboService"
        class="cn.tomcat.quickstart.dubbo.service.impl.DubboServiceImpl" />
</beans>

ProviderTest.java提供者測試

package cn.tomcat.quickstart.dubbo;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ProviderTest {

    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] { "classpath:dubbo-provider.xml" });
        context.start();
        System.in.read(); 
    }
}

ConsumerTest.java消費者測試

package cn.tomcat.quickstart.dubbo;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ProviderTest {

    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] { "classpath:dubbo-provider.xml" });
        context.start();
        System.in.read(); 
    }
}

 

現在用的是zookeeper注冊中心,和dubbo協議,提前得安裝好zookeeper注冊中心,並啟動

先執行ProviderTest,然後再執行ConsumerTest,這只是一個簡單小demo

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved