Commit ac180699 by Nepxion

支持Alibaba Sentinel

parent 61bf9355
......@@ -83,6 +83,11 @@
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>
......
......@@ -13,16 +13,21 @@ import java.util.Collections;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.alibaba.sentinel.annotation.SentinelProtect;
import org.springframework.cloud.alibaba.sentinel.datasource.annotation.SentinelDataSource;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
import com.nepxion.discovery.plugin.example.service.impl.MyDiscoveryEnabledStrategy;
import com.nepxion.discovery.plugin.example.service.impl.MyDiscoveryListener;
import com.nepxion.discovery.plugin.example.service.impl.MyLoadBalanceListener;
import com.nepxion.discovery.plugin.example.service.impl.MyRegisterListener;
import com.nepxion.discovery.plugin.example.service.impl.MySentinelExceptionHandler;
import com.nepxion.discovery.plugin.example.service.impl.MySentinelFlowRuleParser;
import com.nepxion.discovery.plugin.example.service.impl.MySubscriber;
import com.nepxion.discovery.plugin.strategy.service.aop.RestTemplateStrategyInterceptor;
......@@ -36,8 +41,12 @@ public class DiscoveryApplicationA1 {
new SpringApplicationBuilder(DiscoveryApplicationA1.class).run(args);
}
@SentinelDataSource("spring.cloud.sentinel.datasource")
protected ReadableDataSource dataSource;
@Bean
@LoadBalanced
@SentinelProtect(blockHandler = "handleException", blockHandlerClass = MySentinelExceptionHandler.class)
public RestTemplate restTemplate(RestTemplateStrategyInterceptor restTemplateStrategyInterceptor) {
RestTemplate restTemplate = new RestTemplate();
restTemplate.setInterceptors(Collections.singletonList(restTemplateStrategyInterceptor));
......@@ -46,6 +55,11 @@ public class DiscoveryApplicationA1 {
}
@Bean
public MySentinelFlowRuleParser mySentinelFlowRuleParser() {
return new MySentinelFlowRuleParser();
}
@Bean
public MyRegisterListener myRegisterListener() {
return new MyRegisterListener();
}
......
package com.nepxion.discovery.plugin.example.service.impl;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.csp.sentinel.slots.block.BlockException;
public class MySentinelExceptionHandler {
private static final Logger LOG = LoggerFactory.getLogger(MySentinelExceptionHandler.class);
public static void handleException(BlockException e) {
LOG.error("Sentinel exception causes", e);
}
}
\ No newline at end of file
package com.nepxion.discovery.plugin.example.service.impl;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
import java.util.List;
import com.alibaba.csp.sentinel.datasource.Converter;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
public class MySentinelFlowRuleParser implements Converter<String, List<FlowRule>> {
@Override
public List<FlowRule> convert(String source) {
return JSON.parseObject(source, new TypeReference<List<FlowRule>>() {
});
}
}
\ No newline at end of file
......@@ -19,6 +19,7 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.nepxion.discovery.common.constant.DiscoveryConstant;
@RestController
......@@ -30,6 +31,7 @@ public class ARestImpl extends AbstractRestImpl {
private RestTemplate restTemplate;
@RequestMapping(path = "/rest", method = RequestMethod.POST)
@SentinelResource("sentinel-resource")
public String rest(@RequestBody String value) {
value = doRest(value);
value = restTemplate.postForEntity("http://discovery-springcloud-example-b/rest", value, String.class).getBody();
......
......@@ -19,6 +19,7 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.nepxion.discovery.common.constant.DiscoveryConstant;
@RestController
......@@ -30,6 +31,7 @@ public class BRestImpl extends AbstractRestImpl {
private RestTemplate restTemplate;
@RequestMapping(path = "/rest", method = RequestMethod.POST)
@SentinelResource("sentinel-resource")
public String rest(@RequestBody String value) {
value = doRest(value);
value = restTemplate.postForEntity("http://discovery-springcloud-example-c/rest", value, String.class).getBody();
......
......@@ -17,6 +17,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.nepxion.discovery.common.constant.DiscoveryConstant;
@RestController
......@@ -25,6 +26,7 @@ public class CRestImpl extends AbstractRestImpl {
private static final Logger LOG = LoggerFactory.getLogger(CRestImpl.class);
@RequestMapping(path = "/rest", method = RequestMethod.POST)
@SentinelResource("sentinel-resource")
public String rest(@RequestBody String value) {
value = doRest(value);
......
......@@ -20,5 +20,8 @@ spring.cloud.nacos.discovery.metadata.version=1.0
spring.cloud.nacos.discovery.metadata.group=example-service-group
spring.cloud.nacos.discovery.metadata.region=dev
# Sentinel config
spring.cloud.sentinel.transport.port=4100
# Admin config
management.server.port=5100
\ No newline at end of file
......@@ -20,5 +20,8 @@ spring.cloud.nacos.discovery.metadata.version=1.1
spring.cloud.nacos.discovery.metadata.group=example-service-group
spring.cloud.nacos.discovery.metadata.region=qa
# Sentinel config
spring.cloud.sentinel.transport.port=4101
# Admin config
management.server.port=5101
\ No newline at end of file
......@@ -20,5 +20,8 @@ spring.cloud.nacos.discovery.metadata.version=1.0
spring.cloud.nacos.discovery.metadata.group=example-service-group
spring.cloud.nacos.discovery.metadata.region=dev
# Sentinel config
spring.cloud.sentinel.transport.port=4200
# Admin config
management.server.port=5200
\ No newline at end of file
......@@ -20,5 +20,8 @@ spring.cloud.nacos.discovery.metadata.version=1.1
spring.cloud.nacos.discovery.metadata.group=example-service-group
spring.cloud.nacos.discovery.metadata.region=qa
# Sentinel config
spring.cloud.sentinel.transport.port=4201
# Admin config
management.server.port=5201
\ No newline at end of file
......@@ -20,5 +20,8 @@ spring.cloud.nacos.discovery.metadata.version=1.0
spring.cloud.nacos.discovery.metadata.group=example-service-group
spring.cloud.nacos.discovery.metadata.region=dev
# Sentinel config
spring.cloud.sentinel.transport.port=4300
# Admin config
management.server.port=5300
\ No newline at end of file
......@@ -20,5 +20,8 @@ spring.cloud.nacos.discovery.metadata.version=1.1
spring.cloud.nacos.discovery.metadata.group=example-service-group
spring.cloud.nacos.discovery.metadata.region=qa
# Sentinel config
spring.cloud.sentinel.transport.port=4301
# Admin config
management.server.port=5301
\ No newline at end of file
......@@ -20,5 +20,8 @@ spring.cloud.nacos.discovery.metadata.version=1.2
spring.cloud.nacos.discovery.metadata.group=example-service-group
spring.cloud.nacos.discovery.metadata.region=qa
# Sentinel config
spring.cloud.sentinel.transport.port=4302
# Admin config
management.server.port=5302
\ No newline at end of file
......@@ -43,6 +43,15 @@ spring.redis.pool.max-wait=-1
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
# Sentinel config
spring.cloud.sentinel.transport.dashboard=localhost:8075
spring.cloud.sentinel.datasource.type=file
spring.cloud.sentinel.datasource.recommendRefreshMs=3000
spring.cloud.sentinel.datasource.bufSize=4056196
spring.cloud.sentinel.datasource.charset=utf-8
spring.cloud.sentinel.datasource.converter=mySentinelFlowRuleParser
spring.cloud.sentinel.datasource.file=E://sentinel-rule.json
# Admin config
# 该项只对Consul有效,而且必须配置在bootstrap.properties里,配置在application.properties无效
# management.health.consul.enabled=true
......
[
{
"resource": "sentinel-resource",
"controlBehavior": 0,
"count": 5.0,
"grade": 1,
"limitApp": "default",
"strategy": 0
}
]
\ No newline at end of file
......@@ -60,7 +60,7 @@
<caffeine.version>2.6.2</caffeine.version>
<swagger.version>2.7.0</swagger.version>
<spring.cloud.version>Finchley.SR2</spring.cloud.version>
<spring.cloud.alibaba.version>0.2.0.RELEASE</spring.cloud.alibaba.version>
<spring.cloud.alibaba.version>0.2.1.BUILD-SNAPSHOT</spring.cloud.alibaba.version>
<spring.boot.version>2.0.3.RELEASE</spring.boot.version>
<disruptor.version>3.3.7</disruptor.version>
<java.version>1.8</java.version>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment