您现在的位置是:首页 > 后台技术 > 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 作为容器管理对象
服务消费者
将提供者中的实体类和接口文件导出成一个jar包
定义服务接口 (该接口需单独打包,在服务提供方和消费方共享)
在服务提供方实现接口(对服务消费方隐藏实现)
用 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
定义测试类访问
服务消费者开发步骤:
通过 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
先启动服务提供者,再执行服务消费者
到此,表明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
很赞哦! ()
上一篇:第三章 dubbo框架(图文)
相关文章
随机图文
-
第二章 分多个项目(图文)
将上节的一个项目分成两个项目,一个项目提供服务,一个项目消费服务 -
第四章 使用dubbo的第一个项目(图文)
点对点的直连项目:消费者直接访问服务提供者,没有注册中心。消费者必须指定服务 提供者的访问地址(url)。 -
第三章 dubbo框架(图文)
Dubbo 是一个框架 Dubbo 是一个分布式服务框架,致力于提供高性能和透明化的 RPC 远程服务调用方案、服 务治理方案。 -
第五章 使用接口作为独立项目(图文)
公司或者企业有很多的服务,这些服务有不同的部门,不同的人员管理,维护。例如公 司做生活服务类业务的,提供类似 58 同城的业务。其中公共服务部门负责提供天气资讯和 影视资讯两个内容。A 小组两个人负责天气资讯;B 小组三个人负责影视资讯。现在需要在 公司的网站同时提供两种资讯。你作为网站开发人员需要使用 A 和 B 两个小组不同服务内 容。使用 A 组,B 组的两个服务提供者接口。