文档简介:
Sentinel to Apollo to App
1.应用端获取配置中心的流控规则(限流规则、热点参数规则等) 2.Sentinel控制台持久化流控规则到配置中心 3.Sentinel控制台设置的流控规则,是否需要发布可通过参数配置 如果配置为false 则需要在配置中心进行发布操作 基于Apollo配置中心的Sentinel流控规则配置实战(一) 扩展代码:https://github.com/Fxdemon/Sentinel
配置中心相关设置
添加应用
配置openapi
使用openapi配置如下:
创建Sentinel管控台Token
在配置中心授权给需要第三方应用管理的应用对象
在Sentinel控制台
在Sentinel-dashboard的pom.xml中添加
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-openapi</artifactId>
<version>1.1.0</version>
</dependency>
用于实现从Sentinel-dashboard端修改流控配置持久化到配置中心
具体如下代码:
package com.alibaba.csp.sentinel.dashboard.rule.apollo;
import java.util.Date;
import java.util.List;
import org.apache.http.impl.client.CloseableHttpClient;
import com.ctrip.framework.apollo.openapi.client.ApolloOpenApiClient;
import com.ctrip.framework.apollo.openapi.client.constant.ApolloOpenApiConstants;
import com.ctrip.framework.apollo.openapi.client.service.AppOpenApiService;
import com.ctrip.framework.apollo.openapi.dto.NamespaceReleaseDTO;
import com.ctrip.framework.apollo.openapi.dto.OpenEnvClusterDTO;
import com.ctrip.framework.apollo.openapi.dto.OpenItemDTO;
import com.ctrip.framework.apollo.openapi.dto.OpenNamespaceDTO;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
/***
* ApolloOpenApiClient
* @author Fx_demon
*/
public final class ApolloOpenApiUtilTest {
static String serverAddr = "http://localhost:8070"; // portal url
static int readTimeout = 3000;
static String token = "55ce8ffc6ad01daddab26f11913fa4a22a9c669b"; // 申请的token -本应用:sentinel_ds
static ApolloOpenApiClient client = ApolloOpenApiClient.newBuilder()
.withPortalUrl(serverAddr)
.withToken(token)
.withReadTimeout(readTimeout)
.build();
public static void main(String[] args) {
String appId = "SentinelApp";
String env = "DEV";
String clusterName = "default";
String namespaceName = "application";
OpenNamespaceDTO openNameSpaceDTO = client.getNamespace(appId, env, clusterName, namespaceName);
List<OpenEnvClusterDTO> openEnvClusterDTOList = client.getEnvClusterInfo(appId);
// AppOpenApiService appService = client.app
System.out.println("openNameSpaceDTO:"+openNameSpaceDTO.getAppId()+"||"+openNameSpaceDTO.getItems().size());
for(OpenItemDTO item:openNameSpaceDTO.getItems()) {
System.out.println("key:"+item.getKey()+ "\nvalue:" + item.getValue());
}
OpenItemDTO itemDTO = new OpenItemDTO();
itemDTO.setKey("testFlowRule11");
itemDTO.setValue("000");
itemDTO.setDataChangeCreatedBy("apollo");
itemDTO.setDataChangeCreatedTime(new Date());
itemDTO.setDataChangeLastModifiedTime(new Date());
itemDTO.setDataChangeLastModifiedBy("apollo");
/*
//新增规则
client.createItem(appId, env, clusterName, namespaceName, itemDTO);
//修改规则
itemDTO.setValue("00011");
client.updateItem(appId, env, clusterName, namespaceName, itemDTO);
//删除规则
String key = "testFlowRule";
String operator = "apollo";
client.removeItem(appId, env, clusterName, namespaceName, key, operator);
*/
NamespaceReleaseDTO releaseDTO = new NamespaceReleaseDTO();
releaseDTO.setEmergencyPublish(true);
releaseDTO.setReleaseComment("");
releaseDTO.setReleasedBy("apollo");
releaseDTO.setReleaseTitle((new Date()).toLocaleString());
//发布规则
client.publishNamespace(appId, env, clusterName, namespaceName, releaseDTO );
}
}
以上代码用于测试应用端修改配置中心的相关配置






