应用——dubbo的基本使用

来源:互联网 发布:.date域名 编辑:程序博客网 时间:2024/05/17 09:15

一、背景

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"/> 
0 0
原创粉丝点击