dubbo 生产者与消费者

1.api项目 

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.knife</groupId>
	<artifactId>ApiTest</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging>

	<name>ApiTest</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.6.6</version>
		</dependency>
		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
			<version>3.4.10</version>
		</dependency>
		<dependency>
			<groupId>com.101tec</groupId>
			<artifactId>zkclient</artifactId>
			<version>0.5</version>
		</dependency>
		<dependency>
			<groupId>io.netty</groupId>
			<artifactId>netty-all</artifactId>
			<version>4.1.32.Final</version>
		</dependency>
		<dependency>
			<groupId>org.apache.curator</groupId>
			<artifactId>curator-framework</artifactId>
			<version>2.8.0</version>
		</dependency>
		<dependency>
			<groupId>org.apache.curator</groupId>
			<artifactId>curator-recipes</artifactId>
			<version>2.8.0</version>
		</dependency>
	</dependencies>
	<modules>
		<module>DubboProviderTest</module>
		<module>DubboCustomerTest</module>
	</modules>
</project>

api接口

package com.knife.ApiTest;

public interface HelloWorld {

	public String sayHello();
	
}

2.生产者项目

pom文件

<?xml version="1.0"?>
<project
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
	xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.knife</groupId>
		<artifactId>ApiTest</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<groupId>com.knife</groupId>
	<artifactId>DubboCustomerTest</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>DubboCustomerTest</name>
	<url>http://maven.apache.org</url>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
	<dependencies>
		<dependency>
			<groupId>com.knife</groupId>
			<artifactId>ApiTest</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
	</dependencies>
</project>

配置文件

<?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="customer" />

	<dubbo:registry address="zookeeper://localhost:2181" check="false"/>

	<dubbo:reference id="HelloWorld-customer" interface="com.knife.ApiTest.HelloWorld"/>

</beans>

实现类

package org.DubboProviderTest;

import com.knife.ApiTest.HelloWorld;

public class HelloWorldImpl implements HelloWorld {

	public String sayHello() {
		// TODO Auto-generated method stub
		
		System.err.println("be called ...");
		
		return "hello dubbo ...";
	}

}

启动类

package org.DubboProviderTest;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;


public class App 
{
    public static void main( String[] args ) throws IOException
    {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider2.xml");
        context.start();
        System.in.read();
    }
}

3.消费者项目

pom文件

<?xml version="1.0"?>
<project
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
	xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.knife</groupId>
		<artifactId>ApiTest</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<groupId>com.knife</groupId>
	<artifactId>DubboCustomerTest</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>DubboCustomerTest</name>
	<url>http://maven.apache.org</url>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
	<dependencies>
		<dependency>
			<groupId>com.knife</groupId>
			<artifactId>ApiTest</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
	</dependencies>
</project>

配置文件

<?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="customer" />

	<dubbo:registry address="zookeeper://localhost:2181" check="false"/>

	<dubbo:reference id="HelloWorld-customer" interface="com.knife.ApiTest.HelloWorld"/>

</beans>

调用接口类

package org.DubboCustomerTest;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.knife.ApiTest.HelloWorld;

public class App {
	public static void main(String[] args) throws IOException {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("customer2.xml");
		context.start();
		HelloWorld helloWorld = (HelloWorld) context.getBean("HelloWorld-customer");
		String str = helloWorld.sayHello();
		System.err.println(str);
	}
}

4.log4j配置

# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE            debug   info   warn error fatal
log4j.rootCategory=debug, CONSOLE, LOGFILE, info
# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE
# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n

5.测试

依次启动zookeeper 生产者 消费者

 

6.配置文件可以修改成不经过zookeeper 点对点形式

<?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="provider" />

    <dubbo:monitor protocol="registry"/>

    <dubbo:registry address="N/A"/>

    <dubbo:protocol name="dubbo" port="20880"/>

    <dubbo:service interface="com.knife.ApiTest.HelloWorld" ref="HelloWorld-provider"/>

    <bean id="HelloWorld-provider" class="org.DubboProviderTest.HelloWorldImpl"/>

</beans>
<?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="customer" />

	<dubbo:reference id="HelloWorld-customer"
		interface="com.knife.ApiTest.HelloWorld"
		url="dubbo://localhost:20880/com.knife.ApiTest.HelloWorld" />
</beans>

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值