Giter Site home page Giter Site logo

hudson's Introduction

Hudson

Hudson是pistencup工具包的dotNET部分, 关于pistencup工具包请参考:pistencup.

其他语言支持

如果你在寻找其他语言支持, 请参考: 已提供的支持

如何使用

  1. 目前需要自行编译Hudson.Core并加入到项目依赖中.

  2. 在包含服务Api定义的项目中, 需要从nuget或自行编译添加对Refit项目的依赖.

  3. Hudson依赖Steeltoe.Discovery来提供向eureka注册服务和从eureka获取服务列表的能力. 所以需要向appsetting.json文件添加相关配置:

  "spring": {
    "application": {
      "name": "sample-server"
    }
  },
  "eureka": {
    "client": {
      "serviceUrl": "http://eureka-server:10001/eureka/",
      "shouldRegisterWithEureka": true
    },
    "instance": {
      "port": 5000
    }
  }
  1. Hudson依赖Refit来提供远程调用能力. 在定义远端Api的项目中, 需要添加对Refit的依赖, 并定义接口:
using Refit;
using System.Threading.Tasks;

namespace SampleServer.Api
{
    [WorkWithEureka("sample-server")]
    //equals to [ServiceClient("http://cheok.com/", true)]
    public interface ISample
    {
        [Get("/home/get123")]
        Task<string> Get123();

        [Get("/get321")]
        Task<string> Get321();
    }
}
  1. WorkWithEurekaAttribute是对ServiceClientAttribute的简单扩展, 默认指定了workWithEureka参数为true. 被ServiceClientAttributeWorkWithEurekaAttribute标记的接口, 会在应用启动时自动向IOC注册对应的HttpClient. 在项目中可以直接通过ServiceProvider获得实例并进行远程调用:
    public class HomeController : Controller
    {
        private readonly CloudContext cloudContext;
        private readonly ISample sampleClient;
        private readonly ISampleApi sampleApiClient;
        //从构造函数获得Api对象
        public HomeController(ISample sampleClient, ISampleApi sampleApiClient, CloudContext cloudContext)
        {
            this.cloudContext = cloudContext;
            this.sampleClient = sampleClient;
            this.sampleApiClient = sampleApiClient;
        }

        public async Task<IActionResult> Index()
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            //发起远程调用
            string rst = await sampleClient.Get123();
            dic.Add("sampleClient.Get123()", rst);

            dic.Add("sampleClient.Get321()", await sampleClient.Get321());

            dic.Add("sampleApiClient.Get(\"whosyourdaddy\")", Newtonsoft.Json.JsonConvert.SerializeObject(await sampleApiClient.Get("whosyourdaddy")));

            dic.Add("cloudcontext", Newtonsoft.Json.JsonConvert.SerializeObject(cloudContext));
            

            ViewData["dic"] = dic;
            return View();
        }
    }
  1. 关于CloudContext对象: CloudContext是调用上下文的持有者, 主要提供以下成员:
Field 功能 描述
RequestID 请求标识 当前请求第一次到达服务端时生成的唯一标识, 这个标识会在所有后续调用中传递
PreviousSpanID 上一个处理节点标识 请求头中获得的上一个处理块的标识, 用于构造请求链, 如果请求头中这个数据为空, 则说明这是第一个处理节点
CurrentSpanID 当前处理块的标识 为当前处理节点生成的标识, 对于请求链中的第一个请求,这个值与RequestID一致
CallIndex 调用序号 从请求头中获得的当前处理块在同级处理过程中的调用序号
GroupName 处理分组 未完成功能, 用于标记当前请求由哪个分组的服务节点处理

CloudContext的传递策略:
racetrack_flow.png
应用中请 不要 从构造函数构造CloudContext对象, 使用ServiceProvider来获取对每个请求共享的对象,避免对调用过程分析造成困扰.

  1. 更多例子,请clone并参考示例项目.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.