1 Star 0 Fork 9

世界无童话 / OF.JsonRpc

forked from OF Team / OF.JsonRpc 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

OF.JsonRpc

本项目是按照 《JSON-RPC 2.0 Specification》 的 .NET 实现, 它是基于 HTTP 或 TCP 或本地调用方式实现的轻量级松耦合的远程调用服务框架,提供了高效可用、无侵入式的方式搭建自己的服务平台,可以快速开发、调试、发布、调用服务,保持轻量级及可定制性。可以基于它构建 .NET 平台的微服务或 API。

重要特性

  • 支持目前支持 HTTP 和本地调用
  • 强大的自动的服务描述说明
  • 支持 RSA 和自定义授权机制
  • 支持自定义上下文

如何开发实现服务

1.创建新的或使用原有的 Class Library 工程,如OF.Microservices.DemoService ,该工程并不要求引用外部依赖包 OF.JsonRpc.dll ,然后在项目属性中进行设置, build 属性页 > Output > 勾选中 Xml documentation file,以便能自动生成在线文档。

2.在刚才创建的 OF.JsonRpc 工程中,创建一个自己的的服务类,并写一个 public的方法,写加入相应的注释,如下:

namespace OF.Microservices.DemoService
{
    /// <summary>
    /// 演示服务1
    /// </summary>
    public class Demo1
    {
        /// <summary>
        /// 输出两个参数的值
        /// </summary>
        /// <param name="param1">参数1</param>
        /// <param name="param2">参数2</param>
        /// <returns>返回参数1和参数2的信息</returns>
        public string Method1(string param1, string param2)
        {
            return $"param1: {param1}, param2: {param2}";
        }
    }
}

3.将本项目中的工程 OF.Microservices.Host 设为 IIS 的 Web 站点。并配置 jsonrpc.config 文件,在 serviceAssemblies 节点下配置需要暴露的服务实现 dll 程序集名称,jsonrpc.config 配置文件示例如下:

<?xml version="1.0" encoding="utf-8" ?>
<jsonrpc>
  <serviceAssemblies>
    <add assembly="OF.Microservices.DemoService" domain="Demo" methodMode="allPublic" subdomainTrimChars="Service" />
  </serviceAssemblies>
</jsonrpc>

4.然后用浏览器打开该网站的根地址,如:http://localhost:60282 即可查看服务的说明文档。

5.服务暴露的地址形如: http://localhost:60282/json.rpc

客户端 C# 的调用

详细请参见 OF.JsonRpc\OF.JsonRpc.ClientTest\CallDemoTest.cs 中的代码。需要在客户端引入 OF.JsonRpc.Client 程序集,并在 app.config/web.config 配置服务地址:

<configuration>
  <appSettings>
    <add key="JsonRpcServiceUrl" value="http://localhost:60282/json.rpc" />
  </appSettings>

调用的代码很简单,并不需要定义什么接口,完全松耦合的方式,Rpc.Call对应的参数为方法名及按顺序的参数即可。 示例如下:

// 调用服务,输入2个参数,返回一个字符串
var result1 = Rpc.Call<string>(
    "Demo.Demo1.Method1",
    "Jack","John");

// 调用服务,无返回值
Rpc.Call("Demo.Demo1.Method2", "Jack");

// 调用服务,返回一个匿名 dynamic 对象
var result = Rpc.Call<dynamic>(
    "Demo.Demo1.Method3", //JSON-RPC方法名
    new 
    {
        Name = "Jack",
        Email = "Jack@gmail.com",
        MobilePhone = "12306"
    }); // JSON-RPC的参数,
Console.WriteLine("result value, OrderNumber:" + result.OrderId);
Console.WriteLine("Customer.UserName:" + result.Customer.Name);
Console.WriteLine("Customer.Email:" + result.Customer.Email);
Console.WriteLine("Customer.MobilePhone:" + result.Customer.MobilePhone);

// 异常处理
try
{
    var result1 = Rpc.Call<string>(
        "Demo.Demo1.Method1",
        "Jack","John");
    Console.WriteLine("result value:" + result1);
}
catch (JsonRpcException jsonRpcException)
{
    if (jsonRpcException.code == 32000)
        Console.WriteLine("result business excetion:" + jsonRpcException.message);
    else
        Console.WriteLine("调用服务发生异常,{0}, {1}", jsonRpcException.message, jsonRpcException.data);
}

// 将参数为命名的对象属性进行调用
string result2 = Rpc.CallWithDeclaredParams<string>(
   "Demo.Demo1.Method1",
   new { param2 = "John", param1 = "Jack" });

浏览 JSON-RPC Help 文档

打开浏览器地址: http://localhost:60282 后进行文档首页,可以过滤

help-index

进入方法的详细说明:

help-method

进入参数或返回值的类型说明:

help-type

Java 调用

因为使用的统一的 JSON-RPC HTTP 协议,所以使用一些三方的库,推荐:https://github.com/briandilley/jsonrpc4j

JsonRpcHttpClient client = new JsonRpcHttpClient(
    new URL("http://example.com/UserService.json"));

User user = client.invoke("createUser", new Object[] { "bob", "the builder" }, User.class);

Javascript 调用

,所以使用一些三方的库,推荐:https://github.com/datagraph/jquery-jsonrpc

示例:

$.jsonRPC.request('method.name', {
  params: params,
  success: function(result) {
    // Do something with the result here
    // It comes back as an RPC 2.0 compatible response object
  },
  error: function(result) {
    // Result is an RPC 2.0 compatible response object
  }
});

TODOLIST

  • 引入 Zookeeper 作为服务发现机制的选择之一
  • 支持跨服务调用的分布式事务支持
  • 加入 .NET Core 版本
  • 可以不用 IIS 宿主
The MIT License (MIT) Copyright (c) 2017 OF Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

本项目是按照 《JSON-RPC 2.0 Specification》 的 .NET 实现, 它是基于 HTTP 或 TCP 或本地调用方式实现的轻量级松耦合的远程调用服务框架,提供了高效可用、无侵入式的方式搭建自己的服务平台,可以快速开发、调试、发布、调用服务,保持轻量级及可定制性。可以基于它构建 .NET 平台的微服务或 API。 展开 收起
C#
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
C#
1
https://gitee.com/FairyTale/OF.JsonRpc.git
git@gitee.com:FairyTale/OF.JsonRpc.git
FairyTale
OF.JsonRpc
OF.JsonRpc
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891