美文网首页我爱编程
001--最简单Dubbo项目

001--最简单Dubbo项目

作者: 糖纸疯了 | 来源:发表于2018-05-25 20:02 被阅读24次

话题一:搭建Dubbo项目

  • 1.Dubbo依赖zookeeper进行服务注册与发现:先搭建zookeeper并运行
  • 2.创建Maven项目,添加pom的dubbo依赖
<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.vincent.dubbo</groupId>
    <artifactId>TDubbo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>server</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <!-- spring begin -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>
        <!-- spring end -->

        <!-- dubbo begin -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
        </dependency>
        <!-- dubbo end -->

        <!-- 注册中心zookeeper begin -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.3.6</version>
        </dependency>
        <!-- 注册中心zookeeper end -->

        <!-- log begin -->
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.15</version>
            <exclusions>
                <exclusion>
                    <groupId>com.sun.jdmk</groupId>
                    <artifactId>jmxtools</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.jmx</groupId>
                    <artifactId>jmxri</artifactId>
                </exclusion>
                <exclusion>
                    <artifactId>jms</artifactId>
                    <groupId>javax.jms</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>mail</artifactId>
                    <groupId>javax.mail</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- log end -->

        <!-- other begin -->
        <dependency>
            <groupId>org.jboss.netty</groupId>
            <artifactId>netty</artifactId>
            <version>3.2.0.Final</version>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.8</version>
        </dependency>
        <!-- other end -->
    </dependencies>
</project>  
  • 3.创建服务提供者
    • 3.1 创建服务接口
public interface DemoService {  
    String sayHello(String name);  
} 
  • 3.2 实现服务接口
public class DemoServiceImpl implements DemoService {  
      public String sayHello(String name) {  
        System.out.println("init : " + name);  
        return "hello " + name;  
    }  
}
  • 3.3 添加配置文件:applicationProvider.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-demo" />
    <!-- zookeeper注册中心 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />
    <dubbo:protocol name="dubbo" port="20880" />

    <!-- 和本地bean一样实现服务 -->
    <bean id="demoService" class="com.xbz.service.impl.DemoServiceImpl" />

    <!-- 向注册中心注册暴漏服务地址,注册服务 -->
    <dubbo:service interface="com.xbz.service.DemoService" ref="demoService" executes="10" />

</beans>  
  • 3.4 发布服务
public class ServerMain {  
  
    @SuppressWarnings("resource")
    public static void main(String[] args) throws Exception {  
        
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationProvider.xml");  
        context.start();  
        System.out.println("Dubbo provider start...");

        try {
            System.in.read();   // 按任意键退出
        } catch (IOException e) {
            e.printStackTrace();
        }   
    }  
}
  • 3.5 自此服务已经发布,等待服务消费者调用
  • 4.创建服务消费者
    • 4.1 新的项目,内容接口和接口和上面是一样的,不再需要服务实现类
public interface DemoService {  
     String sayHello(String name);  
}  
  • 4.2 添加配置文件:applicationProvider.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 ">
    
    <!-- zookeeper注册中心 -->
    <dubbo:application name="dubbo-demo-consumer" />
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />
    
    <!-- 和本地bean一样实现服务 -->
    <dubbo:reference id="demoService" interface="com.xbz.service.DemoService"/>
</beans>
  • 5.创建服务消费者(服务消费)
public class ServerMain {  
  
    @SuppressWarnings("resource")
    public static void main(String[] args) throws Exception {  
        
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationProvider.xml");  
        context.start();  
        System.out.println("Dubbo Consumer start...");

        DemoService demoService = (DemoService) context.getBean("demoService");
        String hello = demoService.sayHello("world");
        System.out.println("------------------------"+hello);
        
        try {
            System.in.read();   // 按任意键退出
        } catch (IOException e) {
            e.printStackTrace();
        }   
    }  
}  

话题二:参考网址

相关文章

网友评论

    本文标题:001--最简单Dubbo项目

    本文链接:https://www.haomeiwen.com/subject/mawjjftx.html