SQL Server 可以发送 Web 请求吗? [英] Can SQL Server send a web request?

查看:20
本文介绍了SQL Server 可以发送 Web 请求吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在本地网络上有一个 SQL Server 数据库和一个 Intranet Web 应用程序.Intranet Web 应用程序创建记录并将它们发送到数据库.我有一个 Internet Web 应用程序,我想将新记录发送到使用本地网络上的 SQL Server 数据库.

I have an SQL Server database and an intranet web application on a local network. The intranet web application creates records and sends them to the database. I have an Internet web application that I would like to send new records to using the SQL Server database on the local network.

由于各种原因,我无法更改/修改 Intranet Web 应用程序,因此我唯一的选择是在本地 SQL Server 上创建一个触发器,该触发器将使用某种 http post 请求将新记录发送到 Internet Web 应用程序或网址调用.

I can't change/modify the intranet web application for various reasons so the only option I have is to create a trigger on the local SQL Server that will send new records to the Internet web application using some sort of http post request or url call.

INTERNET Web 应用程序设置了 RESTful api,可以通过表单发布到可公开访问的 url(例如 http://www.example.com/records/new).

The INTERNET web application is set up with a RESTful api that can receive new records via form post to a publicly accessible url (e.g. http://www.example.com/records/new).

有谁知道在 SQL Server 中是否可以通过 url 发送数据(xml、json、url 中的普通变量)?

Does anyone know if sending data (xml, json, plain variables in the url) via a url can be accomplished in SQL Server?

感谢您的任何想法!

推荐答案

这是可能的,但在现实世界中比您想象的幼稚方法要复杂一些.首先,让触发器等待 HTTP 请求是不可接受的:

It is possible, but in the real world is a little bit more complicated than the naive approach you envision. Primarily, it is unacceptable to have a trigger wait for a HTTP request:

  • 首先,您的应用程序将爬行到突然停止,因为触发器会阻塞资源(主要是锁)以等待来自某个遥远的 WWW 服务的响应.
  • 第二,更微妙但更糟糕的是存在回滚时的正确性问题.如果发出 HTTP 请求的事务回滚,则无法撤消"HTTP 请求.

解决方案是通过队列将触发器与 HTTP 请求分离.触发器将请求排入本地队列并提交,而单独的处理部分将这些请求出列并发出 HTTP 请求.这解决了上面指出的两个问题.您可以将普通表用于队列(请参阅使用表作为队列),也可以使用 Service Broker,两者都运行良好.

The solution is to decouple the trigger from the HTTP request via a queue. The trigger enqueues the request into a local queue and commits, while a separate piece of processing dequeues these requests and issues the HTTP request. This solves both problems pointed out above. You can use ordinary tables for queues (see Using Tables as Queues) or you can use Service Broker, both work well.

现在关于如何使该请求出列并实际进行 HTTP 调用,我强烈建议使用专用进程(即专用于此目的的应用程序).虽然可以使用 SQLCLR,但这是一个非常糟糕的选择.SQL Server 资源(特别是工作人员)在等待 Internet 响应上非常宝贵.

Now on how to dequeue this requests and actually place the HTTP call, I strongly recommend using a dedicated process (ie. an application dedicated for this purpose). While it is possible to use SQLCLR, it is a very bad choice. SQL Server resources (specifically workers) are far to precious to waste on waiting for Internet responses.

这篇关于SQL Server 可以发送 Web 请求吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆