Dubbo getting started, dubbo

Source: Internet
Author: User

Dubbo getting started, dubbo

Dubbo Official Website: dubbo. io

Introduction of 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 consumer Configuration

<?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 provider Configuration

<?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 provider Test

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

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();     }}

 

Now we use the zookeeper Registration Center and the dubbo protocol. We have to install the zookeeper registration center in advance and start it.

Run ProviderTest first and then ConsumerTest. This is just a simple demo.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.