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

第四章 使用dubbo的第一个项目(图文)

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

简介点对点的直连项目:消费者直接访问服务提供者,没有注册中心。消费者必须指定服务
提供者的访问地址(url)。

服务提供者
服务提供者开发步骤:
定义服务接口 (该接口需单独打包,在服务提供方和消费方共享)
在服务提供方实现接口(对服务消费方隐藏实现)
用 Spring 配置声明暴露服务
加载 Spring 配置(创建 bean)
项目结构:

新建 web 项目,命名:dubbo-provider
导入 jar

dubbo.jar: Dubbo 框架的实现
javaassist-3.15.0-GA.jar:字节码生成 jar
netty-3.2.5.Final.jar:网络传输
spring-*.jar: Dubbo 是基于 spring 的。配置 bean。
定义表示天气信息的对象 Weather

定义服务的接口(面向接口编程)

定义天气接口的实现类

编写 Spring 配置文件,Spring 作为容器管理对象
<?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.WeatherService" 
                         protocol="dubbo" 
                         ref="weatherService" 
                         registry="N/A"/>
       
       <!-- 注册接口的实现类  -->
       <bean id="weatherService" class="com.xinchen.service.impl.WeatherServiceImpl"></bean>
       
</beans>
修改 web.xml 文件,web 应用中使用 Spring
<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>
至此,服务提供者的功能实现完成。


服务消费者
将提供者中的实体类和接口文件导出成一个jar包
服务消费者项目可以是 j2se, j2ee 等项目类型,使用服务。
服务消费者开发步骤:
通过 Spring 配置引用远程服务
加载 Spring 配置,并调用远程服务:(也可以使用 IoC 注入)
项目结构

新建 java project ,命名  dubbo-consumer
导入 jar 服务提供者的jar包中取出webmvc 和web的包
新建配置文件 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="remoteWeatherService" 
                           interface="com.xinchen.service.WeatherService" 
                           url="dubbo://localhost:20880"/>
</beans>
定义测试类访问
package test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.xinchen.service.WeatherService;


public class A {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("dubbo-consumer.xml");
        WeatherService weather = (WeatherService)context.getBean("remoteWeatherService");
        System.out.println(weather.queryWeather("2222"));

    }
}
测试访问远程服务
先启动服务提供者,再执行服务消费者

到此,表明dubbo方式的服务提供者和服务消费者都完成
注入方式使用远程服务对象
新建使用远程服务对象的 Service    InvokeService
package test;

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

public class InvokeService {
    private WeatherService weatherService;
    public void printService(){
        Weather weather = weatherService.queryWeather("111");
        System.out.println(weather);
    }
    public void setWeatherService(WeatherService weatherService) {
        this.weatherService = weatherService;
    }
}
修改 dubbo-consume.xml 声明 ShowWeatherService 对象,注入服务提供者对象
<!-- 用注入的方式使用远程对象 -->                    
<bean id="invokeService" class="test.InvokeService">
         <property name="weatherService" ref="remoteWeatherService"></property>        
</bean>
测试 运行测试类 
package 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();
    }
}

 

Tags:dubbo

很赞哦! ()

文章评论

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

    用户名:

    验证码:

本站推荐

站点信息

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