为 Azure 中的特定 MySQL 服务器创建专用终结点
Create a private endpoint for a specific MySQL server in Azure
我在 Azure 云上创建了一个 MySQL 服务器。现在我想使用 Java 代码 (SDK) 建立专用端点连接。你能帮我用哪个 Java class 来解决这个问题吗? Azure cloud screenshot
如果您想使用java代码建立私有端点,请参考以下代码
- 创建服务主体并将贡献者角色分配给 sp
az login
# it will create a service principal and assign a contributor rolen to the sp
az ad sp create-for-rbac -n "MyApp" --sdk-auth
- SDK
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure</artifactId>
<version>1.36.1</version>
</dependency>
- 代码
String secret ="<the sp client secret>" ;
String domain="<the sp tenantId>";
String clientId="<the sp clientId>";
String subscriptionId="";
String groupName="testmysql";
Region region = Region.JAPAN_EAST;
String networkName="mysqlvnet1";
String subnetName="default";
String serverId="<the mysql server resource id e.g. /subscriptions/b83c1ed3-c5b6-44fb-b5ba-2b83a074c23f/resourceGroups/v-wenxu-chinacxp/providers/Microsoft.DBforMySQL/servers/testsql08>"
ApplicationTokenCredentials credentials = new ApplicationTokenCredentials(clientId,domain,secret, AzureEnvironment.AZURE);
NetworkManager networkManager =NetworkManager.authenticate(credentials,subscriptionId);
// create vnet
Network network = networkManager.networks()
.define(networkName)
.withRegion(region)
.withExistingResourceGroup(groupName)
.withAddressSpace("172.31.0.0/16")
.defineSubnet(subnetName)
.withAddressPrefix("172.31.0.0/24")
.attach()
.create();
network.subnets().get(subnetName).inner().withPrivateEndpointNetworkPolicies("Disabled");
network.subnets().get(subnetName).inner().withPrivateLinkServiceNetworkPolicies("Enabled");
network.update()
.updateSubnet(subnetName)
.parent()
.apply();
// create network private endpoint.
PrivateLinkServiceConnection privateLinkServiceConnection = new PrivateLinkServiceConnection()
.withName("mysql1")
.withPrivateLinkServiceId("the resource id of mysql service")
.withPrivateLinkServiceConnectionState(new PrivateLinkServiceConnectionState()
.withStatus("Approved"))
.withGroupIds(Arrays.asList(serverId);
PrivateEndpointInner privateEndpoint = new PrivateEndpointInner()
.withPrivateLinkServiceConnections(Arrays.asList(privateLinkServiceConnection))
.withSubnet(network.subnets().get(subnetName).inner());
privateEndpoint.withLocation(region.toString());
privateEndpoint =networkManager.inner().privateEndpoints().createOrUpdate(groupName,"mysql1",privateEndpoint);
我在 Azure 云上创建了一个 MySQL 服务器。现在我想使用 Java 代码 (SDK) 建立专用端点连接。你能帮我用哪个 Java class 来解决这个问题吗? Azure cloud screenshot
如果您想使用java代码建立私有端点,请参考以下代码
- 创建服务主体并将贡献者角色分配给 sp
az login
# it will create a service principal and assign a contributor rolen to the sp
az ad sp create-for-rbac -n "MyApp" --sdk-auth
- SDK
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure</artifactId>
<version>1.36.1</version>
</dependency>
- 代码
String secret ="<the sp client secret>" ;
String domain="<the sp tenantId>";
String clientId="<the sp clientId>";
String subscriptionId="";
String groupName="testmysql";
Region region = Region.JAPAN_EAST;
String networkName="mysqlvnet1";
String subnetName="default";
String serverId="<the mysql server resource id e.g. /subscriptions/b83c1ed3-c5b6-44fb-b5ba-2b83a074c23f/resourceGroups/v-wenxu-chinacxp/providers/Microsoft.DBforMySQL/servers/testsql08>"
ApplicationTokenCredentials credentials = new ApplicationTokenCredentials(clientId,domain,secret, AzureEnvironment.AZURE);
NetworkManager networkManager =NetworkManager.authenticate(credentials,subscriptionId);
// create vnet
Network network = networkManager.networks()
.define(networkName)
.withRegion(region)
.withExistingResourceGroup(groupName)
.withAddressSpace("172.31.0.0/16")
.defineSubnet(subnetName)
.withAddressPrefix("172.31.0.0/24")
.attach()
.create();
network.subnets().get(subnetName).inner().withPrivateEndpointNetworkPolicies("Disabled");
network.subnets().get(subnetName).inner().withPrivateLinkServiceNetworkPolicies("Enabled");
network.update()
.updateSubnet(subnetName)
.parent()
.apply();
// create network private endpoint.
PrivateLinkServiceConnection privateLinkServiceConnection = new PrivateLinkServiceConnection()
.withName("mysql1")
.withPrivateLinkServiceId("the resource id of mysql service")
.withPrivateLinkServiceConnectionState(new PrivateLinkServiceConnectionState()
.withStatus("Approved"))
.withGroupIds(Arrays.asList(serverId);
PrivateEndpointInner privateEndpoint = new PrivateEndpointInner()
.withPrivateLinkServiceConnections(Arrays.asList(privateLinkServiceConnection))
.withSubnet(network.subnets().get(subnetName).inner());
privateEndpoint.withLocation(region.toString());
privateEndpoint =networkManager.inner().privateEndpoints().createOrUpdate(groupName,"mysql1",privateEndpoint);