nacos集成Sentinel、gateway

忘是亡心i 2023-06-24 08:09 100阅读 0赞

(一)maven依赖

(1)父项目依赖

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.alibaba.cloud</groupId>
  8. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>com.alibaba.cloud</groupId>
  12. <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
  13. </dependency>
  14. <dependency>
  15. <groupId>com.alibaba.cloud</groupId>
  16. <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
  17. </dependency>
  18. </dependencies>
  19. <dependencyManagement>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.cloud</groupId>
  23. <artifactId>spring-cloud-dependencies</artifactId>
  24. <version>Greenwich.RELEASE</version>
  25. <type>pom</type>
  26. <scope>import</scope>
  27. </dependency>
  28. <!--<dependency>
  29. <groupId>org.springframework.cloud</groupId>
  30. <artifactId>spring-cloud-alibaba-dependencies</artifactId>
  31. <version>0.9.0.RELEASE</version>
  32. <type>pom</type>
  33. <scope>import</scope>
  34. </dependency>-->
  35. <dependency>
  36. <groupId>com.alibaba.cloud</groupId>
  37. <artifactId>spring-cloud-alibaba-dependencies</artifactId>
  38. <version>2.1.1.RELEASE</version>
  39. <type>pom</type>
  40. <scope>import</scope>
  41. </dependency>
  42. </dependencies>
  43. </dependencyManagement>

(2)子项目service-privoder依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>com.alibaba.csp</groupId>
  7. <artifactId>sentinel-datasource-nacos</artifactId>
  8. <version>1.6.0</version>
  9. </dependency>

(3)网关依赖

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-gateway</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>com.alibaba.cloud</groupId>
  7. <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
  8. </dependency>

(二)注册服务service-privoder到nacos并整合sentinel进行限流

(1)配置文件

1.bootstrap.yml配置

  1. spring:
  2. application:
  3. name: privoder-service
  4. cloud:
  5. nacos:
  6. config:
  7. server-addr: 127.0.0.1:8848
  8. discovery:
  9. server-addr: 127.0.0.1:8848
  10. sentinel:
  11. eager: true
  12. transport:
  13. dashboard: localhost:8080
  14. datasource:
  15. ds1:
  16. nacos:
  17. server-addr: 127.0.0.1:8848
  18. dataId: ${spring.application.name}-flow-rules
  19. data-type: json
  20. rule-type: flow

2.application.yml配置

  1. spring:
  2. profiles:
  3. active: dev
  4. server:
  5. port: 8082
  6. sleep: 1000

(2)添加发现微服务注解

  1. @EnableDiscoveryClient
  2. @SpringBootApplication
  3. public class PrivoderApp {
  4. public static void main(String[] args) {
  5. SpringApplication.run(PrivoderApp.class, args);
  6. }
  7. }

(3)数据实体类

  1. public class Balance {
  2. private int id;
  3. private int diamond;
  4. private int ticket;
  5. private String message;
  6. public Balance() {
  7. }
  8. public int getId() {
  9. return id;
  10. }
  11. public Balance(int id, int diamond, int ticket) {
  12. this(id, diamond, ticket, "OK");
  13. }
  14. public Balance(int id, int diamond, int ticket, String message) {
  15. this.id = id;
  16. this.diamond = diamond;
  17. this.ticket = ticket;
  18. this.message = message;
  19. }
  20. public void setId(int id) {
  21. this.id = id;
  22. }
  23. public int getDiamond() {
  24. return diamond;
  25. }
  26. public void setDiamond(int diamond) {
  27. this.diamond = diamond;
  28. }
  29. public int getTicket() {
  30. return ticket;
  31. }
  32. public void setTicket(int ticket) {
  33. this.ticket = ticket;
  34. }
  35. public String getMessage() {
  36. return message;
  37. }
  38. public void setMessage(String message) {
  39. this.message = message;
  40. }
  41. }

(4)控制层

  1. @RestController
  2. @RefreshScope
  3. public class BalanceController {
  4. @Value("${sleep:0}")
  5. private int sleep;
  6. final static Map<Integer, Balance> balanceMap = new HashMap() {
  7. {
  8. put(1, new Balance(1, 10, 1000));
  9. put(2, new Balance(2, 0, 10000));
  10. put(3, new Balance(3, 100, 0));
  11. }
  12. };
  13. @RequestMapping("/service")
  14. @SentinelResource(value = "protected-resource", blockHandler = "handleBlock")
  15. public Balance getBalance(Integer id) {
  16. System.out.println("request: /service?id=" + id + ", sleep: " + sleep);
  17. if (sleep > 0) {
  18. try {
  19. Thread.sleep(sleep);
  20. } catch (InterruptedException e) {
  21. e.printStackTrace();
  22. }
  23. }
  24. if (id != null && balanceMap.containsKey(id)) {
  25. return balanceMap.get(id);
  26. }
  27. return new Balance(0, 0, 0, "不存在");
  28. }
  29. public Balance handleBlock(Integer id, BlockException e) {
  30. return new Balance(0, 0, 0, "限流");
  31. }
  32. }

(5)nacos添加配置文件

Data ID:privoder-service-flow-rules
格式: json

  1. [
  2. {
  3. "resource": "protected-resource",
  4. "controlBehavior": 2,
  5. "count": 1,
  6. "grade": 1,
  7. "limitApp": "default",
  8. "strategy": 0
  9. }
  10. ]

(6)访问服务

访问localhost:8012/service?id=1
不断的刷新访问会出现限流提示

(三)网关

(1)application.yml配置

  1. server:
  2. port: 8010
  3. spring:
  4. application:
  5. name: gateway-server
  6. cloud:
  7. nacos:
  8. discovery:
  9. server-addr: 127.0.0.1:8848
  10. gateway:
  11. enabled: true
  12. discovery:
  13. locator:
  14. lower-case-service-id: true
  15. routes:
  16. # Add your routes here.
  17. - id: college_route
  18. uri: lb://provider-service
  19. predicates:
  20. - Path=/provider/**
  21. sentinel:
  22. transport:
  23. dashboard: localhost:8011
  24. datasource.ds1.file:
  25. file: "classpath: gateway.json"
  26. ruleType: gw-flow

(2)添加发现微服务注解

  1. @EnableDiscoveryClient
  2. @SpringBootApplication
  3. public class GatewayApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(GatewayApplication.class, args);
  6. }
  7. }

(3)resource添加gateway.json

  1. [
  2. {
  3. "resource": "college_route",
  4. "count": 2
  5. }
  6. ]

发表评论

表情:
评论列表 (有 0 条评论,100人围观)

还没有评论,来说两句吧...

相关阅读

    相关 SpringBoot集成Nacos

    目录 简介 依赖 配置文件 代码 测试 简介: Nacos 是阿里巴巴的新开源项目,其核心定位是 “一个更易于帮助构建云原生应用的集注册中心与配置中心于一体的