Dubbo注册和使用服务的两种配置方式(xml配置方式和代码注解方式)

Dubbo 一个可以让我们轻松的把一个服务做成集群的开源框架,这里主要介绍以下使用Dubbo是注册和使用服务的两种方式:

1.xml配置方式:

首先也是一个基本的Dubbo项目的搭建:

 a.新建一个springboot的maven项目:

Dubbo服务提供工程目录浏览:

application-spring.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="hello-world-app1"/>
	<dubbo:registry  protocol="zookeeper"  address="127.0.0.1:2181"  />
   	<dubbo:protocol name="dubbo" port="20881" />     
	<dubbo:service interface="com.yangs.test.service.DemoServer" ref="demoService" />
	<bean id="demoService" class="com.yangs.test.service.impl.DemoServerImpl" /> 

	
</beans>

DemoSe;:rver的实现类:

package com.yangs.test.service.impl;

public class DemoServerImpl implements DemoServer,Serializable {

    @Override
    public String sayHello(String name) throws Exception {
        String str = "Hello app2 "+name +" now time :"+System.currentTimeMillis();
        System.err.println("有客户端调用这个服务了"+str);
        return str;
    }

}

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.yangs.test</groupId>
	<artifactId>DubboAndSpringBootTest</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>DubboAndSpringBootTest</name>
	<url>http://maven.apache.org</url>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.3.3.RELEASE</version>
	</parent>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.7</version>
			<scope>test</scope>
		</dependency>
		<dependency>
	  <groupId>com.alibaba</groupId>
	  <artifactId>dubbo</artifactId>
	  <version>2.5.3</version>
	</dependency>
	<!-- 这里引入 spring-boot-starter-web 可以把spring基本的相关jar引入进来-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
	  <groupId>com.github.sgroschupf</groupId>
	  <artifactId>zkclient</artifactId>
	  <version>0.1</version>
	</dependency>
	<!--  zookeeper -->
	<dependency>
	  <groupId>org.apache.zookeeper</groupId>
	  <artifactId>zookeeper</artifactId>
	  <version>3.3.6</version>
	</dependency>
		<dependency> 
         <groupId>com.101tec</groupId>
         <artifactId>zkclient</artifactId>
          <version>0.10</version>
</dependency>
	</dependencies>
	
	<build>

		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin </artifactId>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

启动类:

public class ApplicationMain {
	public static void main(String[] args) {
		try {
			ClassPathXmlApplicationContext applicationContext = 
					new ClassPathXmlApplicationContext(new String[]{"application-spring.xml"});
			applicationContext.start();
			System.err.println("*******************************");
				System.in.read();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}	

 

b,Dubbo服务使用:

Dubbo服务使用工程目录浏览:

application-spring.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-of-helloworld-app" />      
	 <!-- 使用multicast广播注册中心暴露发现服务地址 -->
	<dubbo:registry  protocol="zookeeper"  address="127.0.0.1:2181" />        
	 <!-- 生成远程服务代理,可以和本地bean一样使用demoService   通过配置实现 -->
	<dubbo:reference id="demoService" interface="com.yangs.test.service.DemoServer" /> 
	
	
</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>DubboAndSpringBootTestClinet</groupId>
  <artifactId>DubboAndSpringBootTestClinet</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>DubboAndSpringBootTestClinet</name>
  <url>http://maven.apache.org</url>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.3.3.RELEASE</version>
	</parent>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.7</version>
			<scope>test</scope>
		</dependency>
		<dependency>
	  <groupId>com.alibaba</groupId>
	  <artifactId>dubbo</artifactId>
	  <version>2.5.3</version>
	</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
	  <groupId>com.github.sgroschupf</groupId>
	  <artifactId>zkclient</artifactId>
	  <version>0.1</version>
	</dependency>
	<!--  zookeeper -->
	<dependency>
	  <groupId>org.apache.zookeeper</groupId>
	  <artifactId>zookeeper</artifactId>
	  <version>3.3.6</version>
	</dependency>
		<dependency> 
         <groupId>com.101tec</groupId>
         <artifactId>zkclient</artifactId>
          <version>0.10</version>
</dependency>
	</dependencies>
	
	<build>

		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin </artifactId>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

启动类:

    	//配置获取服务
    	try {
    		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[]{"application-spring.xml"});
    		applicationContext.start();
    		DemoServer server =  (DemoServer) applicationContext.getBean("demoService");
    		System.err.println("开始时间:"+System.currentTimeMillis());
    		for (int i = 0; i < 10; i++) {
    			server.sayHello("Client00"+i);
			}
    		System.err.println("结束时间:"+System.currentTimeMillis());
		} catch (Exception e) {
			e.printStackTrace();
		}

运行:

1.首先自己下载一个windows下可以运行的zookeeper:

运行服务提供程序:

运行服务消费者:

此时服务提供者端输出:

 

 

到此我们dubbo基于xml配置的服务注册和服务调用演示完成;

2.dubbo基于注解的方式配置服务注册和服务调用:

服务提供者的服务逻辑不变,消费者的逻辑也不变,要变的是xml中的一些注解配置如下:

服务提供者端:

DemoServer实现类加上类注解 @Service (com.alibaba.dubbo.config.annotation.Service)

package com.yangs.test.service.impl;

import java.io.Serializable;

import com.alibaba.dubbo.config.annotation.Service;
import com.yangs.test.service.DemoServer;

@Service
public class DemoServerImpl implements DemoServer,Serializable {

	@Override
	public String sayHello(String name) throws Exception {
		String str = "Hello app2 "+name +" now time :"+System.currentTimeMillis();
		System.err.println("有客户端调用这个服务了"+str);
		return str;
	}

}

application-spring.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="hello-world-app1"/>
	<dubbo:registry  protocol="zookeeper"  address="127.0.0.1:2181"  />
   	<dubbo:protocol name="dubbo" port="20881" />     
	<dubbo:service interface="com.yangs.test.service.DemoServer" ref="demoService" />
	<bean id="demoService" class="com.yangs.test.service.impl.DemoServerImpl" />  -->
	<!-- 以上是配置实现,下面使用注解实现 -->
	
	
	<dubbo:application name="hello-world-app1"/>
	<dubbo:registry  protocol="zookeeper"  address="127.0.0.1:2181"  />
   	<dubbo:protocol name="dubbo" port="20881" />     
   <!-- 	在配置文件中加上<dubbo:annotation>,是dubbo的扫描标签,它除了会扫描带有
   	'@Component'、'@Service'、'@Controller'注解的类,把它们注册成SpringBean之外,
   	它还会扫描带有”@Service” (dubbo的service标签)的接口实现类发布服务(必须有实现接口,不然或抛出BeanCreationException异常)
   	。同时在要发布服务的接口实现类上加上”@Service” (dubbo的service标签)。启动服务器,服务就发布成功了。 -->
	<dubbo:annotation package="com.yangs.test.service.impl"></dubbo:annotation> 
	
</beans>

 

服务调用端:

application-spring.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="hello-world-app"/>
	<
	<dubbo:registry  protocol="zookeeper"  address="127.0.0.1:2181"  />
   	<dubbo:protocol name="dubbo" port="20880" />     
	<dubbo:service interface="com.yangs.test.service.DemoServer" ref="demoService" />
	<bean id="demoService" class="com.yangs.test.service.impl.DemoServerImpl" /> -->
		<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样  
		192.9.145.19:2181,192.9.145.19:2182,192.9.145.19:2183-->
	<dubbo:application name="consumer-of-helloworld-app" />      
	 <!-- 使用multicast广播注册中心暴露发现服务地址 -->
	<dubbo:registry  protocol="zookeeper"  address="127.0.0.1:2181" />        
	 <!-- 生成远程服务代理,可以和本地bean一样使用demoService   通过配置实现 
	<dubbo:reference id="demoService" interface="com.yangs.test.service.DemoServer" /> 
	-->
	<!-- 再配置文件中加上<dubbo:annotation>,它会扫描所有注册bean的java类,发现带”@Reference”标签的属性,
		它会去寻找发布的provider是否有匹配的接口,有就自动注入。 -->
	 <dubbo:annotation package="com.yangs.test"></dubbo:annotation>
	
	
</beans>

启动类:

package com.yangs.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yangs.test.service.DemoServer;

@Component
public class App
{
	
	@Reference  //这个注解表示从服务注册地址获取这个类型的服务
	private static DemoServer demoServer;
	
    public static void main( String[] args )
    {
//    	//配置获取服务
//    	try {
//    		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[]{"application-spring.xml"});
//    		applicationContext.start();
//    		DemoServer server =  (DemoServer) applicationContext.getBean("demoService");
//    		System.err.println("开始时间:"+System.currentTimeMillis());
//    		for (int i = 0; i < 10; i++) {
//    			server.sayHello("Client00"+i);
//			}
//    		System.err.println("结束时间:"+System.currentTimeMillis());
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
    	
//    	//通过注解获取服务
    	try {
    		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[]{"application-spring.xml"});
    		applicationContext.start();
//    		demoServer =  (DemoServer) applicationContext.getBean("demoService");
    		System.err.println("开始时间:"+System.currentTimeMillis());
    		for (int i = 0; i < 10; i++) {
    			demoServer.sayHello("Client00"+i);
			}
    		System.err.println("结束时间:"+System.currentTimeMillis());
		} catch (Exception e) {
			e.printStackTrace();
		}
    	
    }
}

分别运行服务提供者和服务调用者,和使用xml配置方式一直

 

 

这里还随便测试一下dubbo的负载均衡,dubbo的负载均衡可自行查阅资料,dubbo默认是以random随机调用策略。

要测试负载均衡,我们需要启动改变dubbo协议暴露的服务端口 :

<dubbo:protocol name="dubbo" port="20881" />  保存,并启动服务提供者

<dubbo:protocol name="dubbo" port="20880" />  保存,并启动服务提供者

启动服务调用端,查看启动的两个服务端:

 

这里只是初步介绍dubbo,想要更进一步了解dubbo还请参考其他资料。

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值