您现在的位置是:首页 > 后台技术 > dubbodubbo

第五章 使用接口作为独立项目(图文)

第十三双眼睛2021-06-19【dubbo】人已围观

简介公司或者企业有很多的服务,这些服务有不同的部门,不同的人员管理,维护。例如公
司做生活服务类业务的,提供类似 58 同城的业务。其中公共服务部门负责提供天气资讯和
影视资讯两个内容。A 小组两个人负责天气资讯;B 小组三个人负责影视资讯。现在需要在
公司的网站同时提供两种资讯。你作为网站开发人员需要使用 A 和 B 两个小组不同服务内
容。使用 A 组,B 组的两个服务提供者接口。

公司使用 Dubbo 管理服务,A 组,B 组分别各自服务的接口的 jar 包。比如 A-Weather.jar ,B-Movie.jar . 网站的开发人员需要同时维护两个 jar。任何一个有改动,都需要做调整代码。
现在只要使用公共服务部门提供的一个服务接口 jar 包就可以了。
服务提供者接口定义
新建 Java Project 项目 dubbo-interface

新建天气信息的数据类 Weatcher

新建天气服务接口

新建影视信息的数据类 Movie

新建影视服务接口

导出包含所有接口的 jar
服务提供者接口实现
新建 web 工程 05dubbo-interface-provider
项目结构:

导入 dubbo,spring,服务接口的 jar
新建实现 WeatherService 接口的实现类
package com.xinchen.serviceimpl;

import com.xinchen.entity.Weather;
import com.xinchen.service.WeatherService;

public class WeatherServiceImpl implements WeatherService {
    @Override
    public Weather queryWeather(String code) {
        Weather weather = new Weather();
        if("111".equals(code)){
            weather.setName("北京");
            weather.setSd("15%");
            weather.setTemp("20");
            weather.setDesc("北京天气信息");
        }else if("222".equals(code)){
            weather.setName("上海");
            weather.setSd("18%");
            weather.setTemp("30");
            weather.setDesc("上海天气信息");
        }else{
            weather.setName("天津");
            weather.setSd("18%");
            weather.setTemp("26");
            weather.setDesc("天津天气信息");
        }
        return weather;
    }
}
新建实现 MovieService 接口的实现类
package com.xinchen.serviceimpl;

import java.util.ArrayList;
import java.util.List;
import com.xinchen.entity.Movie;
import com.xinchen.service.MovieService;
public class MovieServiceImpl implements MovieService{
    @Override
    public List<Movie> getMovies() {
        List<Movie> movies = new ArrayList<Movie>();
        Movie movie1 = new Movie();
        movie1.setName("卧虎藏龙");
        movie1.setTime("90");
        movie1.setActor("章子怡");
        movies.add(movie1);
        
        Movie movie2 = new Movie();
        movie2.setName("天蚕变");
        movie2.setTime("120");
        movie2.setActor("徐少强");
        movies.add(movie2);
        return movies;
    }
}
新建 spring 配置文件 dubbo-provider.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-provider"/>
       <!-- 
               暴露dubbo的服务
               interface:接口的全限定名
               protocal:访问这个服务使用的协议 使用dubbo
               ref:接口实现类的id
               registry:注册中心 不使用注册中心的时候填写N/A
        -->
       <dubbo:service interface="com.xinchen.service.MovieService" 
                         protocol="dubbo" 
                         ref="movieService" 
                         registry="N/A"/>
       <!-- 注册接口的实现类  -->
       <bean id="movieService" class="com.xinchen.serviceimpl.MovieServiceImpl"></bean>
       
       
       <dubbo:service interface="com.xinchen.service.WeatherService" 
                         protocol="dubbo" 
                         ref="weatherService" 
                         registry="N/A"/>
       <!-- 注册接口的实现类  -->
       <bean id="weatherService" class="com.xinchen.serviceimpl.WeatherServiceImpl"></bean>
       
</beans>
修改 web.xml,增加 ContextLoaderListener 监听器
<context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:dubbo-provider.xml</param-value>
</context-param>
  
<listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
服务消费者
新建 Java Project  05dubbo-interface-consumer
项目结构:

导入 dubbo,spring,服务提供者接口 jar
新建 spring 配置文件 dubbo-consume.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:application name="dubbo-consumer"/>
       <!-- 
               声明要使用的服务
               id:表示代理对象
               interface:接口
               url:不使用注册中心时的访问地址
        -->
       <dubbo:reference id="movieService" 
                           interface="com.xinchen.service.MovieService" 
                           url="dubbo://localhost:20880"/>
                           
       <dubbo:reference id="weatherService" 
                           interface="com.xinchen.service.WeatherService" 
                           url="dubbo://localhost:20880"/>
                           
       <!-- 用注入的方式使用远程对象 -->                    
       <bean id="invokeService" class="com.test.InvokeService">
               <property name="weatherService" ref="weatherService"></property>        
               <property name="movieService" ref="movieService"></property>        
       </bean>
                           
</beans>
定义访问服务的测试类
package com.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class B {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("dubbo-consumer.xml");
        InvokeService invokeService = (InvokeService)context.getBean("invokeService");
        invokeService.printService();
    }
}
请求结果:
至此,接口作为单独项目已经完成

 
dubbo 常用标签
Dubbo 中常用标签。分为三个类别:公用标签,服务提供者标签,服务消费者标签
公用标签
<dubbo:application/> 和 <dubbo:registry/>
配置应用信息<dubbo:application name=”服务的名称”/>
配置注册中心<dubbo:registry address=”ip:port” protocol=”协议”/>
服务提供者标签
配置访问服务提供者的协议信息<dubbo:protocol name=”dubbo” port=”20880”/>
配置暴露的服务<dubbo:service interface=”服务接口名” ref=”服务实现对象 bean”>

服务消费者
配置服务消费者引用远程服务
<dubbo:reference id=”服务引用 bean 的 id” interface=”服务接口名”/>
常用配置项目
check , retries 等
关闭检查 check
Dubbo 缺省会在启动时检查依赖的服务是否可用,不可用时会抛出异常,阻止 Spring
初始化完成,以便上线时,能及早发现问题,默认 check=true。通过 check="false"关闭检查,
比如,测试时,有些服务不关心,或者出现了循环依赖,必须有一方先启动。
例 1:关闭某个服务的启动时检查
<dubbo:reference interface="com.foo.BarService" check="false" />
例 2:关闭注册中心启动时检查
<dubbo:registry check="false" />
默认启动服务时检查注册中心存在并已运行。不启动报错。
请求重试 retries
远程服务调用重试次数,不包括第一次调用,默认是 2 次。 加上第一次共 3 次。
<dubbo:reference retries=”5”>

Tags:dubbo

很赞哦! ()

文章评论

    共有条评论来说两句吧...

    用户名:

    验证码:

本站推荐

站点信息

  • 网站名称:JavaStudy
  • 建站时间:2019-1-14
  • 网站程序:帝国CMS7.5
  • 文章统计242篇文章
  • 标签管理标签云
  • 统计数据百度统计
  • 微信公众号:扫描二维码,关注我们