Dubbo 第一个栗子

一、创建一个基础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.imagno</groupId>
  <artifactId>common</artifactId>
  <version>0.0.1-SNAPSHOT</version>

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

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


通用接口文件
package com.imagno.common;

public interface UserService {
	
	public String getUser(Integer i);

}


二、提供者

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.imagno</groupId>
  <artifactId>producer</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

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

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

  <dependencies>
  	<dependency>
	    <groupId>com.alibaba</groupId>
	    <artifactId>dubbo</artifactId>
	    <version>2.5.7</version>
	</dependency>

	<dependency>
		<groupId>com.github.sgroschupf</groupId>
		<artifactId>zkclient</artifactId>
		<version>0.1</version>
	</dependency>
	
        <!-- 上面第一节的通用公共接口,为了减少代码冗余及工作量 -->
	<dependency>
		<groupId>com.imagno</groupId>
		<artifactId>common</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</dependency>
	
  </dependencies>
</project>

java 服务层实现类
package com.imagno.producer.impl;

import com.imagno.common.UserService;

public class UserServiceImpl implements UserService {

	public String getUser(Integer i) {
		System.out.println("被消息过1次");
		if (i == 1) {
			return "Java";
		}
		if (i == 2) {
			return "C++";
		}
		return "Go";
	}

}

package com.imagno.producer;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Provider {
	
	public static void main(String[] args) throws IOException {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
				new String[] { "producer.xml" });
		context.start();
		System.out.println("生产者启动中!");
		System.in.read(); //让程序阻塞
	}
	
}

dubbo 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管理页面比较清晰是哪个应用暴露出来的 -->
    <dubbo:application name="dubbo_provider"></dubbo:application>
    
    <!--zookeeper注册中心 -->  
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />  
	
    <!--使用multicast广播注册中心暴露服务地址 -->  
    <!-- <dubbo:registry address="multicast://10.10.8.101:20881" /> -->  
    <dubbo:protocol name="dubbo" port="20881" />  
    
    <dubbo:service interface="com.imagno.common.UserService" ref="userService"/>
    <bean id="userService" class="com.imagno.producer.impl.UserServiceImpl"/>
</beans>

三、消费者

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.imagno</groupId>
  <artifactId>customer</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

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

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

  <dependencies>
  	<dependency>
	    <groupId>com.alibaba</groupId>
	    <artifactId>dubbo</artifactId>
	    <version>2.5.7</version>
	</dependency>

	<dependency>
		<groupId>com.github.sgroschupf</groupId>
		<artifactId>zkclient</artifactId>
		<version>0.1</version>
	</dependency>
	
        
        <!-- 上面第一节的通用公共接口,为了减少代码冗余及工作量 -->
<dependency><groupId>com.imagno</groupId><artifactId>common</artifactId><version>0.0.1-SNAPSHOT</version></dependency> </dependencies></project>


  
  


消费者java类
package com.imagno.customer;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.imagno.common.UserService;

public class Customer {
	public static void main(String[] args) {
		ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("customer.xml");
		app.start();
		UserService userService = (UserService)app.getBean("userService");
		String name = userService.getUser(2);
		System.out.println(name);
	}
}


dubbo配置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="consumer-app"/>
    
    <!--zookeeper注册中心 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />
    
    <!-- 生成远程服务代理,可以和本地bean一样使用 -->
    <dubbo:reference id="userService" interface="com.imagno.common.UserService"/>
</beans>

相关文章

相关标签/搜索