• RMI远程方法调用


    RMI远程方法调用:适用于 客户端 调用 服务器 内的方法;(Kotlin 语言编写)

    如果业务为二个服务器之间的通信,还是得用消息队列的形式,因为RMI 不适合 双向 调用

    下面介绍RMI 的使用方法:

    1,服务器端代码:

    定义接口(此接口放入服务端和客户端公用的模块 或者打成jar给服务器/客户端使用)

     1 interface RmiService {
     2 
     3     /**
     4      * 订单发货
     5      * @param saleItemId  BTB订单项 itemId
     6      * @param shipmentName  物流公司名称
     7      * @param shipmentNumber  物流单号
     8      *
     9      */
    10     fun shippedSale(saleItemId: Long, shipmentName: String, shipmentNumber: String): RestResponse<Any>
    11 }
    View Code

    定义实现类

     1 @Service
     2 class RmiServiceImpl : RmiService {
     3 
     4 
     5     @Autowired
     6     private lateinit var supplierSaleService: SupplierSaleService
     7 
     8 
     9     /**
    10      * 订单发货
    11      * @param saleItemId  BTB订单项 itemId
    12      * @param shipmentName  物流公司名称
    13      * @param shipmentNumber  物流单号
    14      *
    15      */
    16     override fun shippedSale(saleItemId: Long, shipmentName: String, shipmentNumber: String): RestResponse<Any> {
    17         return supplierSaleService.shippedSale(saleItemId, shipmentName, shipmentNumber)
    18     }
    19 
    20 
    21 }
    View Code

    注册RMI服务

     1 @Component
     2 class RegistryExporter {
     3 
     4     @Autowired
     5     private lateinit var rmiService: RmiService
     6 
     7     @Bean
     8     fun rmiServiceExporter(): RmiServiceExporter {
     9         val exporter = RmiServiceExporter()
    10         exporter.service = rmiService
    11         exporter.serviceInterface = RmiService::class.java
    12         exporter.setServiceName("SERVER_NAME")
    13         exporter.setRegistryPort(RmiServiceConstants.REGISTRY_PORT)
    14         return exporter
    15     }
    16 
    17 }
    View Code

    2.客户端代码:

    通过RMI代理实例化接口(即 服务器定义的接口)

     1  @Bean("rmiService")
     2     fun rmiService(): RmiProxyFactoryBean {
     3         //创建rmi的代理
     4         val rmiProxyFactoryBean = RmiProxyFactoryBean()
     5         //访问rmi,ip,端口,和rmi名字
     6         rmiProxyFactoryBean.serviceUrl = "rmi://${RmiServiceConstants.SERVER_HOST}:${RmiServiceConstants.REGISTRY_PORT}/" + RmiServiceConstants.SERVER_NAME
     7         //设置代理类代理的接口
     8         rmiProxyFactoryBean.serviceInterface = RmiService::class.java
     9         return rmiProxyFactoryBean
    10     }
    View Code

    调用接口

    1 @Autowired
    2     private lateinit var rmiService: RmiService
    3 
    4 fun test(){
    5 rmiService.addSupplierSale(saleId)
    6 }
    View Code
  • 相关阅读:
    css
    数据库
    手机编码
    火锅开2了
    Linux 与 Virtual PC(VirtualBox)
    切图
    [zz] Loading Master Data without using the PSA in SAP BI 7.0
    Notes: CRM Analytics–BI from a CRM perspective (2)
    Notes: CRM Analytics–BI from a CRM perspective (3)
    Sharing: hints, tcodes, commands that may help when solving support package implementation problems
  • 原文地址:https://www.cnblogs.com/dwb91/p/10579884.html
Copyright © 2020-2023  润新知