首页 技术 正文
技术 2022年11月14日
0 收藏 900 点赞 4,588 浏览 4021 个字

一、背景

dubbo是个什么?

首先要说的是,网上有很多高大上的回答,可自行百度,这里只说一些非常狭隘的东西:

dubbo是一个分布式服务框架,我们一般用它进行远程方法调用。(分布式、远程方法调用下面有注释)

ok,狭隘的东西回答完毕(下面注释也是狭隘的)~~~

分布式:将一个功能分成多个小模块,交由不同服务器处理,整合得到最终结果。

远程方法调用:RMI,可像本地调用一样调用其它系统的功能

二、适用场景

供应商各子系统间的相互调用

三、服务端配置

1、实现提供远程方法调用的类

接口类

public interface ICompanyService {    /**
* 检测公司是否存在:根据公司名称检测CRM系统中公司是否存在
*/
boolean checkCompanyExist(String companyName) throws Exception;}

实现类

public class CompanyServiceImpl implements ICompanyService {    /**
* 检测公司是否存在:根据公司名称检测CRM系统中公司是否存在
*/
@Override
public boolean checkCompanyExist(String companyName) throws Exception {
return false;
}}

2、在pom.xml增加dubbo所需jar包

<!-- Dubbo Jar包引入 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.4.9</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</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.4.5</version>
<exclusions>
<exclusion>
<artifactId>jmxtools</artifactId>
<groupId>com.sun.jdmk</groupId>
</exclusion>
<exclusion>
<artifactId>jmxri</artifactId>
<groupId>com.sun.jmx</groupId>
</exclusion>
<exclusion>
<artifactId>jms</artifactId>
<groupId>javax.jms</groupId>
</exclusion>
</exclusions>
</dependency>

3、增加dubbo配置文件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: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服务器端配置文件 -->
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="dubbo-crm-service" />
<!-- 使用zookeeper注册中心,暴露服务地址 -->
<dubbo:registry protocol="zookeeper" address="${zookeeper.url}" />
<!-- 用dubbo协议在20883端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20883" /> <!-- 声明需要暴露的服务接口及接口实现 -->
<!-- 公司自助注册接口 ,用户验证、权限接口 -->
<dubbo:service interface="com.glodon.crm.dubbo.service.ICompanyService"
ref="crmCompanyService" />
<bean id="crmCompanyService" class="com.glodon.crm.dubbo.service.impl.CompanyServiceImpl" /></beans>

4、在web.xml中加载dubbo的配置文件

  <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml,classpath*:dubbo.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

5、导出远程方法调用的接口为jar包

eclipse中选中接口文件,右键 – Export – 选中java下的JAR file – 选择保存路径,输入jar包名称,点击Finish

四、客户端配置

1、在pom.xml增加dubbo所需jar包,和服务端提供的接口jar包(略)

2、增加dubbo配置文件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: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服务器端配置文件 -->
<!-- 消费方应用信息,用于计算依赖关系 -->
<dubbo:application name="gcxx_consumer" />
<!-- 使用zookeeper注册中心,获取服务地址 -->
<dubbo:registry protocol="zookeeper" address="${zookeeper.url}" />
<!-- dubbo端口 -->
<dubbo:protocol name="dubbo" port="20880" /><!-- 生成远程服务代理,可以像使用本地bean一样使用companyService -->
<dubbo:reference id="companyService" interface="com.glodon.crm.dubbo.service.ICompanyService" check="false"/>
</beans>

3、在web.xml中加载dubbo的配置文件(略)

4、在程序中调用远程方法,要注意spring注入的远程接口类的对象名要和上方dubbo:reference的id相同

public class Test {
@Autowired
ICompanyService companyService; public void test(){
try {
companyService.checkCompanyExist("test");
} catch (Exception e) {
e.printStackTrace();
}
}}

五、直连服务提供方

开发时会碰到开发期间多个服务提供方中只有一个是自己要调的,而上述配置中dubbo的调用近乎随机,不能调到自己想调的服务方

这时,调整dubbo配置文件为ip直连即可

    <!-- 生成远程服务代理,可以像使用本地bean一样使用companyService -->
<dubbo:reference id="companyService" interface="com.glodon.crm.dubbo.service.ICompanyService" check="false" url="172.16.231.230:20883"/>
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295