其他分享
首页 > 其他分享> > dubbo API的使用方式

dubbo API的使用方式

作者:互联网

本文使用maven方式

1:pom文件

    <dependencies>
        <!-- 引入spring的jar -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.0.2.RELEASE</version>
        </dependency>

        <!-- 引入zk -->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.5</version>
        </dependency>

        <!-- 引入dubbo -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.7</version>
            <scope>compile</scope>
            <exclusions>
                <exclusion>
                    <artifactId>spring</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>

    </dependencies>

 

2:服务端

//服务端的代码
public class DubboServer {

    public static void main(String[] args) throws IOException {
        initServer();
    }

    public static void initServer() throws IOException {
        //设置应用名称
        ApplicationConfig config = new ApplicationConfig();
        config.setName("dubboAppServer");

        //连接注册中心
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress("192.168.30.128:2181");  //本地虚拟机的地址
        registryConfig.setProtocol("zookeeper");

        //设置协议
        ProtocolConfig protocolConfigRmi = new ProtocolConfig();
        protocolConfigRmi.setPort(12880);
        protocolConfigRmi.setName("rmi");   //设置rmi的协议

        ProtocolConfig protocolConfigDubbo = new ProtocolConfig();
        protocolConfigDubbo.setPort(12881);
        protocolConfigDubbo.setName("dubbo");   //设置dubbo的协议

        //服务提供者暴露服务
        ServiceConfig<OrderService> serviceServiceConfig = new ServiceConfig<OrderService>();
        serviceServiceConfig.setApplication(config);    //设置应用名称
        serviceServiceConfig.setRegistry(registryConfig);     //设置注册中心
        serviceServiceConfig.
                setProtocols(Arrays.asList(protocolConfigRmi,protocolConfigDubbo));  //设置两个协议

        serviceServiceConfig.setInterface(OrderService.class);      //设置接口
        serviceServiceConfig.setRef(new OrderServiceImpl());        //设置具体实现类

        serviceServiceConfig.export();      //暴露和注册服务

        System.in.read();

    }

 

3:消费端

//消费端代码
public class DubboConsumer {

    public static void main(String[] args) {
        initConsumer();
    }

    public static void initConsumer(){
        //设置应用名称
        ApplicationConfig config = new ApplicationConfig();
        config.setName("dubboAppConsumer");

        //连接注册中心
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress("192.168.30.128:2181");  //本地虚拟机的地址
        registryConfig.setProtocol("zookeeper");

        ReferenceConfig<OrderService> referenceConfig = new ReferenceConfig<OrderService>();
        referenceConfig.setApplication(config);
        referenceConfig.setRegistry(registryConfig);
        referenceConfig.setInterface(OrderService.class);
        referenceConfig.setProtocol("dubbo");


        OrderService order = referenceConfig.get();
        Integer num = order.buyShop();   //具体的调用
        referenceConfig.destroy();

        System.out.println(num);

    }

 

标签:dubbo,方式,registryConfig,API,serviceServiceConfig,new,referenceConfig,config,publ
来源: https://www.cnblogs.com/orange-time/p/11408133.html