Signed-off-by: Eric Zhao <sczyh16@gmail.com>master
@@ -13,7 +13,7 @@ | |||
* See the License for the specific language governing permissions and | |||
* limitations under the License. | |||
*/ | |||
package com.taobao.csp.sentinel.dashboard.inmem; | |||
package com.taobao.csp.sentinel.dashboard.client; | |||
import java.io.UnsupportedEncodingException; | |||
import java.net.URLEncoder; | |||
@@ -53,31 +53,31 @@ import org.springframework.stereotype.Component; | |||
* @author leyou | |||
*/ | |||
@Component | |||
public class HttpHelper { | |||
public class SentinelApiClient { | |||
private static Logger logger = LoggerFactory.getLogger(HttpHelper.class); | |||
private static Logger logger = LoggerFactory.getLogger(SentinelApiClient.class); | |||
private static final Charset defaultCharset = Charset.forName(SentinelConfig.charset()); | |||
private CloseableHttpAsyncClient httpclient; | |||
private CloseableHttpAsyncClient httpClient; | |||
private final String resourceUrlPath = "jsonTree"; | |||
private final String clusterNodePath = "clusterNode"; | |||
private final String getRulesPath = "getRules"; | |||
private final String setRulesPath = "setRules"; | |||
private final String flowRuleType = "flow"; | |||
private final String degradeRuleType = "degrade"; | |||
private final String systemRuleType = "system"; | |||
public HttpHelper() { | |||
public SentinelApiClient() { | |||
IOReactorConfig ioConfig = IOReactorConfig.custom().setConnectTimeout(3000).setSoTimeout(3000) | |||
.setIoThreadCount(Runtime.getRuntime().availableProcessors() * 2).build(); | |||
httpclient = HttpAsyncClients.custom().setRedirectStrategy(new DefaultRedirectStrategy() { | |||
httpClient = HttpAsyncClients.custom().setRedirectStrategy(new DefaultRedirectStrategy() { | |||
@Override | |||
protected boolean isRedirectable(final String method) { | |||
return false; | |||
} | |||
}).setMaxConnTotal(4000).setMaxConnPerRoute(1000).setDefaultIOReactorConfig(ioConfig).build(); | |||
httpclient.start(); | |||
httpClient.start(); | |||
} | |||
public List<NodeVo> fetchResourceOfMachine(String ip, int port, String type) { | |||
@@ -282,7 +282,7 @@ public class HttpHelper { | |||
final HttpGet httpGet = new HttpGet(url); | |||
final CountDownLatch latch = new CountDownLatch(1); | |||
final AtomicReference<String> reference = new AtomicReference<>(); | |||
httpclient.execute(httpGet, new FutureCallback<HttpResponse>() { | |||
httpClient.execute(httpGet, new FutureCallback<HttpResponse>() { | |||
@Override | |||
public void completed(final HttpResponse response) { | |||
try { | |||
@@ -324,4 +324,7 @@ public class HttpHelper { | |||
return EntityUtils.toString(response.getEntity(), charset != null ? charset : defaultCharset); | |||
} | |||
public void close() throws Exception { | |||
httpClient.close(); | |||
} | |||
} |
@@ -13,7 +13,7 @@ | |||
* See the License for the specific language governing permissions and | |||
* limitations under the License. | |||
*/ | |||
package com.taobao.csp.sentinel.dashboard.inmem; | |||
package com.taobao.csp.sentinel.dashboard.repository.rule; | |||
import java.util.concurrent.atomic.AtomicLong; | |||
@@ -24,7 +24,7 @@ import org.springframework.stereotype.Component; | |||
* @author leyou | |||
*/ | |||
@Component | |||
public class InMemDegradeRuleStore extends InMemRepositoryAdapter<DegradeRuleEntity> { | |||
public class InMemDegradeRuleStore extends InMemoryRuleRepositoryAdapter<DegradeRuleEntity> { | |||
private static AtomicLong ids = new AtomicLong(0); | |||
@@ -13,7 +13,7 @@ | |||
* See the License for the specific language governing permissions and | |||
* limitations under the License. | |||
*/ | |||
package com.taobao.csp.sentinel.dashboard.inmem; | |||
package com.taobao.csp.sentinel.dashboard.repository.rule; | |||
import java.util.concurrent.atomic.AtomicLong; | |||
@@ -26,7 +26,7 @@ import org.springframework.stereotype.Component; | |||
* @author leyou | |||
*/ | |||
@Component | |||
public class InMemFlowRuleStore extends InMemRepositoryAdapter<FlowRuleEntity> { | |||
public class InMemFlowRuleStore extends InMemoryRuleRepositoryAdapter<FlowRuleEntity> { | |||
private static AtomicLong ids = new AtomicLong(0); | |||
@Override |
@@ -13,7 +13,7 @@ | |||
* See the License for the specific language governing permissions and | |||
* limitations under the License. | |||
*/ | |||
package com.taobao.csp.sentinel.dashboard.inmem; | |||
package com.taobao.csp.sentinel.dashboard.repository.rule; | |||
import java.util.concurrent.atomic.AtomicLong; | |||
@@ -24,7 +24,7 @@ import org.springframework.stereotype.Component; | |||
* @author leyou | |||
*/ | |||
@Component | |||
public class InMemSystemRuleStore extends InMemRepositoryAdapter<SystemRuleEntity> { | |||
public class InMemSystemRuleStore extends InMemoryRuleRepositoryAdapter<SystemRuleEntity> { | |||
private static AtomicLong ids = new AtomicLong(0); | |||
@@ -13,7 +13,7 @@ | |||
* See the License for the specific language governing permissions and | |||
* limitations under the License. | |||
*/ | |||
package com.taobao.csp.sentinel.dashboard.inmem; | |||
package com.taobao.csp.sentinel.dashboard.repository.rule; | |||
import java.util.ArrayList; | |||
import java.util.List; | |||
@@ -27,7 +27,7 @@ import com.taobao.csp.sentinel.dashboard.discovery.MachineInfo; | |||
/** | |||
* @author leyou | |||
*/ | |||
public abstract class InMemRepositoryAdapter<T extends RuleEntity> implements RuleRepository<T, Long> { | |||
public abstract class InMemoryRuleRepositoryAdapter<T extends RuleEntity> implements RuleRepository<T, Long> { | |||
/** | |||
* {@code <machine, <id, rule>>} | |||
*/ |
@@ -13,7 +13,7 @@ | |||
* See the License for the specific language governing permissions and | |||
* limitations under the License. | |||
*/ | |||
package com.taobao.csp.sentinel.dashboard.inmem; | |||
package com.taobao.csp.sentinel.dashboard.repository.rule; | |||
import java.util.List; | |||
@@ -22,8 +22,8 @@ import com.alibaba.csp.sentinel.util.StringUtil; | |||
import com.taobao.csp.sentinel.dashboard.datasource.entity.DegradeRuleEntity; | |||
import com.taobao.csp.sentinel.dashboard.discovery.MachineInfo; | |||
import com.taobao.csp.sentinel.dashboard.inmem.HttpHelper; | |||
import com.taobao.csp.sentinel.dashboard.inmem.InMemDegradeRuleStore; | |||
import com.taobao.csp.sentinel.dashboard.client.SentinelApiClient; | |||
import com.taobao.csp.sentinel.dashboard.repository.rule.InMemDegradeRuleStore; | |||
import org.slf4j.Logger; | |||
import org.slf4j.LoggerFactory; | |||
import org.springframework.beans.factory.annotation.Autowired; | |||
@@ -42,7 +42,7 @@ public class DegradeController { | |||
@Autowired | |||
InMemDegradeRuleStore repository; | |||
@Autowired | |||
private HttpHelper httpHelper; | |||
private SentinelApiClient sentinelApiClient; | |||
@ResponseBody | |||
@RequestMapping("/rules.json") | |||
@@ -57,7 +57,7 @@ public class DegradeController { | |||
return Result.ofFail(-1, "port can't be null"); | |||
} | |||
try { | |||
List<DegradeRuleEntity> rules = httpHelper.fetchDegradeRuleOfMachine(app, ip, port); | |||
List<DegradeRuleEntity> rules = sentinelApiClient.fetchDegradeRuleOfMachine(app, ip, port); | |||
rules = repository.saveAll(rules); | |||
return Result.ofSuccess(rules); | |||
} catch (Throwable throwable) { | |||
@@ -195,6 +195,6 @@ public class DegradeController { | |||
private boolean publishRules(String app, String ip, Integer port) { | |||
List<DegradeRuleEntity> rules = repository.findAllByMachine(MachineInfo.of(app, ip, port)); | |||
return httpHelper.setDegradeRuleOfMachine(app, ip, port, rules); | |||
return sentinelApiClient.setDegradeRuleOfMachine(app, ip, port, rules); | |||
} | |||
} |
@@ -22,8 +22,8 @@ import com.alibaba.csp.sentinel.util.StringUtil; | |||
import com.taobao.csp.sentinel.dashboard.datasource.entity.FlowRuleEntity; | |||
import com.taobao.csp.sentinel.dashboard.discovery.MachineInfo; | |||
import com.taobao.csp.sentinel.dashboard.inmem.HttpHelper; | |||
import com.taobao.csp.sentinel.dashboard.inmem.InMemFlowRuleStore; | |||
import com.taobao.csp.sentinel.dashboard.client.SentinelApiClient; | |||
import com.taobao.csp.sentinel.dashboard.repository.rule.InMemFlowRuleStore; | |||
import org.slf4j.Logger; | |||
import org.slf4j.LoggerFactory; | |||
import org.springframework.beans.factory.annotation.Autowired; | |||
@@ -45,7 +45,7 @@ public class FlowController { | |||
private InMemFlowRuleStore repository; | |||
@Autowired | |||
private HttpHelper httpHelper; | |||
private SentinelApiClient sentinelApiClient; | |||
@ResponseBody | |||
@RequestMapping("/rules.json") | |||
@@ -60,7 +60,7 @@ public class FlowController { | |||
return Result.ofFail(-1, "port can't be null"); | |||
} | |||
try { | |||
List<FlowRuleEntity> rules = httpHelper.fetchFlowRuleOfMachine(app, ip, port); | |||
List<FlowRuleEntity> rules = sentinelApiClient.fetchFlowRuleOfMachine(app, ip, port); | |||
rules = repository.saveAll(rules); | |||
return Result.ofSuccess(rules); | |||
} catch (Throwable throwable) { | |||
@@ -244,6 +244,6 @@ public class FlowController { | |||
private boolean publishRules(String app, String ip, Integer port) { | |||
List<FlowRuleEntity> rules = repository.findAllByMachine(MachineInfo.of(app, ip, port)); | |||
return httpHelper.setFlowRuleOfMachine(app, ip, port, rules); | |||
return sentinelApiClient.setFlowRuleOfMachine(app, ip, port, rules); | |||
} | |||
} |
@@ -22,7 +22,7 @@ import com.alibaba.csp.sentinel.util.StringUtil; | |||
import com.alibaba.csp.sentinel.command.vo.NodeVo; | |||
import com.taobao.csp.sentinel.dashboard.domain.ResourceTreeNode; | |||
import com.taobao.csp.sentinel.dashboard.inmem.HttpHelper; | |||
import com.taobao.csp.sentinel.dashboard.client.SentinelApiClient; | |||
import com.taobao.csp.sentinel.dashboard.view.vo.ResourceVo; | |||
import org.slf4j.Logger; | |||
import org.slf4j.LoggerFactory; | |||
@@ -41,7 +41,7 @@ public class ResourceController { | |||
private static Logger logger = LoggerFactory.getLogger(ResourceController.class); | |||
@Autowired | |||
HttpHelper httpFetcher; | |||
SentinelApiClient httpFetcher; | |||
/** | |||
* Fetch real time statistics info of the machine. | |||
@@ -22,8 +22,8 @@ import com.alibaba.csp.sentinel.util.StringUtil; | |||
import com.taobao.csp.sentinel.dashboard.datasource.entity.SystemRuleEntity; | |||
import com.taobao.csp.sentinel.dashboard.discovery.MachineInfo; | |||
import com.taobao.csp.sentinel.dashboard.inmem.HttpHelper; | |||
import com.taobao.csp.sentinel.dashboard.inmem.InMemSystemRuleStore; | |||
import com.taobao.csp.sentinel.dashboard.client.SentinelApiClient; | |||
import com.taobao.csp.sentinel.dashboard.repository.rule.InMemSystemRuleStore; | |||
import org.slf4j.Logger; | |||
import org.slf4j.LoggerFactory; | |||
import org.springframework.beans.factory.annotation.Autowired; | |||
@@ -43,7 +43,7 @@ public class SystemController { | |||
@Autowired | |||
private InMemSystemRuleStore repository; | |||
@Autowired | |||
private HttpHelper httpHelper; | |||
private SentinelApiClient sentinelApiClient; | |||
@ResponseBody | |||
@RequestMapping("/rules.json") | |||
@@ -58,7 +58,7 @@ public class SystemController { | |||
return Result.ofFail(-1, "port can't be null"); | |||
} | |||
try { | |||
List<SystemRuleEntity> rules = httpHelper.fetchSystemRuleOfMachine(app, ip, port); | |||
List<SystemRuleEntity> rules = sentinelApiClient.fetchSystemRuleOfMachine(app, ip, port); | |||
rules = repository.saveAll(rules); | |||
return Result.ofSuccess(rules); | |||
} catch (Throwable throwable) { | |||
@@ -209,6 +209,6 @@ public class SystemController { | |||
private boolean publishRules(String app, String ip, Integer port) { | |||
List<SystemRuleEntity> rules = repository.findAllByMachine(MachineInfo.of(app, ip, port)); | |||
return httpHelper.setSystemRuleOfMachine(app, ip, port, rules); | |||
return sentinelApiClient.setSystemRuleOfMachine(app, ip, port, rules); | |||
} | |||
} |