本文介绍了服务商如何接入许可证并校验许可证的有效性。

背景信息

服务商仅需在其服务中加入相应校验许可证的代码,即可完成许可证鉴权功能。

许可证校验只针对服务实例包含的服务(软件/应用),不校验服务实例的资源。

鉴权许可证是否有效

  1. 获取应用部署的ECS地域(regionld)信息。
    获取到的地域信息,会在后续步骤中使用,因此需要服务商记录。访问如下网址,获得地域信息。
    curl http://100.100.100.200/latest/meta-data/region-id
    返回示例
    cn-wulanchabu

    示例中返回的cn-wulanchabu,表示ECS部署地域为乌兰察布

  2. 鉴权许可证是否有效。
    调用CheckOutLicense,鉴权许可证是否有效。
    • 若发现某个服务实例到期仍未续费,计算巢将终止此服务实例的许可证,使其许可证不可用。这时,调用CheckOutLicense后,返回许可证不可用,服务商可根据返回结果完成后续处理。
    • 若用户未出现欠费情况,则应用发起的CheckOutLicense将持续有效;若用户在欠费后重新续费,计算巢将创建新的许可证,创建之后调用CheckOutLicense后,返回许可证状态将从过期状态变成成功状态。

    调用CheckOutLicense格式如下:

    curl -H "Content-Type: application/json" -XPOST https://<regionId>.axt.aliyun.com/computeNest/license/check_out_license -d '{}'
    说明

    请将regionId替换成之前获取的地域信息。

    示例

    curl -H "Content-Type: application/json" -XPOST https://cn-wulanchabu.axt.aliyun.com/computeNest/license/check_out_license -d '{}'

    返回结果

    许可证未过期

    {
      "code":200,
      "requestId":"27b897b3-f0b1-4a03-92f9-a37b00d019ae",
      "instanceId":"i-0jl892sv08nqob89533y",
      "result":{
          "RequestId":"E7544873-69AC-1761-9F81-1248F0A0C26A",
          "ServiceInstanceId":"si-093591ffbbea4307a624",
          "LicenseMetadata":"{\"TemplateName\":\"Custom_Image_Ecs\",\"SpecificationName\":\"dataDiskSize\",\"CustomData\":\"30T\"}",
          "ExpireTime":"2022-11-08T08:56:59Z"
      }
    }
    许可证未过期

    许可证已过期

    {
      "code":400,
      "requestId":"3b39185e-44d5-45eb-b178-7fa1bbd57672",
      "instanceId":"i-0jl892sv08nqob89533y",
      "errCode":"LicenseExpired",
      "errMsg":"LicenseExpired : The license of the current service instance si-093591ffbbea4307a624 has expired, expired time Tue Nov 08 16:56:59 CST 2022.\r\nRequestId : 3AF563FD-2FED-1B97-84A1-64F15B581264"
    }
    许可证已过期

    Java代码示例

    根据上面的鉴权步骤,完整的Java代码示例如下:
    import javax.net.ssl.HttpsURLConnection;
    import java.io.BufferedReader;
    import java.io.DataOutputStream;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    public class licenseManagerDemo {
        public static void main(String[] args) throws Exception {
            //步骤一:获取应用部署的ECS地域(regionId)信息
            //通过默认Url获取ECS的regionId
            URL getRegionIdURL = new URL("http://100.100.100.200/latest/meta-data/region-id");
            HttpURLConnection httpURLConnection = (HttpURLConnection) getRegionIdURL.openConnection();
            //设置超时时间
            httpURLConnection.setConnectTimeout(10000);
            httpURLConnection.setReadTimeout(10000);
            InputStreamReader inputStreamReader = new InputStreamReader(httpURLConnection.getInputStream());
            int readerRead = inputStreamReader.read();
            String regionId = "";
            while (readerRead != -1) {
                regionId = regionId + ((char) readerRead);
                readerRead = inputStreamReader.read();
            }
            System.out.println("RegionId : " + regionId);
    
            //步骤二:鉴权许可证是否有效
            //完成鉴权相关操作 : CheckOutLicense
            licenseManagerDemo licenseManagerDemo = new licenseManagerDemo();
            String checkOutLicenseResponse = licenseManagerDemo.checkOutLicense(regionId);
            String code = licenseManagerDemo.getCode(checkOutLicenseResponse);
            //循环执行 checkOutLicense
            while (true) {
                if (!"200".equals(code)) {
                    //处理返回不成功情况
                    break;
                } else {
                    //再次调用,示例时间为60分钟
                    Thread.sleep(3600000);
                    checkOutLicenseResponse = licenseManagerDemo.checkOutLicense(regionId);
                    code = licenseManagerDemo.getCode(checkOutLicenseResponse);
                    System.out.println(checkOutLicenseResponse);
                }
            }
        }
    
        private String checkOutLicense(String regionId) throws Exception {
            String urlFormat = "https://%s.axt.aliyun.com/computeNest/license/%s";
            String operation = "check_out_license";
            URL operationURL = new URL(String.format(urlFormat, regionId, operation));
            HttpsURLConnection httpsURLConnection = (HttpsURLConnection) operationURL.openConnection();
            httpsURLConnection.setRequestProperty("Content-Type", "application/json");
            httpsURLConnection.setDoOutput(true);
            httpsURLConnection.setConnectTimeout(10000);
            httpsURLConnection.setReadTimeout(10000);
    
            DataOutputStream dataOutputStream = new DataOutputStream(httpsURLConnection.getOutputStream());
            //包年包月场景CheckOutLicense参数
            String checkOutLicenseParameter = "{\"Channel\":\"ComputeNest\"}";
            dataOutputStream.writeBytes(checkOutLicenseParameter);
            dataOutputStream.flush();
            dataOutputStream.close();
    
            BufferedReader bufferedReader =
                    new BufferedReader(new InputStreamReader(httpsURLConnection.getInputStream()));
            String checkOutLicenseResponse = bufferedReader.readLine();
            System.out.println(checkOutLicenseResponse);
            bufferedReader.close();
            return checkOutLicenseResponse;
        }
    
        private String getCode(String response) {
            int codeIndexOf = response.indexOf("code");
            return response.substring(codeIndexOf + 6, codeIndexOf + 9);
        }
    }