dubbo API's use

As used herein, fashion maven

1: pom file

    <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: server

// server code 
public  class DubboServer { 

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

    public  static  void initServer () throws IOException {
         // set the application name 
        ApplicationConfig config = new new ApplicationConfig (); 
        config .setName ( "dubboAppServer" ); 

        // connection to the registry 
        RegistryConfig registryConfig = new new RegistryConfig (); 
        registryConfig.setAddress ( "192.168.30.128:2181");  // local virtual machine address 
        registryConfig.setProtocol ( "ZooKeeper" ); 

        // Set the protocol 
        ProtocolConfig protocolConfigRmi = new new ProtocolConfig (); 
        protocolConfigRmi.setPort ( 12880 ); 
        protocolConfigRmi.setName ( "rmi");    // set protocol rmi 

        protocolConfigDubbo ProtocolConfig = new new ProtocolConfig (); 
        protocolConfigDubbo.setPort ( 12881 ); 
        protocolConfigDubbo.setName ( "Dubbo");    // set Dubbo protocol 

        // service provider services expose 
        ServiceConfig <OrderService> serviceServiceConfig = ();new new the ServiceConfig <OrderService> 
        serviceServiceConfig.setApplication (config);     // set the application name 
        serviceServiceConfig.setRegistry (registryConfig);      // Set the registry 
        . serviceServiceConfig 
                setProtocols (Arrays.asList (protocolConfigRmi, protocolConfigDubbo));   // set two protocols 

        serviceServiceConfig .setInterface (OrderService. class );       // set the interface 
        serviceServiceConfig.setRef ( new new OrderServiceImpl ());         // set implementation class 

        serviceServiceConfig.export ();       // exposure and registration service
 
        System.in.read (); 

    }

 

3: Consumer end

// consumer side code 
public  class DubboConsumer { 

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

    public  static  void initConsumer () {
         // set the application name 
        ApplicationConfig config = new new ApplicationConfig (); 
        config.setName ( " dubboAppConsumer " ); 

        // connection to the registry 
        registryConfig registryConfig = new new registryConfig (); 
        registryConfig.setAddress ( " 192.168.30.128:2181 ");   // local virtual machine address
        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);

    }

 

Guess you like

Origin www.cnblogs.com/orange-time/p/11408133.html