分布式RPC系统框架Dubbo-07服务分组

        服务分组与多版本控制的使用方式几乎是相同的,只要将version替换为group即可,但使用目的不同,使用版本控制的目的是为了升级,将原有老版本替换掉,将来不再提供老版本的服务,所以不同版本间不能出现相互调用,而分组的目的则不同,其也是针对相同接口,给出了多种实现类,但不同的是,这些不同实现并没有谁替换掉谁的意思,是针对不同需求,或针对不同功能模块所给出的不同实现,这些实现所提供的服务是并存的,所以它们间可以出现相互调用关系,例如,对于支付服务的实现,可以有微信支付实现与支付宝支付实现等。

1 创建提供者provider-group

step1 创建工程

       提供者工程provider-group,引入依赖。

<properties> 
	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
	<maven.compiler.source>1.8</maven.compiler.source> 
	<maven.compiler.target>1.8</maven.compiler.target> 
	<!-- 自定义版本号 --> 
	<spring-version>4.3.16.RELEASE</spring-version> 
  </properties> 
  <dependencies> 
	<!--业务接口工程依赖--> 
	<dependency> 
		<groupId>com.zxy</groupId>
  		<artifactId>0-api</artifactId>
  		<version>0.0.1-SNAPSHOT</version> 
	</dependency> 
	<!-- dubbo依赖 --> 
	<dependency>
	    <groupId>org.apache.dubbo</groupId>
	    <artifactId>dubbo</artifactId>
	    <version>2.7.0</version>
	</dependency>

	<!-- Spring依赖 --> 
	<dependency> 
		<groupId>org.springframework</groupId> 
		<artifactId>spring-beans</artifactId> 
		<version>${spring-version}</version> 
	</dependency> 
	<dependency> 
		<groupId>org.springframework</groupId> 
		<artifactId>spring-core</artifactId> 
		<version>${spring-version}</version> 
	</dependency> 
	<dependency>
		<groupId>org.springframework</groupId> 
		<artifactId>spring-context</artifactId> 
		<version>${spring-version}</version> 
	</dependency> 
	<dependency> 
		<groupId>org.springframework</groupId> 
		<artifactId>spring-expression</artifactId> 
		<version>${spring-version}</version> 
	</dependency> 
	<dependency> 
		<groupId>org.springframework</groupId> 
		<artifactId>spring-aop</artifactId> 
		<version>${spring-version}</version> 
	</dependency> 
	<dependency> 
		<groupId>org.springframework</groupId> 
		<artifactId>spring-aspects</artifactId> 
		<version>${spring-version}</version> 
	</dependency> 
	<dependency> 
		<groupId>org.springframework</groupId> 
		<artifactId>spring-tx</artifactId> 
		<version>${spring-version}</version> 
	</dependency> 
	<dependency> 
		<groupId>org.springframework</groupId> 
		<artifactId>spring-jdbc</artifactId> 
		<version>${spring-version}</version> 
	</dependency> 
	
	<!-- zk客户端依赖:curator --> 
	<dependency> 
		<groupId>org.apache.curator</groupId> 
		<artifactId>curator-recipes</artifactId> 
		<version>2.13.0</version> 
	</dependency> <dependency> 
		<groupId>org.apache.curator</groupId> 
		<artifactId>curator-framework</artifactId> 
		<version>2.13.0</version> 
	</dependency>
	
	<!-- commons-logging依赖 --> 
	<dependency> 
		<groupId>commons-logging</groupId> 
		<artifactId>commons-logging</artifactId> 
		<version>1.2</version> 
	</dependency> 	
</dependencies>

step2 定义两个接口实现类

      定义两个实现类,实现api工程中的接口 https://blog.csdn.net/zxylwj/article/details/108540657;

public class AlipayServiceImpl implements SomeService{

	@Override
	public String helloDubbo(String msg) {

		System.out.println("使用【支付宝】");
		return "Alipay";
	}

}
public class WeixinServiceImpl implements SomeService{

	@Override
	public String helloDubbo(String msg) {

		System.out.println("使用【微信支付】");
		return "WeixinServiceImpl";
	}

}

step3 修改配置文件

<!-- 添加 DUBBO SCHEMA -->
<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">
        
	<!-- 指定当前工程在Monitor监控中心显示的名称,一般与工程名相同 -->
	<dubbo:application name="provider-group" />
	
	<!-- 指定注册中心 -->
	<dubbo:registry address="zookeeper://192.168.85.129:2181" />
	
	<!-- 注册业务实现类,真正的服务提供者 -->
	<bean id="weixinService" class="com.zxy.service.WeixinServiceImpl"/>
	<bean id="alipayService" class="com.zxy.service.AlipayServiceImpl"/>
	
	<!-- 暴露服务:接口即服务名称,指向真正的服务提供者 -->
	<dubbo:service interface="com.zxy.service.SomeService" 
	               ref="weixinService" group="pay.weixin"/>
	               
	<dubbo:service interface="com.zxy.service.SomeService" 
	               ref="alipayService" group="pay.alipay"/>
</beans>

2 创建消费者consumer-group

step1 创建工程

        创建工程consumer-group,导入依赖,与provider相同。

step2 修改配置文件

<!-- 添加 DUBBO SCHEMA -->
<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-group">
		<dubbo:parameter key="qos.port" value="33333"/>
	</dubbo:application>
	
	<!-- 声明zookeeper注册中,单机zk -->
	<dubbo:registry address="zookeeper://192.168.85.129:2181" />	
	<!-- 指定调用微信服务 -->
	<dubbo:reference id="weixin" check="false" group="pay.weixin"
					 interface="com.zxy.service.SomeService"/>
	
	<!-- 指定调用支付宝服务 -->
	<dubbo:reference id="alipay" check="false" group="pay.alipay"
					 interface="com.zxy.service.SomeService"/>
</beans>

step3 修改消费者类

public class ConsumerRun {

	
	public static void main(String[] args) throws IOException {
		// 创建Spring容器
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-consumer.xml");

		//使用微信支付
		SomeService weixinService = (SomeService) applicationContext.getBean("weixin");
		String weixin = weixinService.helloDubbo("pay...");
		System.out.println(weixin);
		
		//使用支付宝支付
		SomeService alipayService = (SomeService) applicationContext.getBean("alipay");
		String alipay = alipayService.helloDubbo("pay...");
		System.out.println(alipay);
		
	}

}

运行:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值