dubbo配置及使用

现有两个工程一个是server层工程 一个是前端调用工程​

​​

service提供服务容器

pom文件中添加dubbo和zookeeper注册中心依赖

<!-- dubbo相关 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<exclusions>
				<exclusion>
					<groupId>org.springframework</groupId>
					<artifactId>spring</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.jboss.netty</groupId>
					<artifactId>netty</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
		</dependency>
		<dependency>
			<groupId>com.github.sgroschupf</groupId>
			<artifactId>zkclient</artifactId>
		</dependency>

 

service工程spring配置结构如下:

其中service.xml配置如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" 
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
	http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
	<context:component-scan base-package="cn.e3mall.service"/>
	
	<!-- 使用dubbo发布服务 -->
	<!-- 提供方应用信息,用于计算依赖关系 -->
	<dubbo:application name="e3-manager" />
	<dubbo:registry protocol="zookeeper"
		address="192.168.3.200:2181"/>
	<!-- 用dubbo协议在20880端口暴露服务 -->
	<dubbo:protocol name="dubbo" port="20880" />
	<!-- 声明需要暴露的服务接口 -->
	<dubbo:service interface="cn.e3mall.service.ItemService" ref="itemServiceImpl" />
	
</beans>

其中配置文件中添加dubbo的约束

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd

(其中本地要配置dubbo.xsd约束,不然会报错)

1.

<dubbo:application name="e3-manager" />

指定工程名称

2.

<dubbo:registry protocol="zookeeper"  address="192.168.3.200:2181"/>

使用zookeeper注册服务,地址为192.168.3.200:2181,如果有多个服务集群可以如下配置

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

3.

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

 用dubbo协议在20880端口暴露服务​​​​​​​

4.

<dubbo:service interface="cn.e3mall.service.ItemService" ref="itemServiceImpl" />

声明服务对外接口,其中interface为接口全限定名,ref为容器中实现类的引用

 

web工程中spring配置如下

springmvc.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:p="http://www.springframework.org/schema/p"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
	<context:component-scan base-package="cn.e3mall.controller"/>
	
	<mvc:annotation-driven />
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>
	<!-- 使用dubbo发布服务 -->
	<!-- 提供方应用信息,用于计算依赖关系 -->
	<dubbo:application name="e3-manager-web" />
	<dubbo:registry protocol="zookeeper"
		address="192.168.3.200:2181"/>
	<!-- 声明需要暴露的服务接口 -->
	<dubbo:reference interface="cn.e3mall.service.ItemService" id="itemService" />
</beans>

其中配置文件中添加dubbo的约束

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd

(其中本地要配置dubbo.xsd约束,不然会报错)

1.

<dubbo:application name="e3-manager-web" />

指定工程名称

2.

<dubbo:registry protocol="zookeeper"  address="192.168.3.200:2181"/>

使用zookeeper注册服务,地址为192.168.3.200:2181

3.

<dubbo:reference interface="cn.e3mall.service.ItemService" id="itemService" />

声明要调用的接口,interface为接口全限定名,id为容器中接口的引用

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值