本文主要描述容器服务 Java 语言 SDK 的安装和配置,及使用 Java SDK 调用云端 API 的示例。
javax.annotation-api
。javax.annotation-api
被移除,因此需要在项目文件中加入以下依赖。<dependency> <groupId>javax.annotation</groupId> <artifactId>javax.annotation-api</artifactId> <version>1.3.2</version> </dependency>
Java SDK 下载地址:volcengine-java-sdk。
推荐通过 Maven 管理和使用 Java SDK。
说明
字节跳动的 Maven 仓库更新时间会比 Maven 中央仓库提早几个小时,您也可以按需选用字节跳动的 Maven 仓库。
conf/settings.xml
中,针对字节跳动的 Maven 仓库进行镜像设置,设置方法如下:<mirror> <id>bytedanceMaven</id> <mirrorOf>my-repo-id</mirrorOf> <name>字节跳动maven仓库</name> <url>https://artifact.bytedance.com/repository/releases/</url> </mirror>
新建一个 Maven 项目,在项目下的pom.xml
目录中,添加如下设置,引入服务依赖。引入依赖后, Maven 项目管理工具会自动下载相关 JAR 包。
注意
SDK 包更新频繁,建议您从 volcengine-java-sdk 获取 最新版本 依赖。
独立服务包
您可以按需引入服务依赖,下方代码以引入 VKE 服务依赖为例。
<dependencies> <dependency> <groupId>com.volcengine</groupId> <artifactId>volcengine-java-sdk-vke</artifactId> <version>0.1.77</version> </dependency> </dependencies>
全部 SDK 包
<dependencyManagement> <dependencies> <dependency> <groupId>com.volcengine</groupId> <artifactId>volcengine-java-sdk-bom</artifactId> <version>0.1.77</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
容器服务支持如下两种证书配置方法:
export VOLCENGINE_ACCESS_KEY=your_ak // 您火山引擎账号的 Access Key ID。 export VOLCENGINE_SECRET_KEY=your_sk // 您火山引擎账号的 Secret Access Key。
Credentials credentials = Credentials.getCredentials(ak,sk);
下方示例中参数的详细说明及配置方法,请参见 CreateCluster。
/** * Example Code generated by Beijing Volcanoengine Technology. */ package com.volcengine.sdk.example; import com.volcengine.ApiClient; import com.volcengine.ApiException; import com.volcengine.sign.Credentials; import com.volcengine.vke.VkeApi; import com.volcengine.vke.model.*; import java.util.*; public class TestCreateCluster { public static void main(String[] args) throws Exception { // 您火山引擎账号的 Access Key ID、Secret Access Key 和业务所在地域(Region)。 String ak = "Your AK"; String sk = "Your SK"; String region = "Your Region"; ApiClient apiClient = new ApiClient() .setCredentials(Credentials.getCredentials(ak, sk)) .setRegion(region); VkeApi api = new VkeApi(apiClient); PublicAccessNetworkConfigForCreateClusterInput reqPublicAccessNetworkConfig = new PublicAccessNetworkConfigForCreateClusterInput(); reqPublicAccessNetworkConfig.setBandwidth(10); reqPublicAccessNetworkConfig.setBillingType(3); ApiServerPublicAccessConfigForCreateClusterInput reqApiServerPublicAccessConfig = new ApiServerPublicAccessConfigForCreateClusterInput(); reqApiServerPublicAccessConfig.setPublicAccessNetworkConfig(reqPublicAccessNetworkConfig); ClusterConfigForCreateClusterInput reqClusterConfig = new ClusterConfigForCreateClusterInput(); reqClusterConfig.setApiServerPublicAccessConfig(reqApiServerPublicAccessConfig); reqClusterConfig.setApiServerPublicAccessEnabled(true); reqClusterConfig.setResourcePublicAccessDefaultEnabled(false); reqClusterConfig.setSubnetIds(Arrays.asList("subnet-id")); FlannelConfigForCreateClusterInput reqFlannelConfig = new FlannelConfigForCreateClusterInput(); reqFlannelConfig.setMaxPodsPerNode(64); reqFlannelConfig.setPodCidrs(Arrays.asList("172.17.**.**/19")); PodsConfigForCreateClusterInput reqPodsConfig = new PodsConfigForCreateClusterInput(); reqPodsConfig.setFlannelConfig(reqFlannelConfig); reqPodsConfig.setPodNetworkMode(PodsConfigForCreateClusterInput.PodNetworkModeEnum.valueOf(PodsConfigForCreateClusterInput.PodNetworkModeEnum.class, "Flannel")); ServicesConfigForCreateClusterInput reqServicesConfig = new ServicesConfigForCreateClusterInput(); reqServicesConfig.setServiceCidrsv4(Arrays.asList("172.25.**.**/17")); TagForCreateClusterInput reqTags = new TagForCreateClusterInput(); reqTags.setKey("tag-key"); reqTags.setValue("tag-value"); CreateClusterRequest createClusterRequest = new CreateClusterRequest(); createClusterRequest.setClientToken("client-token"); createClusterRequest.setClusterConfig(reqClusterConfig); createClusterRequest.setDeleteProtectionEnabled(false); createClusterRequest.setDescription("cluster-description"); createClusterRequest.setKubernetesVersion("1.24"); createClusterRequest.setName("cluster-name"); createClusterRequest.setPodsConfig(reqPodsConfig); createClusterRequest.setServicesConfig(reqServicesConfig); createClusterRequest.setTags(Arrays.asList(reqTags)); try { // 复制代码运行示例,请自行打印 API 返回值。 api.createCluster(createClusterRequest); } catch (ApiException e) { // 复制代码运行示例,请自行打印 API 错误信息。 // System.out.println(e.getResponseBody()); } } }
下方示例中参数的详细说明及配置方法,请参见 UpdateClusterConfig。
/** * Example Code generated by Beijing Volcanoengine Technology. */ package com.volcengine.sdk.example; import com.volcengine.ApiClient; import com.volcengine.ApiException; import com.volcengine.sign.Credentials; import com.volcengine.vke.VkeApi; import com.volcengine.vke.model.*; public class TestUpdateClusterConfig { public static void main(String[] args) throws Exception { // 您火山引擎账号的 Access Key ID、Secret Access Key 和业务所在地域(Region)。 String ak = "Your AK"; String sk = "Your SK"; String region = "Your Region"; ApiClient apiClient = new ApiClient() .setCredentials(Credentials.getCredentials(ak, sk)) .setRegion(region); VkeApi api = new VkeApi(apiClient); PublicAccessNetworkConfigForUpdateClusterConfigInput reqPublicAccessNetworkConfig = new PublicAccessNetworkConfigForUpdateClusterConfigInput(); reqPublicAccessNetworkConfig.setBandwidth(100); reqPublicAccessNetworkConfig.setBillingType(3); ApiServerPublicAccessConfigForUpdateClusterConfigInput reqApiServerPublicAccessConfig = new ApiServerPublicAccessConfigForUpdateClusterConfigInput(); reqApiServerPublicAccessConfig.setPublicAccessNetworkConfig(reqPublicAccessNetworkConfig); ClusterConfigForUpdateClusterConfigInput reqClusterConfig = new ClusterConfigForUpdateClusterConfigInput(); reqClusterConfig.setApiServerPublicAccessConfig(reqApiServerPublicAccessConfig); reqClusterConfig.setApiServerPublicAccessEnabled(true); UpdateClusterConfigRequest updateClusterConfigRequest = new UpdateClusterConfigRequest(); updateClusterConfigRequest.setClientToken("client-token"); updateClusterConfigRequest.setClusterConfig(reqClusterConfig); updateClusterConfigRequest.setDescription("cluster-description"); updateClusterConfigRequest.setId("cluster-id"); updateClusterConfigRequest.setName("cluster-name"); try { // 复制代码运行示例,请自行打印 API 返回值。 api.updateClusterConfig(updateClusterConfigRequest); } catch (ApiException e) { // 复制代码运行示例,请自行打印 API 错误信息。 // System.out.println(e.getResponseBody()); } } }
下方示例中参数的详细说明及配置方法,请参见 ListClusters。
/** * Example Code generated by Beijing Volcanoengine Technology. */ package com.volcengine.sdk.example; import com.volcengine.ApiClient; import com.volcengine.ApiException; import com.volcengine.sign.Credentials; import com.volcengine.vke.VkeApi; import com.volcengine.vke.model.*; import java.util.*; public class TestListClusters { public static void main(String[] args) throws Exception { // 您火山引擎账号的 Access Key ID、Secret Access Key 和业务所在地域(Region)。 String ak = "Your AK"; String sk = "Your SK"; String region = "Your Region"; ApiClient apiClient = new ApiClient() .setCredentials(Credentials.getCredentials(ak, sk)) .setRegion(region); VkeApi api = new VkeApi(apiClient); FilterForListClustersInput reqFilter = new FilterForListClustersInput(); reqFilter.setIds(Arrays.asList("cluster-id")); ListClustersRequest listClustersRequest = new ListClustersRequest(); listClustersRequest.setFilter(reqFilter); listClustersRequest.setPageNumber(1); listClustersRequest.setPageSize(10); try { // 复制代码运行示例,请自行打印 API 返回值。 api.listClusters(listClustersRequest); } catch (ApiException e) { // 复制代码运行示例,请自行打印 API 错误信息。 // System.out.println(e.getResponseBody()); } } }
下方示例中参数的详细说明及配置方法,请参见 CreateNodePool。
/** * Example Code generated by Beijing Volcanoengine Technology. */ package com.volcengine.sdk.example; import com.volcengine.ApiClient; import com.volcengine.ApiException; import com.volcengine.sign.Credentials; import com.volcengine.vke.VkeApi; import com.volcengine.vke.model.*; import java.util.*; public class TestCreateNodePool { public static void main(String[] args) throws Exception { // 您火山引擎账号的 Access Key ID、Secret Access Key 和业务所在地域(Region)。 String ak = "Your AK"; String sk = "Your SK"; String region = "Your Region"; ApiClient apiClient = new ApiClient() .setCredentials(Credentials.getCredentials(ak, sk)) .setRegion(region); VkeApi api = new VkeApi(apiClient); AutoScalingForCreateNodePoolInput reqAutoScaling = new AutoScalingForCreateNodePoolInput(); reqAutoScaling.setDesiredReplicas(1); reqAutoScaling.setEnabled(true); reqAutoScaling.setMaxReplicas(100); reqAutoScaling.setMinReplicas(0); reqAutoScaling.setPriority(10); LabelForCreateNodePoolInput reqLabels = new LabelForCreateNodePoolInput(); reqLabels.setKey("label-key"); reqLabels.setValue("label-value"); TaintForCreateNodePoolInput reqTaints = new TaintForCreateNodePoolInput(); reqTaints.setEffect(TaintForCreateNodePoolInput.EffectEnum.valueOf(TaintForCreateNodePoolInput.EffectEnum.class, "NoSchedule")); reqTaints.setKey("taint-key"); reqTaints.setValue("taint-value"); KubernetesConfigForCreateNodePoolInput reqKubernetesConfig = new KubernetesConfigForCreateNodePoolInput(); reqKubernetesConfig.setCordon(false); reqKubernetesConfig.setLabels(Arrays.asList(reqLabels)); reqKubernetesConfig.setTaints(Arrays.asList(reqTaints)); DataVolumeForCreateNodePoolInput reqDataVolumes = new DataVolumeForCreateNodePoolInput(); reqDataVolumes.setSize(20); reqDataVolumes.setType(DataVolumeForCreateNodePoolInput.TypeEnum.valueOf(DataVolumeForCreateNodePoolInput.TypeEnum.class, "ESSD_PL0")); LoginForCreateNodePoolInput reqLogin = new LoginForCreateNodePoolInput(); reqLogin.setPassword("***"); SecurityForCreateNodePoolInput reqSecurity = new SecurityForCreateNodePoolInput(); reqSecurity.setLogin(reqLogin); reqSecurity.setSecurityStrategies(Arrays.asList(SecurityForCreateNodePoolInput.SecurityStrategiesEnum.valueOf(SecurityForCreateNodePoolInput.SecurityStrategiesEnum.class, "Hids"))); SystemVolumeForCreateNodePoolInput reqSystemVolume = new SystemVolumeForCreateNodePoolInput(); reqSystemVolume.setSize(40); reqSystemVolume.setType(SystemVolumeForCreateNodePoolInput.TypeEnum.valueOf(SystemVolumeForCreateNodePoolInput.TypeEnum.class, "ESSD_PL0")); TagForCreateNodePoolInput reqTags = new TagForCreateNodePoolInput(); reqTags.setKey("tag-key"); reqTags.setValue("tag-value"); NodeConfigForCreateNodePoolInput reqNodeConfig = new NodeConfigForCreateNodePoolInput(); reqNodeConfig.setDataVolumes(Arrays.asList(reqDataVolumes)); reqNodeConfig.setImageId("image-id"); reqNodeConfig.setInstanceChargeType(NodeConfigForCreateNodePoolInput.InstanceChargeTypeEnum.valueOf(NodeConfigForCreateNodePoolInput.InstanceChargeTypeEnum.class, "PostPaid")); reqNodeConfig.setInstanceTypeIds(Arrays.asList("instance-type")); reqNodeConfig.setNamePrefix("prefix"); reqNodeConfig.setSecurity(reqSecurity); reqNodeConfig.setSubnetIds(Arrays.asList("subnet-id")); reqNodeConfig.setSystemVolume(reqSystemVolume); reqNodeConfig.setTags(Arrays.asList(reqTags)); TagForCreateNodePoolInput reqTags1 = new TagForCreateNodePoolInput(); reqTags1.setKey("tag-key"); reqTags1.setValue("tag-value"); CreateNodePoolRequest createNodePoolRequest = new CreateNodePoolRequest(); createNodePoolRequest.setAutoScaling(reqAutoScaling); createNodePoolRequest.setClientToken("client-token"); createNodePoolRequest.setClusterId("cluster-id"); createNodePoolRequest.setKubernetesConfig(reqKubernetesConfig); createNodePoolRequest.setName("nodepool-name"); createNodePoolRequest.setNodeConfig(reqNodeConfig); createNodePoolRequest.setTags(Arrays.asList(reqTags1)); try { // 复制代码运行示例,请自行打印 API 返回值。 api.createNodePool(createNodePoolRequest); } catch (ApiException e) { // 复制代码运行示例,请自行打印 API 错误信息。 // System.out.println(e.getResponseBody()); } } }
下方示例中参数的详细说明及配置方法,请参见 UpdateNodePoolConfig。
/** * Example Code generated by Beijing Volcanoengine Technology. */ package com.volcengine.sdk.example; import com.volcengine.ApiClient; import com.volcengine.ApiException; import com.volcengine.sign.Credentials; import com.volcengine.vke.VkeApi; import com.volcengine.vke.model.*; public class TestUpdateNodePoolConfig { public static void main(String[] args) throws Exception { // 您火山引擎账号的 Access Key ID、Secret Access Key 和业务所在地域(Region)。 String ak = "Your AK"; String sk = "Your SK"; String region = "Your Region"; ApiClient apiClient = new ApiClient() .setCredentials(Credentials.getCredentials(ak, sk)) .setRegion(region); VkeApi api = new VkeApi(apiClient); AutoScalingForUpdateNodePoolConfigInput reqAutoScaling = new AutoScalingForUpdateNodePoolConfigInput(); reqAutoScaling.setDesiredReplicas(10); UpdateNodePoolConfigRequest updateNodePoolConfigRequest = new UpdateNodePoolConfigRequest(); updateNodePoolConfigRequest.setAutoScaling(reqAutoScaling); updateNodePoolConfigRequest.setClientToken("client-token"); updateNodePoolConfigRequest.setClusterId("cluster-id"); updateNodePoolConfigRequest.setId("nodepool-id"); updateNodePoolConfigRequest.setName("nodepool-name"); try { // 复制代码运行示例,请自行打印 API 返回值。 api.updateNodePoolConfig(updateNodePoolConfigRequest); } catch (ApiException e) { // 复制代码运行示例,请自行打印 API 错误信息。 // System.out.println(e.getResponseBody()); } } }
下方示例中参数的详细说明及配置方法,请参见 ListNodePools。
/** * Example Code generated by Beijing Volcanoengine Technology. */ package com.volcengine.sdk.example; import com.volcengine.ApiClient; import com.volcengine.ApiException; import com.volcengine.sign.Credentials; import com.volcengine.vke.VkeApi; import com.volcengine.vke.model.*; import java.util.*; public class TestListNodePools { public static void main(String[] args) throws Exception { // 您火山引擎账号的 Access Key ID、Secret Access Key 和业务所在地域(Region)。 String ak = "Your AK"; String sk = "Your SK"; String region = "Your Region"; ApiClient apiClient = new ApiClient() .setCredentials(Credentials.getCredentials(ak, sk)) .setRegion(region); VkeApi api = new VkeApi(apiClient); FilterForListNodePoolsInput reqFilter = new FilterForListNodePoolsInput(); reqFilter.setClusterIds(Arrays.asList("cluster-id")); reqFilter.setIds(Arrays.asList("nodepool-id")); ListNodePoolsRequest listNodePoolsRequest = new ListNodePoolsRequest(); listNodePoolsRequest.setFilter(reqFilter); listNodePoolsRequest.setPageNumber(1); listNodePoolsRequest.setPageSize(10); try { // 复制代码运行示例,请自行打印 API 返回值。 api.listNodePools(listNodePoolsRequest); } catch (ApiException e) { // 复制代码运行示例,请自行打印 API 错误信息。 // System.out.println(e.getResponseBody()); } } }
下方示例中参数的详细说明及配置方法,请参见 CreateAddon。
/** * Example Code generated by Beijing Volcanoengine Technology. */ package com.volcengine.sdk.example; import com.volcengine.ApiClient; import com.volcengine.ApiException; import com.volcengine.sign.Credentials; import com.volcengine.vke.VkeApi; import com.volcengine.vke.model.*; public class TestCreateAddon { public static void main(String[] args) throws Exception { // 您火山引擎账号的 Access Key ID、Secret Access Key 和业务所在地域(Region)。 String ak = "Your AK"; String sk = "Your SK"; String region = "Your Region"; ApiClient apiClient = new ApiClient() .setCredentials(Credentials.getCredentials(ak, sk)) .setRegion(region); VkeApi api = new VkeApi(apiClient); CreateAddonRequest createAddonRequest = new CreateAddonRequest(); createAddonRequest.setClientToken("client-token"); createAddonRequest.setClusterId("cluster-id"); createAddonRequest.setDeployMode(CreateAddonRequest.DeployModeEnum.valueOf(CreateAddonRequest.DeployModeEnum.class, "Managed")); createAddonRequest.setDeployNodeType(CreateAddonRequest.DeployNodeTypeEnum.valueOf(CreateAddonRequest.DeployNodeTypeEnum.class, "Node")); createAddonRequest.setName("cluster-autoscaler"); try { // 复制代码运行示例,请自行打印 API 返回值。 api.createAddon(createAddonRequest); } catch (ApiException e) { // 复制代码运行示例,请自行打印 API 错误信息。 // System.out.println(e.getResponseBody()); } } }