首页 > 其他分享 >Dubbo -注解方式和Api方式

Dubbo -注解方式和Api方式

时间:2022-11-30 14:32:47浏览次数:39  
标签:Dubbo www http dubbo springframework Api org 注解 schema


注解方式

服务端

<?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:context="http://www.springframework.org/schema/context"
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://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

<context:component-scan base-package="com.enjoy.dao"/>

<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="storeServer_annotation"/>

<!-- 使用zookeeper注册中心暴露服务地址 -->
<!--<dubbo:registry address="zookeeper://127.0.0.1:2181"/>-->

<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="rmi" port="20880"/>

<dubbo:annotation package="com.enjoy.service" />

</beans>

注意,@Service使用import com.alibaba.dubbo.config.annotation.Service;

客户端

<?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:context="http://www.springframework.org/schema/context"
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://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="enjoyStore_annotation"/>

<!-- 使用zookeeper注册中心暴露服务地址 -->
<!--<dubbo:registry address="zookeeper://127.0.0.1:2181" check="false"/>-->

<dubbo:annotation package="com.enjoy.controller" />

</beans>

注入使用:

 @Reference
 private UserService userService;

Api方式

provider

public static void initDubbo() throws IOException {
// 当前应用配置
ApplicationConfig application = new ApplicationConfig();
application.setName("StoreServerApi");

// 连接注册中心配置
RegistryConfig registry = new RegistryConfig();
registry.setProtocol("zookeeper");
registry.setAddress("192.168.46.133:2181");

// 服务提供者协议配置
ProtocolConfig protocol = new ProtocolConfig();
protocol.setName("rmi");
protocol.setPort(21880);
protocol.setThreads(100);

// 注意:ServiceConfig为重对象,内部封装了与注册中心的连接,以及开启服务端口
// 服务提供者暴露服务配置
// 此实例很重,封装了与注册中心的连接,请自行缓存,否则可能造成内存和连接泄漏
ServiceConfig<VipUserService> service = new ServiceConfig<>();

service.setApplication(application);
service.setRegistry(registry); // 多个注册中心可以用setRegistries()
service.setProtocol(protocol); // 多个协议可以用setProtocols()
service.setInterface(VipUserService.class);
service.setRef(new VipUserServiceImpl());

// 暴露及注册服务
service.export();

System.out.println("服务已经启动");
System.in.read();

}

consumer

public class StoreConsumer {
public static void main(String[] args) throws IOException {
// 当前应用配置
ApplicationConfig application = new ApplicationConfig();
application.setName("StoreServerClientApi");

// 连接注册中心配置
RegistryConfig registry = new RegistryConfig();
registry.setProtocol("zookeeper");
registry.setAddress("192.168.46.133:2181");

// 服务提供者协议配置
ProtocolConfig protocol = new ProtocolConfig();
protocol.setName("dubbo");
protocol.setPort(20882);
protocol.setThreads(100);

// 注意:ReferenceConfig为重对象,内部封装了与注册中心的连接,以及与服务提供方的连接
// 引用远程服务
ReferenceConfig<VipUserService> reference = new ReferenceConfig<>(); // 此实例很重,封装了与注册中心的连接以及与提供者的连接,请自行缓存,否则可能造成内存和连接泄漏
reference.setApplication(application);
reference.setRegistry(registry); // 多个注册中心可以用setRegistries()
reference.setInterface(VipUserService.class);

// 和本地bean一样使用xxxService
VipUserService vipUserService = reference.get(); // 注意:此代理对象内部封装了所有通讯细节,对象较重,请缓存复用
String ret = vipUserService.getVipDetail("123");
reference.destroy();
System.out.println(ret);



}

}

标签有个继承关系,

------下层会继承上层属性配置

------ 消费方,会继承服务方属性配置:服务只提供者自己知道,配什么属性最合适

标签:Dubbo,www,http,dubbo,springframework,Api,org,注解,schema
From: https://blog.51cto.com/u_14906615/5899439

相关文章

  • Servlet3.1规范翻译 - 注解和可插拔性
    ​​https://github.com/javahongxi​​本文转载自kaitao.hongxi.org注解和可插拔性本章讨论Servlet3.0规范定义的注解和使web应用内使用的框架和库能够可插拔的增强。8.1......
  • API网关【gateway 】- 1
    最近在公司进行API网关重写,公司内采用serverMesh进行服务注册,调用,这里结合之前学习对API网关服务进行简单的总结与分析。网关的单节点场景:网关的多节点场景:这里的多节点是根......
  • API网关【gateway 】- 2
    最近在公司进行API网关重写,公司内采用serverMesh进行服务注册,调用,这里结合之前学习对API网关服务进行简单的总结与分析。由于采用了大量的nginx相关的东西,所以在此记录一下:......
  • UPEK API 代码
     UPEKAPI代码 usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text......
  • Spring Boot + @Async 太好用了,助你大大提升 API 并发能力!
    来源:https://developer.aliyun.com/article/694020异步调用几乎是处理高并发Web应用性能问题的万金油,那么什么是“异步调用”?“异步调用”对应的是“同步调用”,同步调用......
  • aop注解
    注解aop开发步骤1 使用@Aspect标注切面类2 使用@通知注解标注通知方法@Component("myAspect")@Aspect//标注当前MyAspect是一个切面类publicclassMyAs......
  • .net core .net6 webapi 连接mysql 8
    1.表结构:CREATETABLE`table2`(`id`BIGINTNOTNULLAUTO_INCREMENT,`myname`varchar(255)NOTNULL,`create_time`DATETIMENOTNULL,PRIMARYKEY(`......
  • @Repository相关注解
    @Repository@Repository注解修饰哪个类,则表明这个类具有对对象进行CRUD(增删改查)的功能,而且@Repository是@Component注解的一个派生品,所以被@Repository注解的类可以自动的......
  • EventTarget.addEventListener() - Web API 接口参考
    EventTarget.addEventListener()-WebAPI接口参考  https://developer.mozilla.org/zh-CN/docs/Web/API/EventTarget/addEventListener#%E8%AF%AD%E6%B3%95  <......
  • 三大实例带你搞定 Prometheus API 使用
    作为一位优秀的技术人员,往往能通过对数据的最大化利用来产生更多价值。而Prometheus的监控数据则是可以为我们所用的重要数据,它并不只能用于日常的监控和告警使用,也可以用......