使用dubbo调用服务

创建服务提供者

1.创建服务接口并编写实现类
2.导入jar包,这里使用maven导入

<dependencies>  
	<dependency>
			<groupId>com.alibabagroupId>
			<artifactId>dubboartifactId>
			<version>2.6.5version>
		dependency>

		<dependency>
			<groupId>io.nettygroupId>
			<artifactId>netty-allartifactId>
			<version>4.0.23.Finalversion>
		dependency>
<dependency>
			<groupId>com.101tecgroupId>
			<artifactId>zkclientartifactId>
			<version>0.11version>
		dependency>
		<dependency>
			<groupId>org.apache.curatorgroupId>
			<artifactId>curator-recipesartifactId>
			<version>4.0.1version>
		dependency>
	dependencies>

3.编写服务配置文件provider.xml


<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
	xsi:schemaLocation="http://www.springframework.org/schema/beans   
         http://www.springframework.org/schema/beans/spring-beans-4.3.xsd   
              http://dubbo.apache.org/schema/dubbo      
                http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

	
	<dubbo:application name="provider" />

	
	
	

	

	
	<dubbo:registry protocol="zookeeper" address="192.168.127.99:2181,192.168.127.99:2182,192.168.127.99:2183" />
	
	
	<dubbo:protocol name="dubbo" port="20880" />

	
	<dubbo:service interface="cn.demoService.DemoService"
		ref="demoService" />

	
	<bean id="demoService" class="cn.demoService.impl.DemoServiceImpl" />
beans>

项目结构如下
使用dubbo调用服务_第1张图片
创建测试服务类

public static void main(String[] args)  {
 	ApplicationContext ac = new ClassPathXmlApplicationContext("provider.xml");
 	try {
 		System.in.read();
 	} catch (IOException e) {
 		// TODO Auto-generated catch block
 		e.printStackTrace();
 	}

 }

消费方

1.创建与服务方相同的接口,包名也应该相同
2.创建消费方xml配置文件


<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
	xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        http://dubbo.apache.org/schema/dubbo        http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

	
	<dubbo:application name="providerss" />

	
	


      <dubbo:registry protocol="zookeeper" address="192.168.127.99:2181,192.168.127.99:2182,192.168.127.99:2183" />
      


	
	
	
	
	
	<dubbo:reference id="demoService1" check="false"
		interface="cn.demoService.DemoService"  />
beans>

项目路径
使用dubbo调用服务_第2张图片
创建测试消费类

public class TestMain {

	public static void main(String[] args) {
		ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("customer.xml");
		ac.start();
		DemoService demoService = (DemoService) ac.getBean("demoService1");
		String hello = demoService.getUser("world"); // 执行远程方法
		System.out.println(hello); // 显示调用结果
	}
}

你可能感兴趣的:(其他)