Java sdk依赖包以及使用说明
我们提供Java-SDK编译过后的jar包,通过在项目中引入相关jar包来导入依赖,包名为core-api-3.1.0-SNAPSHOT和core-httpclient-impl-3.1.0-SNAPSHOT
要试用SDK,请执行以下操作:
- 创建一个空的项目目录。例如,在终端中运行:
Shell
mkdir eyeofcloud-java-quickstart
- 移入新目录:
Shell
cd eyeofcloud-java-quickstart
- 创建项目结构
Shell
mkdir src\main\java
mkdir src\main\resources
mkdir libs
- 在项目根目录下创建build.gradle文件,内容如下:
plugins {
id 'java'
id 'application'
id 'com.github.johnrengelman.shadow' version '7.1.2'
}
repositories {
mavenCentral()
}
dependencies {
// 这里添加你的依赖
// 示例:添加一个常用的库
implementation 'org.slf4j:slf4j-api:1.7.36'
implementation 'ch.qos.logback:logback-classic:1.2.11'
implementation 'com.google.code.gson:gson:2.10.1'
implementation 'org.apache.httpcomponents:httpclient:4.5.13' // 可以根据需要选择合适的版本
implementation 'com.googlecode.json-simple:json-simple:1.1.1'
// 添加本地jar包依赖(将你的jar包放在libs目录下)
implementation fileTree(dir: 'libs', include: ['*.aar','*.jar'], exclude:[])
}
application {
mainClass = 'com.example.Main'
}
- 创建示例代码Main.java
Shell
mkdir src\main\java\com\example
cd src\main\java\com\example
type nul > Main.java
Main.java文件内容如下:
package com.example;
import com.eyeofcloud.ab.EyeofcloudFactory;
import com.eyeofcloud.ab.config.parser.JsonParseException;
import com.eyeofcloud.ab.Eyeofcloud;
import com.eyeofcloud.ab.EyeofcloudUserContext;
import com.eyeofcloud.ab.eyeofclouddecision.EyeofcloudDecision;
import java.util.Random;
public class Main {
public static void main(String[] args) {
// this Eyeofcloud initialization is synchronous. for other methods see the Java SDK reference
Eyeofcloud eyeofcloudClient = EyeofcloudFactory.newDefaultInstance("YOUR_SDK_KEY");
if (eyeofcloudClient.isValid()) {
/* --------------------------------
* to get rapid demo results, generate random users. Each user always sees the same variation unless you reconfigure the flag rule.
* --------------------------------
*/
Random rnd = new Random();
boolean hasOnFlags = false;
for (int i = 0; i < 10; i++) {
String userId = (rnd.nextInt(9999 - 1000) + 1000) + "";
/* --------------------------------
Create hardcoded user & bucket user into a flag variation
--------------------------------
*/
EyeofcloudUserContext user = eyeofcloudClient.createUserContext(userId);
// "product_sort" corresponds to a flag key in your Eyeofcloud project
EyeofcloudDecision decision = user.decide("product_sort");
// did decision fail with a critical error?
if (decision.getVariationKey() == null) {
System.out.println("\n\ndecision error: " + decision.getReasons());
}
// get a dynamic configuration variable
// "sort_method" corresponds to a variable key in your Eyeofcloud project
String sortMethod = null;
try {
sortMethod = decision.getVariables().getValue("sort_method", String.class);
} catch (JsonParseException e) {
e.printStackTrace();
}
if (decision.getEnabled()) {
// Keep count how many visitors had the flag enabled
hasOnFlags = true;
}
/* --------------------------------
Mock what the users sees with print statements (in production, use flag variables to implement feature configuration)
--------------------------------
*/
// always returns false until you enable a flag rule in your Eyeofcloud project
System.out.println("\n\nFlag " + (decision.getEnabled()? "on": "off") + ". User number " + user.getUserId() + " saw flag variation: " + decision.getVariationKey() + " and got products sorted by: " + sortMethod + " config variable as part of flag rule: " + decision.getRuleKey());
}
if (!hasOnFlags) {
System.out.println("\n\nFlag was off for everyone. Some reasons could include:" +
"\n1. Your sample size of visitors was too small. Rerun, or increase the iterations in the FOR loop" +
"\n2. By default you have 2 keys for 2 project environments (dev/prod). Verify in Settings>Environments that you used the right key for the environment where your flag is toggled to ON." +
"\n\nCheck your key at https://app.eyeofcloud.com/v2/projects/" + eyeofcloudClient.getProjectConfig().getProjectId() + "settings/implementation");
}
} else {
System.out.println("Eyeofcloud client invalid. Verify in Settings>Environments that you used the primary environment's SDK key");
}
// close before exit to flush out queued events
eyeofcloudClient.close();
}
}
-
导入java-sdk,将SDK的jar包放入libs目录下
-
. 创建gradle控制器
在根目录下执行命令
Shell
gradle wrapper
在根目录下,对之前创建的示例应用运行以下命令:
gradlew build
- 使用gradlew命令运行
Shell
gradlew run
- 使用java命令运行
Shell
java -cp "build\libs\*" com.example.Main
- 使用jar包运行
Shell
java -jar build\libs\eyeofcloud-java-quickstart-all.jar