Dubbo搭建

在这里插入图片描述

1.Dubbo架构图

1.1 Provider :提供者,服务发布方.

1.2 Consumer:消费者, 调用服务方

1.3 Container:Dubbo容器.依赖于Spring容器.

1.4 Registry: 注册中心.当Container启动时把所有可以提供的服务列表上Registry中进行注册.

1.4.1 作用:告诉Consumer提供了什么服务和服务方在哪里.

1.5 Monitor:监听器

1.6 虚线都是异步访问,实线都是同步访问

1.7 蓝色虚线:在启动时完成的功能

1.8 红色虚线(实线)都是程序运行过程中执行的功能

1.9 所有的角色都是可以在单独的服务器上.所以必须遵守特定的协议.

项目结构图
在这里插入图片描述

2,注册中心安装

确认安装jdk,下载并解压zookeeper

/conf/zoo.cfg 可修改端口号

/bin/zkServer.cmd window运行
/bin/zkServer.sh linx运行

启动管理界面项目
建项目war包解压到tomcat/webapps/目录下,
配置/WEB-INF/dubbo.properties
dubbo.registry.address=zookeeper://10.20.90.149:2181
zookeeper所在机器ip即使是本地也不要用127.0.0.1或者localhost。
http://127.0.0.1:8080/dubbo-admin
在这里插入图片描述

3,Provider提供者搭建

1.新建Maven Project, 里面只有接口(dubbo-service)
1.1 为什么这么做?
RPC框架,不希望Consumer知道具体实现.如果实现类和接口在同一个项目中,Consumer依赖这个项目时,就会知道实现类具体实现.
2.新建Maven Project, 写接口的实现类(dubbo-service-impl)
3.在duboo-service-impl中配置pom.xml
pom.xml配置

<dependencies>
		<dependency>
			<groupId>com.bjsxt</groupId>
			<artifactId>dubbo-service</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<version>2.5.3</version>
			<exclusions>
				<exclusion>
					<artifactId>spring</artifactId>
					<groupId>org.springframework</groupId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>4.1.6.RELEASE</version>
		</dependency>
		<!-- 访问zookeeper的客户端jar -->
		<dependency>
			<groupId>com.101tec</groupId>
			<artifactId>zkclient</artifactId>
			<version>0.10</version>
		</dependency>
	</dependencies>

4.新建实现类,并实现接口方法.
5.在WETA-INF/spring/下新建配置文件applicationContext-dubbo.xml,并配置

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:context="http://www.springframework.org/schema/context"
	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://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context.xsd
		http://code.alibabatech.com/schema/dubbo 
		http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
	<!-- 给当前Provider自定义个名字 -->
	<dubbo:application name="dubbo-service"/>
	<!-- 配置注册中心  -->
	<dubbo:registry address="192.168.139.130:2181" protocol="zookeeper"></dubbo:registry>
	<!-- 配置端口 -->
	<dubbo:protocol name="dubbo" port="20888"></dubbo:protocol>
	<!-- 注册功能 -->
	<dubbo:service interface="com.bjsxt.service.DemoService" ref="demoServiceImpl"></dubbo:service>
	<bean id="demoServiceImpl" class="com.bjsxt.service.impl.DemoServiceImpl"></bean>
</beans>

6.启动类

import com.alibaba.dubbo.container.Main;
public class Test {
	public static void main(String[] args) {
		Main.main(args);
	}
}
4.Consumer搭建

1.在pom.xml中按照普通web项目添加依赖,追加dubbo相关的3个依赖(service接口,dubbo.jar,zkClient)
2.web.xml中修改applicationContext-*.xml

<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>

3.spring配置文件命名为applicationContext-spring.xml,配置dubbo的配置文件applicationContext-dubbo.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:context="http://www.springframework.org/schema/context"
	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://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context.xsd
		http://code.alibabatech.com/schema/dubbo 
		http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
	<!-- 给当前Provider自定义个名字 -->
	<dubbo:application name="dubbo-consumer"/>
	<!-- 配置注册中心  -->
	<dubbo:registry address="10.20.90.149:2181" protocol="zookeeper"></dubbo:registry>
	<!-- 配置注解扫描 -->
	<dubbo:annotation package="com.bjsxt.service.impl"/>
	
</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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd"
	default-autowire="byName">
	<!-- 注解扫描 -->
	<context:component-scan base-package="com.bjsxt.service.impl"></context:component-scan>
</beans>

4.除了ServiceImpl中引用Provider中接口对象改变,其他代码都一样.

@Service
public class TestServiceImpl implements TestService {
//	@Resource
//	private xxMapper xxxMapper;
	@Reference
	private DemoService demoService;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

占星安啦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值