MarshalByRefObjects远程对象及其调用方法

A Short Description to MarshalByRefObjects Remote Object and Invocation Tips<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

 

A MarshalByRefObject is a remote object that runs on the server and accepts methods calls from the client. Its data is stored in the server’s memeory and its methods executed in the server’s AppDomain. Instead of passing around a variable that points to an object of this type, in reality only a pointer-like construct-called an ObjRef-is passed around.

MarshalByRefObject远程对象在服务端运行并接受来自Client端的方法调用。MarshalByRefObject远程对象的数据保存在Server端内存,并且也在ServerAppDomain内执行方法。仅仅一个类似指针的ObjRefObjRef is [Serializable])对象引用传递到Client端,ObjRef封装在Client端的Proxy object中。
Remoting_MarshalByRefObjects.jpg

ObjRef不同于C++语言的指针执行内存地址,而是包含网络地址(如TCP/IP地址和TCP端口)和Object IDServer端通过Object ID来识别Client端正在调用的对象实例。

 

 

MarshalByRefObjects can be categorized into two groups: server-activated objects (SAOs) and client-activated objects (CAOs).

MarshalByRefObjects远程对象分为2组:服务端激活对象和客户端激活对象。这一点相信大家都了解。

 

1. Server-Activated Objects

服务端激活对象可以分为如下3种类型,不过Client端的调用代码都相同,使用Activator.GetObject()方法获取Remote Objects

(1) Singleton Objects

Only one instance of a Singleton object can exist at any given time. When receiving a client’s request, the server checks its internal tables to see if an instance of this class already exists; if not, this object will be created an stored in the table.

Note  Singletons have an associated lifetime as well, so be sure to override the standard lease time if you don't want your object to be destroyed after some minutes.

在任何时间,Server端仅存在一个Singleton对象实例(当然,有可能在Server端没有Singleton对象实例)。

For registering an object as a Singleton, you can use the following lines of code:

 

RemotingConfiguration.RegisterWellKnownServiceType(

         typeof(<YourClass>), "<URL>",

         WellKnownObjectMode.Singleton);

 

Use Singletons when you want to share data or resources between clients.

 

(2) SingleCall Objects

For SingleCall objects the server will create a single object, execute the method, and destroy the object again. Objects of this kind can obviously not hold any state information.

每次接受来自Client端的请求,Server端创建一个新的SingleCall对象,执行调用的方法,然后释放对象。在Server端不保存对象状态信息。

SingleCall Objects are registered at the server using the following statement:

 

RemotingConfiguration.RegisterWellKnownServiceType(

         typeof(<YourClass>), "<URL>",

         WellKnownObjectMode.SingleCall);

 

(3) Published Objects

When using either SingleCall or Singleton objects, the necessary instances will be created dynamically during a client's request. You publish a certain object instance that's been precreated on the server.

在没有Client端的请求过来之前,Published对象已经在Server端创建,这点和SingleCall/Singleton对象不同。并且Published对象可以通过constructor来初始化Class的成员变量。

 

In this case you can use RemotingServices.Marshal() to publish a given instance that behaves like a Singleton afterwards. The only difference is that the object has to already exist at the server before publication.

你需要使用RemotingServices.Marshal()来发行一个对象实例,如下代码所示。之后,Published对象和Singleton对象行为相似。

 

YourObject obj = new YourObject(<your params for constructor>);

RemotingServices.Marshal(obj,"YourUrl.soap");

 

2. Client-Activated Objects

A client-activated object (CAO) behaves mostly the same way as does a "normal" .NET object (or a COM object). When a creation request on the client is encountered (using Activator.CreateInstance() or the new operator), an activation message is sent to the server, where a remote object is created. On the client a proxy that holds the ObjRef to the server object is created like it is with SAOs.

CAO对象的行为与.Net对象(或COM对象)类似。当Client通过Activator.CreateInstance()new来要求创建对象实例时,激活的消息就会发送到Server端,并创建客户端激活的对象实例。同时在Client端创建一个proxyproxy拥有指向Server对象的ObjRef(对象引用)。

 

A client-activated object's lifetime is managed by the same lifetime service used by SAOs. CAOs are so-called stateful objects; an instance variable that has been set by the client can be retrieved again and will contain the correct value. These objects will store state information from one method call to the other. CAOs are explicitly created by the client, so they can have distinct constructors like normal .NET objects do.

CAO对象的生存期租约和SAO一样通过生存期租约服务(lifetime service)来管理。CAO对象是所谓的有状态对象。Client设置的实例变量可以再次被正确获取,这些对象将保存状态信息。CAO对象由Client显式创建,因此它们可以与正常的.Net对象一样有显式的构造函数。

 

(1) Direct/Transparent Creation 直接/透明创建对象

The .NET Remoting Framework can be configured to allow client-activated objects to be created like normal objects using the new operator. Unfortunately, this manner of creation has one serious drawback: you cannot use shared interfaces or base classes. This means that you either have to ship the compiled objects to your clients or use SoapSuds to extract the metadata. 注意其中提及的new关键字创建对象的缺点。

 

Server端注册Client-Activated Object示例代码如下:

RemotingConfiguration.ApplicationName = "MyServer";

RemotingConfiguration.RegisterActivatedServiceType(

                             typeof(MyRemoteObject));

 

这里,不需要知道URL,但是需要设定RemotingConfiguration.ApplicationName来识别Remote Object

 

Remote ObjectURL为:http://<hostname>:<port>/<ApplicationName>

 

然后在Server对象存放的目录,通过 SoapSuds来提取对象的metadata(元数据):

soapsuds -ia:<server> -nowp -oa:<generated_metadata.dll>

<server> - 输入的Assembly Name,不要有.dll or .exe等后缀。

-nowp – 表示生成nonwrapped proxy,如果没有-nowp选项,则生成wrapped proxy

 

另外,请注意:SoapSuds cannot extract the metadata for nondefault constructors

 

Client通过引用上面创建的<generated_metadata.dll>,然后调用Client-Activated Objects如下:

//Client端上的对象Type注册为可在Server上激活的类型

RemotingConfiguration.RegisterActivatedClientType(

               typeof(MyRemoteObject),

               "http://localhost:1234/MyServer");

// 调用newServer端创建客户端激活的对象实例,Client端会创建一个proxy

MyRemoteObject obj1 = new MyRemoteObject();

 

(2) Using the Factory Design Pattern使用工厂设计模式来创建对象

我们知道SoapSuds不能够提取非缺省构造函数的元数据(metadata),因此当你的应用程序的设计依赖这一功能时,就可以考虑使用Factory Design Pattern来实现,Factory Design Pattern通过SAO提供一个的工厂方法来创建CAO对象示例。

 

这里简单描述一下步骤,详细代码可以查阅《Advanced .Net Remoting - C# EditionChapter 3

  • 定义Remote ObjectFactory Object的共享接口或抽象类,编译.DLL

  • Server端引用上述DLL,并分别实现Remote ObjectFactory Object

  • Factory Object提供一个工厂方法,用来根据Client的请求创建对应的Remote Object

  • Factory Object注册为SingletonSAO对象。

  • Client端获取Factory Object远程对象,并调用工厂方法获取对应的Remote Object

 

 

 

Reference:

1. Ingo Rammer, Advanced .Net Remoting – C# Version

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 Feign 来实现远程方法调用。Feign 是一个基于 Java 的声明式 HTTP 客户端,它简化了编写 HTTP 客户端的过程。要使用 Feign 进行远程调用,你需要按照以下步骤进行操作: 1. 添加依赖:在你的项目中添加 Feign 的依赖。你可以在 Maven 或 Gradle 中添加相应的依赖项。 2. 创建接口:定义一个接口,其中包含要调用远程服务的方法。使用 `@FeignClient` 注解标记接口,并指定要调用的服务的名称。 3. 声明方法:在接口中声明要调用方法,并使用 `@RequestMapping` 或其他相关注解来指定远程服务的路径、请求方法和参数。 4. 注入接口:在你的业务类中通过依赖注入的方式注入 Feign 接口。 5. 调用方法:通过调用 Feign 接口的方法来实现远程服务的调用。 以下是一个简单的示例: ```java // 1. 添加依赖 // Maven: // <dependency> // <groupId>org.springframework.cloud</groupId> // <artifactId>spring-cloud-starter-openfeign</artifactId> // </dependency> // Gradle: // implementation 'org.springframework.cloud:spring-cloud-starter-openfeign' // 2. 创建接口 @FeignClient(name = "remote-service") public interface RemoteService { @RequestMapping(value = "/api/remoteMethod", method = RequestMethod.GET) String remoteMethod(@RequestParam("param") String param); } // 3. 声明方法 // 4. 注入接口 @Service public class MyService { private final RemoteService remoteService; public MyService(RemoteService remoteService) { this.remoteService = remoteService; } // 5. 调用方法 public String myMethod(String param) { return remoteService.remoteMethod(param); } } ``` 以上示例演示了如何使用 Feign 进行远程方法调用。你可以根据你的实际需求自定义接口和方法,并通过 Feign 来简化远程调用的过程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值