Skip to content

Commit ddd1f86

Browse files
committed
add sentinel-service
1 parent d2075e4 commit ddd1f86

File tree

15 files changed

+539
-1
lines changed

15 files changed

+539
-1
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
- [Spring Cloud Consul:服务治理与配置中心](https://juejin.im/post/5db05582f265da4d4c20180f)
2626
- [Spring Cloud Gateway:新一代API网关服务](https://juejin.im/post/5db6eed6518825644076d0b6)
2727
- [Spring Boot Admin:微服务应用监控](https://juejin.im/post/5db98a2d518825649c730f81)
28+
- [Spring Cloud Security:Oauth2使用入门](https://juejin.im/post/5dc013bae51d456e817cec30)
29+
- [Spring Cloud Security:Oauth2结合JWT使用](https://juejin.im/post/5dc2bec6f265da4d4f65bebe)
30+
- [Spring Cloud Security:Oauth2实现单点登录](https://juejin.im/post/5dc95a675188256e040db43f)
31+
- [Spring Cloud Alibaba:Nacos 作为注册中心和配置中心使用](https://juejin.im/post/5dcbf7bc5188250d1f5a78ea)
2832

2933
## 项目结构
3034

@@ -49,7 +53,13 @@ springcloud-learning
4953
├── api-gateway -- gateway作为网关的测试服务
5054
├── admin-server -- admin监控中心服务
5155
├── admin-client -- admin监控中心监控的应用服务
52-
└── admin-security-server -- 带登录认证的admin监控中心服务
56+
├── admin-security-server -- 带登录认证的admin监控中心服务
57+
├── oauth2-server -- oauth2认证测试服务
58+
├── oauth2-jwt-server -- 使用jwt的oauth2认证测试服务
59+
├── oauth2-client -- 单点登录的oauth2客户端服务
60+
├── nacos-config-client -- 用于演示nacos作为配置中心的nacos客户端
61+
├── nacos-user-service -- 注册到nacos的提供User对象CRUD接口的服务
62+
└── nacos-ribbon-service -- 注册到nacos的ribbon服务调用测试服务
5363
```
5464

5565
## 公众号

sentinel-service/.gitignore

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
HELP.md
2+
target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
!**/src/main/**
5+
!**/src/test/**
6+
7+
### STS ###
8+
.apt_generated
9+
.classpath
10+
.factorypath
11+
.project
12+
.settings
13+
.springBeans
14+
.sts4-cache
15+
16+
### IntelliJ IDEA ###
17+
.idea
18+
*.iws
19+
*.iml
20+
*.ipr
21+
22+
### NetBeans ###
23+
/nbproject/private/
24+
/nbbuild/
25+
/dist/
26+
/nbdist/
27+
/.nb-gradle/
28+
build/
29+
30+
### VS Code ###
31+
.vscode/

sentinel-service/pom.xml

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.1.7.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>com.macro.cloud</groupId>
12+
<artifactId>sentinel-service</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>sentinel-service</name>
15+
<description>Demo project for Spring Boot</description>
16+
17+
<properties>
18+
<java.version>1.8</java.version>
19+
<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
20+
</properties>
21+
22+
<dependencies>
23+
<dependency>
24+
<groupId>com.alibaba.cloud</groupId>
25+
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
26+
</dependency>
27+
<dependency>
28+
<groupId>com.alibaba.cloud</groupId>
29+
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.springframework.cloud</groupId>
33+
<artifactId>spring-cloud-starter-openfeign</artifactId>
34+
</dependency>
35+
<dependency>
36+
<groupId>com.alibaba.csp</groupId>
37+
<artifactId>sentinel-datasource-nacos</artifactId>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-starter-actuator</artifactId>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-starter-web</artifactId>
46+
</dependency>
47+
<dependency>
48+
<groupId>org.springframework.boot</groupId>
49+
<artifactId>spring-boot-starter-test</artifactId>
50+
<scope>test</scope>
51+
</dependency>
52+
<dependency>
53+
<groupId>cn.hutool</groupId>
54+
<artifactId>hutool-all</artifactId>
55+
<version>4.6.3</version>
56+
</dependency>
57+
</dependencies>
58+
59+
<dependencyManagement>
60+
<dependencies>
61+
<dependency>
62+
<groupId>org.springframework.cloud</groupId>
63+
<artifactId>spring-cloud-dependencies</artifactId>
64+
<version>${spring-cloud.version}</version>
65+
<type>pom</type>
66+
<scope>import</scope>
67+
</dependency>
68+
<dependency>
69+
<groupId>com.alibaba.cloud</groupId>
70+
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
71+
<version>2.1.0.RELEASE</version>
72+
<type>pom</type>
73+
<scope>import</scope>
74+
</dependency>
75+
</dependencies>
76+
</dependencyManagement>
77+
78+
<build>
79+
<plugins>
80+
<plugin>
81+
<groupId>org.springframework.boot</groupId>
82+
<artifactId>spring-boot-maven-plugin</artifactId>
83+
</plugin>
84+
</plugins>
85+
</build>
86+
87+
</project>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.macro.cloud;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
6+
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
7+
import org.springframework.cloud.openfeign.EnableFeignClients;
8+
9+
@EnableFeignClients
10+
@EnableDiscoveryClient
11+
@SpringBootApplication
12+
public class SentinelServiceApplication {
13+
14+
public static void main(String[] args) {
15+
SpringApplication.run(SentinelServiceApplication.class, args);
16+
}
17+
18+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.macro.cloud.config;
2+
3+
import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate;
4+
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.web.client.RestTemplate;
8+
9+
/**
10+
* Created by macro on 2019/8/29.
11+
*/
12+
@Configuration
13+
public class RibbonConfig {
14+
15+
@Bean
16+
@SentinelRestTemplate
17+
public RestTemplate restTemplate(){
18+
return new RestTemplate();
19+
}
20+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.macro.cloud.controller;
2+
3+
import com.alibaba.csp.sentinel.annotation.SentinelResource;
4+
import com.macro.cloud.domain.CommonResult;
5+
import com.macro.cloud.domain.User;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.beans.factory.annotation.Value;
10+
import org.springframework.web.bind.annotation.PathVariable;
11+
import org.springframework.web.bind.annotation.RequestMapping;
12+
import org.springframework.web.bind.annotation.RestController;
13+
import org.springframework.web.client.RestTemplate;
14+
15+
/**
16+
* 熔断功能
17+
* Created by macro on 2019/11/7.
18+
*/
19+
@RestController
20+
@RequestMapping("/breaker")
21+
public class CircleBreakerController {
22+
23+
private Logger LOGGER = LoggerFactory.getLogger(CircleBreakerController.class);
24+
@Autowired
25+
private RestTemplate restTemplate;
26+
@Value("${service-url.user-service}")
27+
private String userServiceUrl;
28+
29+
@RequestMapping("/fallback/{id}")
30+
@SentinelResource(value = "fallback",fallback = "handleFallback")
31+
public CommonResult fallback(@PathVariable Long id) {
32+
return restTemplate.getForObject(userServiceUrl + "/user/{1}", CommonResult.class, id);
33+
}
34+
35+
@RequestMapping("/fallbackException/{id}")
36+
@SentinelResource(value = "fallbackException",fallback = "handleFallback2", exceptionsToIgnore = {NullPointerException.class})
37+
public CommonResult fallbackException(@PathVariable Long id) {
38+
if (id == 1) {
39+
throw new IndexOutOfBoundsException();
40+
} else if (id == 2) {
41+
throw new NullPointerException();
42+
}
43+
return restTemplate.getForObject(userServiceUrl + "/user/{1}", CommonResult.class, id);
44+
}
45+
46+
public CommonResult handleFallback(Long id) {
47+
User defaultUser = new User(-1L, "defaultUser", "123456");
48+
return new CommonResult<>(defaultUser,"服务降级返回",200);
49+
}
50+
51+
public CommonResult handleFallback2(@PathVariable Long id, Throwable e) {
52+
LOGGER.error("handleFallback2 id:{},throwable class:{}", id, e.getClass());
53+
User defaultUser = new User(-2L, "defaultUser2", "123456");
54+
return new CommonResult<>(defaultUser,"服务降级返回",200);
55+
}
56+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.macro.cloud.controller;
2+
3+
import com.alibaba.csp.sentinel.annotation.SentinelResource;
4+
import com.alibaba.csp.sentinel.slots.block.BlockException;
5+
import com.macro.cloud.domain.CommonResult;
6+
import com.macro.cloud.handler.CustomBlockHandler;
7+
import org.springframework.web.bind.annotation.GetMapping;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
/**
12+
* 限流功能
13+
* Created by macro on 2019/11/7.
14+
*/
15+
@RestController
16+
@RequestMapping("/rateLimit")
17+
public class RateLimitController {
18+
19+
/**
20+
* 按资源名称限流,需要指定限流处理逻辑
21+
*/
22+
@GetMapping("/byResource")
23+
@SentinelResource(value = "byResource",blockHandler = "handleException")
24+
public CommonResult byResource() {
25+
return new CommonResult("按资源名称限流", 200);
26+
}
27+
28+
/**
29+
* 按URL限流,有默认的限流处理逻辑
30+
*/
31+
@GetMapping("/byUrl")
32+
@SentinelResource(value = "byUrl",blockHandler = "handleException")
33+
public CommonResult byUrl() {
34+
return new CommonResult("按url限流", 200);
35+
}
36+
37+
/**
38+
* 自定义通用的限流处理逻辑
39+
*/
40+
@GetMapping("/customBlockHandler")
41+
@SentinelResource(value = "customBlockHandler", blockHandler = "handleException",blockHandlerClass = CustomBlockHandler.class)
42+
public CommonResult blockHandler() {
43+
return new CommonResult("限流成功", 200);
44+
}
45+
46+
public CommonResult handleException(BlockException exception){
47+
return new CommonResult(exception.getClass().getCanonicalName(),200);
48+
}
49+
50+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.macro.cloud.controller;
2+
3+
import com.macro.cloud.domain.CommonResult;
4+
import com.macro.cloud.domain.User;
5+
import com.macro.cloud.service.UserService;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.web.bind.annotation.*;
8+
9+
/**
10+
* Created by macro on 2019/8/29.
11+
*/
12+
@RestController
13+
@RequestMapping("/user")
14+
public class UserFeignController {
15+
@Autowired
16+
private UserService userService;
17+
18+
@GetMapping("/{id}")
19+
public CommonResult getUser(@PathVariable Long id) {
20+
return userService.getUser(id);
21+
}
22+
23+
@GetMapping("/getByUsername")
24+
public CommonResult getByUsername(@RequestParam String username) {
25+
return userService.getByUsername(username);
26+
}
27+
28+
@PostMapping("/create")
29+
public CommonResult create(@RequestBody User user) {
30+
return userService.create(user);
31+
}
32+
33+
@PostMapping("/update")
34+
public CommonResult update(@RequestBody User user) {
35+
return userService.update(user);
36+
}
37+
38+
@PostMapping("/delete/{id}")
39+
public CommonResult delete(@PathVariable Long id) {
40+
return userService.delete(id);
41+
}
42+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.macro.cloud.domain;
2+
3+
/**
4+
* Created by macro on 2019/8/29.
5+
*/
6+
public class CommonResult<T> {
7+
private T data;
8+
private String message;
9+
private Integer code;
10+
11+
public CommonResult() {
12+
}
13+
14+
public CommonResult(T data, String message, Integer code) {
15+
this.data = data;
16+
this.message = message;
17+
this.code = code;
18+
}
19+
20+
public CommonResult(String message, Integer code) {
21+
this(null, message, code);
22+
}
23+
24+
public CommonResult(T data) {
25+
this(data, "操作成功", 200);
26+
}
27+
28+
public T getData() {
29+
return data;
30+
}
31+
32+
public void setData(T data) {
33+
this.data = data;
34+
}
35+
36+
public String getMessage() {
37+
return message;
38+
}
39+
40+
public void setMessage(String message) {
41+
this.message = message;
42+
}
43+
44+
public Integer getCode() {
45+
return code;
46+
}
47+
48+
public void setCode(Integer code) {
49+
this.code = code;
50+
}
51+
}

0 commit comments

Comments
 (0)