1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00

update sentinel example

This commit is contained in:
fangjian0423 2018-08-09 10:53:50 +08:00
parent 8142f3f4d7
commit 1a0bfc23cd
3 changed files with 38 additions and 2 deletions

View File

@ -0,0 +1,14 @@
package org.springframework.cloud.alibaba.cloud.examples;
import com.alibaba.csp.sentinel.slots.block.BlockException;
/**
* @author fangjian
*/
public class ExceptionUtil {
public static void handleException(BlockException ex) {
System.out.println("Oops: " + ex.getClass().getCanonicalName());
}
}

View File

@ -2,6 +2,9 @@ package org.springframework.cloud.alibaba.cloud.examples;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.alibaba.sentinel.annotation.SentinelProtect;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/** /**
* @author xiaojing * @author xiaojing
@ -9,6 +12,17 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication @SpringBootApplication
public class ServiceApplication { public class ServiceApplication {
@Bean
@SentinelProtect(blockHandler = "handleException", blockHandlerClass = ExceptionUtil.class)
public RestTemplate restTemplate() {
return new RestTemplate();
}
@Bean
public RestTemplate restTemplate2() {
return new RestTemplate();
}
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(ServiceApplication.class, args); SpringApplication.run(ServiceApplication.class, args);
} }

View File

@ -1,9 +1,10 @@
package org.springframework.cloud.alibaba.cloud.examples; package org.springframework.cloud.alibaba.cloud.examples;
import org.springframework.cloud.alibaba.sentinel.custom.EnableSentinel; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/** /**
* @author xiaojing * @author xiaojing
@ -11,8 +12,10 @@ import org.springframework.web.bind.annotation.RestController;
@RestController @RestController
public class TestController { public class TestController {
@Autowired
private RestTemplate restTemplate;
@RequestMapping(value = "/hello", method = RequestMethod.GET) @RequestMapping(value = "/hello", method = RequestMethod.GET)
@EnableSentinel("resource")
public String hello() { public String hello() {
return "Hello"; return "Hello";
} }
@ -22,4 +25,9 @@ public class TestController {
return "Hello test"; return "Hello test";
} }
@RequestMapping(value = "/template", method = RequestMethod.GET)
public String client() {
return restTemplate.getForObject("http://www.taobao.com/test", String.class);
}
} }