• DUBBO入门


    DUBBO入门

    官方文档:https://dubbo.gitbooks.io/dubbo-user-book/content/preface/

    服务提供者

    项目结构:

    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.xh.dubbo.demon</groupId>
        <artifactId>demon_provider</artifactId>
        <packaging>pom</packaging>
        <version>1.0-SNAPSHOT</version>
        <modules>
            <module>demon_api</module>
            <module>demon_pro</module>
        </modules>
        <name>demon_provider</name>
        <!-- FIXME change it to the project's website -->
        <url>http://www.example.com</url>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <maven.compiler.source>1.7</maven.compiler.source>
            <maven.compiler.target>1.7</maven.compiler.target>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.11</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>com.alibaba</groupId>
                    <artifactId>dubbo</artifactId>
                    <version>2.6.0</version>
                </dependency>
            </dependencies>
        </dependencyManagement>
        <build>
    ...
        </build>
    </project>
    

    api

    接口:

    package com.xh.dubbo.demon.service;
    
    /**
     * Created by root on 4/10/18.
     */
    public interface SayHello {
        public String hello(String name);
    }
    

    pom文件:
    默认

    实现

    实现类:

    package com.xh.dubbo.demon.serviceImpl;
    
    import com.xh.dubbo.demon.service.SayHello;
    
    /**
     * Created by root on 4/10/18.
     */
    public class SayHelloImpl implements SayHello {
        @Override
        public String hello(String name) {
            return "Hello " + name;
        }
    }
    

    启动类:

    package com.xh.dubbo.demon;
    
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import java.io.IOException;
    
    /**
     * Hello world!
     */
    public class App {
        public static void main(String[] args) throws IOException {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"classpath:provider.xml"});
            context.start();
            System.in.read(); // 按任意键退出
        }
    }
    

    配置文件:

    <?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="hello-world-app"/>
    
        <!-- 使用multicast广播注册中心暴露服务地址 -->
        <dubbo:registry address="multicast://224.5.6.7:1234"/>
    
        <!-- 用dubbo协议在20880端口暴露服务 -->
        <dubbo:protocol name="dubbo" port="20880"/>
    
        <!-- 声明需要暴露的服务接口 -->
        <dubbo:service interface="com.xh.dubbo.demon.service.SayHello" ref="helloImpl"/>
    
        <!-- 和本地bean一样实现服务 -->
        <bean id="helloImpl" class="com.xh.dubbo.demon.serviceImpl.SayHelloImpl"/>
    </beans>
    

    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>demon_provider</artifactId>
            <groupId>com.xh.dubbo.demon</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>demon_pro</artifactId>
        <packaging>jar</packaging>
        <name>demon_pro</name>
        <!-- FIXME change it to the project's website -->
        <url>http://www.example.com</url>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <maven.compiler.source>1.7</maven.compiler.source>
            <maven.compiler.target>1.7</maven.compiler.target>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.11</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>com.xh.dubbo.demon</groupId>
                <artifactId>demon_api</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>dubbo</artifactId>
            </dependency>
        </dependencies>
    
        <build>
    ...
        </build>
    </project>
    

    服务消费者

    项目结构:

    调用服务:

    package com.xh.dubbo.demon;
    
    import com.xh.dubbo.demon.service.SayHello;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * Hello world!
     */
    public class App {
        public static void main(String[] args) {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"classpath:consumer.xml"});
            context.start();
            SayHello demoService = (SayHello) context.getBean("demoService"); // 获取远程服务代理
            String hello = demoService.hello("nana "); // 执行远程方法
            System.out.println(hello); // 显示调用结果
        }
    }
    

    配置文件:

    <?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-of-helloworld-app"/>
    
        <!-- 使用multicast广播注册中心暴露发现服务地址 -->
        <dubbo:registry address="multicast://224.5.6.7:1234"/>
    
        <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
        <dubbo:reference id="demoService" interface="com.xh.dubbo.demon.service.SayHello"/>
    </beans>
    

    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.xh.dubbo.demon</groupId>
        <artifactId>demon_consumer</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <name>demon_consumer</name>
        <!-- FIXME change it to the project's website -->
        <url>http://www.example.com</url>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <maven.compiler.source>1.7</maven.compiler.source>
            <maven.compiler.target>1.7</maven.compiler.target>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.11</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>dubbo</artifactId>
                <version>2.6.0</version>
            </dependency>
            <dependency>
                <groupId>com.xh.dubbo.demon</groupId>
                <artifactId>demon_api</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
        </dependencies>
    
        <build>
        ...
        </build>
    </project>
    
  • 相关阅读:
    NodeJS优缺点及适用场景讨论
    gitHub安装步骤
    ubuntu16.04爬坑
    Dubbo入门
    oracle11g的卸载
    数据库对象的创建和管理
    oracle数据库中的面试题
    dml数据操作和tcl事务管理
    oracle sql单行函数 常用函数实例
    oracle查询语句汇总与分类
  • 原文地址:https://www.cnblogs.com/lanqie/p/8778710.html
Copyright © 2020-2023  润新知