首页 > 系统相关 >Windows Azure Service Bus (7) 使用Service Principal访问Azure Service Bus

Windows Azure Service Bus (7) 使用Service Principal访问Azure Service Bus

时间:2024-04-25 17:26:53浏览次数:21  
标签:Service Bus client Azure using Principal

  《Windows Azure Platform 系列文章目录

 

  本文介绍的是,国内由世纪互联运维的Azure China

 

 

  在笔者之前的文档中,我们链接Service Bus,都是通过链接字符串Connection String来连接的。

  Windows Azure Service Bus (2) 队列(Queue)入门

 

  Azure Service Principal,类似AWS Application。是通过API或者SDK的方式,开发使用。

  我们可以通过Service Principal,来连接Azure Service Bus。

  1.首先我们先创建1个Service Principal,具体步骤:Azure AD (8) 创建配置应用程序和服务主体 (Application and Service Principal)

  2.在RBAC里,把Service Principal设置权限为Azure Service Bus Data Owner

  

  上面三个Role的区别:

  (1)Azure Service Bus Data Owner,具有全部权限(可以新增、或者删除Sevice Bus对象),对Service Bus数据可以读,也可以写

  (2)Azure Service Bus Data Receiver,对Service Bus操作只读(不能删除Service Bus对象),对Service Bus数据只读

  (3)Azure Service Bus Data Sender,对Service Bus操作只读(不能删除Service Bus对象),对Service Bus数据可以发送

 

  3.新建Visual Studio Console项目,使用下面的代码:

  

using Azure.Messaging.ServiceBus;
using Azure.Identity;
using Azure.Core;
using Microsoft.Identity.Client;

// name of your Service Bus queue
// the client that owns the connection and can be used to create senders and receivers
ServiceBusClient client;

// the sender used to publish messages to the queue
ServiceBusSender sender;

// number of messages to be sent to the queue
const int numOfMessages = 3;

// The Service Bus client types are safe to cache and use as a singleton for the lifetime
// of the application, which is best practice when messages are being published or read
// regularly.
//
// Set the transport type to AmqpWebSockets so that the ServiceBusClient uses the port 443. 
// If you use the default AmqpTcp, ensure that ports 5671 and 5672 are open.
var clientOptions = new ServiceBusClientOptions
{
    TransportType = ServiceBusTransportType.AmqpWebSockets
};

string tenantid = "";
string clientid = "";
string clientsecret = "";


ClientSecretCredentialOptions opts = new ClientSecretCredentialOptions()
{
    AuthorityHost = AzureAuthorityHosts.AzureChina
};

TokenCredential credential = new ClientSecretCredential(tenantid, clientid, clientsecret, opts);

//这里输入Sevice Bus的访问url,这里是我的演示环境
client = new ServiceBusClient("leiservicebus01.servicebus.chinacloudapi.cn", credential);

//这里我用的队列名称为queue01
sender = client.CreateSender("queue01");

// create a batch 
using ServiceBusMessageBatch messageBatch = await sender.CreateMessageBatchAsync();

for (int i = 1; i <= numOfMessages; i++)
{
    // try adding a message to the batch
    if (!messageBatch.TryAddMessage(new ServiceBusMessage($"Message {i}")))
    {
        // if it is too large for the batch
        throw new Exception($"The message {i} is too large to fit in the batch.");
    }
}

try
{
    // Use the producer client to send the batch of messages to the Service Bus queue
    await sender.SendMessagesAsync(messageBatch);
    Console.WriteLine($"A batch of {numOfMessages} messages has been published to the queue.");
}
finally
{
    // Calling DisposeAsync on client types is required to ensure that network
    // resources and other unmanaged objects are properly cleaned up.
    await sender.DisposeAsync();
    await client.DisposeAsync();
}

Console.WriteLine("Press any key to end the application");
Console.ReadKey();

 

标签:Service,Bus,client,Azure,using,Principal
From: https://www.cnblogs.com/threestone/p/18158158

相关文章

  • Dynamics 365 F&O and firewalls - monitor Azure IP ranges
    Contents  hide 1 AzureIPRanges:canwemonitorthem?2 Myproposal:anAzurefunction2.1 Authentication2.2 Thefunction2.3 Environmentvariables2.4 HTTPcall2.5 Functionresponse3 Usingthefunction4 Let’stestit!4.1 Subsc......
  • modbus和字节序
    numconvert软件上显示的十六进制是大端顺序,即数值顺序。modbus协议规定是按大端传输(见英文版说明),但是确切的说,它只是借用“大端”这个术语以表示它是由左往右依次字节传输的,因为毕竟大小端只有到了数值层面才有意义。针对16位传输,只存在正序(AB)或反序(BA)两种方式。针对双字32位......
  • Modbus转Profinet网关接电表与工控机通讯
    Modbus转Profinet网关(XD-MDPN100/300)的主要功能是实现Modbus协议和Profinet协议之间的转换和通信。Modbus转Profinet网关集成了Modbus和Profinet两种协议,支持ModbusRTU主站/从站,并可以与RS485接口的设备,如变频器、智能高低压电器、电量测量装置等进行连接。同时,它自带网口和串口,......
  • 【Azure Logic App】中国区标准版本的逻辑应用(Standard Logic App)无法查看历史执行
    问题描述使用中国区标准版本逻辑应用(StandardLogicApp),常规情况下,可以正常查看历史执行中的输入/输出日志,方便排查。但是,现在居然无法查看,这个情况有什么解决之道呢? 问题解答Azure门户报错调查第一规则:打开浏览器开发者模式,查看页面中所发送的网络请求,检查是否有请求......
  • Modbus转Profinet网关接称重设备与工控机通讯
    Modbus转Profinet网关接称重设备与工控机通讯Modbus转Profinet网关(XD-MDPN100)是一种能够实现Modbus协议和Profinet协议之间转换的设备。Modbus转Profinet网关可提供单个或多个RS485接口,使得不同设备之间可以顺利进行通信,进一步提升了工业自动化程度。通过使用Modbus转Profinet网关......
  • Azure REST API (0) 概述
    《WindowsAzurePlatform系列文章目录》 1.概述1.我们在使用Azure云服务的时候,可以通过AzurePortal: https://portal.azure.com,输入邮箱地址和密码,然后通过交互式(鼠标点击)的方式创建或者删除微软云的资源2.我们也可以通过API或者SDK的方式进行调用,集......
  • nfs-server启动失败:Unable to break cycle starting with nfs-server.service/start
    [root@harbor~]#systemctlstatusnfs-server.service●nfs-server.service-NFSserverandservicesLoaded:loaded(/usr/lib/systemd/system/nfs-server.service;disabled;vendorpreset:disabled)Drop-In:/run/systemd/generator/nfs-server.service.d......
  • 如何解决Modbus转Profinet网关无法识别或连接Modbus设备
    Modbus转Profinet网关(XD-MDPN100)是一种用于工业自动化领域的设备,主要实现Modbus和Profinet之间的数据转换和传输。Modbus转Profinet网关(XD-MDPN100)不仅具有将Modbus协议转换为Profinet协议的功能,还具有RS485/232接口,使得不同厂商的设备之间能够连接实现通信和数据交换,从而提高系统......
  • 'vue-cli-service' 不是内部或外部命令,也不是可运行的程序
    D:\Work\NewProgramm\14)福建附二院后台管理\FujianHospitalTextile\ZR.Vue>yarnrundevyarnrunv1.22.22warningpackage.json:Nolicensefield$setNODE_OPTIONS=--openssl-legacy-provider&vue-cli-serviceserve'vue-cli-service'不是内部或外部命令,也不......
  • k8s serviceIP: range is full
    创建svc报错Internalerroroccurred:failedtoallocateaserviceIP:rangeisfullk8ssvc的网段默认ip为256个,在master节点的/etc/kubernetes/manifests/kube-apiserver.yaml和/etc/kubernetes/manifests/kube-controller-manager.yaml中的service-cluster-ip-range进行......