forked from macrozheng/springcloud-learning
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d2075e4
commit ddd1f86
Showing
15 changed files
with
539 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
HELP.md | ||
target/ | ||
!.mvn/wrapper/maven-wrapper.jar | ||
!**/src/main/** | ||
!**/src/test/** | ||
|
||
### STS ### | ||
.apt_generated | ||
.classpath | ||
.factorypath | ||
.project | ||
.settings | ||
.springBeans | ||
.sts4-cache | ||
|
||
### IntelliJ IDEA ### | ||
.idea | ||
*.iws | ||
*.iml | ||
*.ipr | ||
|
||
### NetBeans ### | ||
/nbproject/private/ | ||
/nbbuild/ | ||
/dist/ | ||
/nbdist/ | ||
/.nb-gradle/ | ||
build/ | ||
|
||
### VS Code ### | ||
.vscode/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.1.7.RELEASE</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
<groupId>com.macro.cloud</groupId> | ||
<artifactId>sentinel-service</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>sentinel-service</name> | ||
<description>Demo project for Spring Boot</description> | ||
|
||
<properties> | ||
<java.version>1.8</java.version> | ||
<spring-cloud.version>Greenwich.SR2</spring-cloud.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.alibaba.cloud</groupId> | ||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.alibaba.cloud</groupId> | ||
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-starter-openfeign</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.alibaba.csp</groupId> | ||
<artifactId>sentinel-datasource-nacos</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-actuator</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>cn.hutool</groupId> | ||
<artifactId>hutool-all</artifactId> | ||
<version>4.6.3</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-dependencies</artifactId> | ||
<version>${spring-cloud.version}</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.alibaba.cloud</groupId> | ||
<artifactId>spring-cloud-alibaba-dependencies</artifactId> | ||
<version>2.1.0.RELEASE</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
18 changes: 18 additions & 0 deletions
18
sentinel-service/src/main/java/com/macro/cloud/SentinelServiceApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.macro.cloud; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; | ||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; | ||
import org.springframework.cloud.openfeign.EnableFeignClients; | ||
|
||
@EnableFeignClients | ||
@EnableDiscoveryClient | ||
@SpringBootApplication | ||
public class SentinelServiceApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(SentinelServiceApplication.class, args); | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
sentinel-service/src/main/java/com/macro/cloud/config/RibbonConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.macro.cloud.config; | ||
|
||
import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate; | ||
import org.springframework.cloud.client.loadbalancer.LoadBalanced; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
/** | ||
* Created by macro on 2019/8/29. | ||
*/ | ||
@Configuration | ||
public class RibbonConfig { | ||
|
||
@Bean | ||
@SentinelRestTemplate | ||
public RestTemplate restTemplate(){ | ||
return new RestTemplate(); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
sentinel-service/src/main/java/com/macro/cloud/controller/CircleBreakerController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.macro.cloud.controller; | ||
|
||
import com.alibaba.csp.sentinel.annotation.SentinelResource; | ||
import com.macro.cloud.domain.CommonResult; | ||
import com.macro.cloud.domain.User; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
/** | ||
* 熔断功能 | ||
* Created by macro on 2019/11/7. | ||
*/ | ||
@RestController | ||
@RequestMapping("/breaker") | ||
public class CircleBreakerController { | ||
|
||
private Logger LOGGER = LoggerFactory.getLogger(CircleBreakerController.class); | ||
@Autowired | ||
private RestTemplate restTemplate; | ||
@Value("${service-url.user-service}") | ||
private String userServiceUrl; | ||
|
||
@RequestMapping("/fallback/{id}") | ||
@SentinelResource(value = "fallback",fallback = "handleFallback") | ||
public CommonResult fallback(@PathVariable Long id) { | ||
return restTemplate.getForObject(userServiceUrl + "/user/{1}", CommonResult.class, id); | ||
} | ||
|
||
@RequestMapping("/fallbackException/{id}") | ||
@SentinelResource(value = "fallbackException",fallback = "handleFallback2", exceptionsToIgnore = {NullPointerException.class}) | ||
public CommonResult fallbackException(@PathVariable Long id) { | ||
if (id == 1) { | ||
throw new IndexOutOfBoundsException(); | ||
} else if (id == 2) { | ||
throw new NullPointerException(); | ||
} | ||
return restTemplate.getForObject(userServiceUrl + "/user/{1}", CommonResult.class, id); | ||
} | ||
|
||
public CommonResult handleFallback(Long id) { | ||
User defaultUser = new User(-1L, "defaultUser", "123456"); | ||
return new CommonResult<>(defaultUser,"服务降级返回",200); | ||
} | ||
|
||
public CommonResult handleFallback2(@PathVariable Long id, Throwable e) { | ||
LOGGER.error("handleFallback2 id:{},throwable class:{}", id, e.getClass()); | ||
User defaultUser = new User(-2L, "defaultUser2", "123456"); | ||
return new CommonResult<>(defaultUser,"服务降级返回",200); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
sentinel-service/src/main/java/com/macro/cloud/controller/RateLimitController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.macro.cloud.controller; | ||
|
||
import com.alibaba.csp.sentinel.annotation.SentinelResource; | ||
import com.alibaba.csp.sentinel.slots.block.BlockException; | ||
import com.macro.cloud.domain.CommonResult; | ||
import com.macro.cloud.handler.CustomBlockHandler; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
/** | ||
* 限流功能 | ||
* Created by macro on 2019/11/7. | ||
*/ | ||
@RestController | ||
@RequestMapping("/rateLimit") | ||
public class RateLimitController { | ||
|
||
/** | ||
* 按资源名称限流,需要指定限流处理逻辑 | ||
*/ | ||
@GetMapping("/byResource") | ||
@SentinelResource(value = "byResource",blockHandler = "handleException") | ||
public CommonResult byResource() { | ||
return new CommonResult("按资源名称限流", 200); | ||
} | ||
|
||
/** | ||
* 按URL限流,有默认的限流处理逻辑 | ||
*/ | ||
@GetMapping("/byUrl") | ||
@SentinelResource(value = "byUrl",blockHandler = "handleException") | ||
public CommonResult byUrl() { | ||
return new CommonResult("按url限流", 200); | ||
} | ||
|
||
/** | ||
* 自定义通用的限流处理逻辑 | ||
*/ | ||
@GetMapping("/customBlockHandler") | ||
@SentinelResource(value = "customBlockHandler", blockHandler = "handleException",blockHandlerClass = CustomBlockHandler.class) | ||
public CommonResult blockHandler() { | ||
return new CommonResult("限流成功", 200); | ||
} | ||
|
||
public CommonResult handleException(BlockException exception){ | ||
return new CommonResult(exception.getClass().getCanonicalName(),200); | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
sentinel-service/src/main/java/com/macro/cloud/controller/UserFeignController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.macro.cloud.controller; | ||
|
||
import com.macro.cloud.domain.CommonResult; | ||
import com.macro.cloud.domain.User; | ||
import com.macro.cloud.service.UserService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
/** | ||
* Created by macro on 2019/8/29. | ||
*/ | ||
@RestController | ||
@RequestMapping("/user") | ||
public class UserFeignController { | ||
@Autowired | ||
private UserService userService; | ||
|
||
@GetMapping("/{id}") | ||
public CommonResult getUser(@PathVariable Long id) { | ||
return userService.getUser(id); | ||
} | ||
|
||
@GetMapping("/getByUsername") | ||
public CommonResult getByUsername(@RequestParam String username) { | ||
return userService.getByUsername(username); | ||
} | ||
|
||
@PostMapping("/create") | ||
public CommonResult create(@RequestBody User user) { | ||
return userService.create(user); | ||
} | ||
|
||
@PostMapping("/update") | ||
public CommonResult update(@RequestBody User user) { | ||
return userService.update(user); | ||
} | ||
|
||
@PostMapping("/delete/{id}") | ||
public CommonResult delete(@PathVariable Long id) { | ||
return userService.delete(id); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
sentinel-service/src/main/java/com/macro/cloud/domain/CommonResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.macro.cloud.domain; | ||
|
||
/** | ||
* Created by macro on 2019/8/29. | ||
*/ | ||
public class CommonResult<T> { | ||
private T data; | ||
private String message; | ||
private Integer code; | ||
|
||
public CommonResult() { | ||
} | ||
|
||
public CommonResult(T data, String message, Integer code) { | ||
this.data = data; | ||
this.message = message; | ||
this.code = code; | ||
} | ||
|
||
public CommonResult(String message, Integer code) { | ||
this(null, message, code); | ||
} | ||
|
||
public CommonResult(T data) { | ||
this(data, "操作成功", 200); | ||
} | ||
|
||
public T getData() { | ||
return data; | ||
} | ||
|
||
public void setData(T data) { | ||
this.data = data; | ||
} | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
|
||
public void setMessage(String message) { | ||
this.message = message; | ||
} | ||
|
||
public Integer getCode() { | ||
return code; | ||
} | ||
|
||
public void setCode(Integer code) { | ||
this.code = code; | ||
} | ||
} |
Oops, something went wrong.