dubbo入門

Dubbo架構:



服務註冊中心使用zookeeper,部署在Linux(192.168.80.129)上。

Maven pom.xml文件:

<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.zzj</groupId>
  <artifactId>dubble</artifactId>
  <version>0.0.1</version>
  <packaging>jar</packaging>

  <name>dubble</name>
  <url>http://maven.apache.org</url>

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

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    
    <dependency>
	    <groupId>com.alibaba</groupId>
	    <artifactId>dubbo</artifactId>
	    <version>2.5.5</version>
	</dependency>
	
	<dependency>
	    <groupId>com.101tec</groupId>
	    <artifactId>zkclient</artifactId>
	    <version>0.3</version>
	</dependency>
    
  </dependencies>
</project>

服務接口類:

package com.zzj.dubble.demo;

public interface DemoService {
	String sayHello(String name);
}

接口實現類:

package com.zzj.dubble.demo.provider;

import com.zzj.dubble.demo.DemoService;

public class DemoServiceImpl implements DemoService {

	public String sayHello(String name) {
        return "Hello " + name;
    }

}

Dobbo提供者:

package com.zzj.dubble;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Hello world!
 *
 */
public class Provider {
	public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] {"classpath:dubbo-demo-provider.xml"});
        context.start();
        System.out.println("Dubbo provider started!");
        System.in.read(); // press any key to exit
        System.out.println("exit!");
    }
}

提供者配置文件:

<?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="demo-provider"/>
    <dubbo:registry protocol="zookeeper" address="192.168.80.129:2181" />
    
    <!-- 默認協議端口20880 -->
    <!-- <dubbo:protocol name="dubbo" port="20880"/> -->
    
    <dubbo:service interface="com.zzj.dubble.demo.DemoService" ref="demoService"/>
    
    <bean id="demoService" class="com.zzj.dubble.demo.provider.DemoServiceImpl"/>
</beans>
Dubbo消費者:
package com.zzj.dubble;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zzj.dubble.demo.DemoService;

public class Consumer {
	public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                new String[]{"classpath:dubbo-demo-consumer.xml"});
        context.start();
        DemoService demoService = (DemoService) context.getBean("demoService"); // obtain proxy object for remote invocation
        String hello = demoService.sayHello("world"); // execute remote invocation
        System.out.println(hello); // show the result
        context.close();
    }
}

消費者配置文件:

<?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="demo-consumer"/>
    
    <dubbo:registry protocol="zookeeper" address="192.168.80.129:2181" />
    
    <dubbo:reference id="demoService" interface="com.zzj.dubble.demo.DemoService"/>
</beans>
啓動提供者:
Dubbo provider started!
運行消費者:
Hello world







發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章