SpringCloud学习笔记----SpringCloud和SpringCloud Alibaba Sentinel的整合

381 阅读2分钟

当前环境 是已经创建了一个服务提供者 一个服务消费者 并配置了nacos注册中心 利用Openfeign实现了远程微服务调用

安装Sentinel控制台并启动 java -jar jar包名称

image.png

image.png

默认是 http:locahost:8080 账号和密码都是:sentinel 因为是懒加载所以需要访问一次服务提供者的接口 才能在控制台看到此服务 目前我已经加载过了

image.png

创建服务提供者

maven添加依赖

<dependencies>

<!--        Nacos  注册中心相关依赖-->
     <dependency>
         <groupId>com.alibaba.cloud</groupId>
         <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
     </dependency>
     <dependency>
         <groupId>com.alibaba.csp</groupId>
         <artifactId>sentinel-datasource-nacos</artifactId>
     </dependency>

<!--        Sentinel 相关依赖-->
     <dependency>
         <groupId>com.alibaba.cloud</groupId>
         <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
     </dependency>

<!--      Openfeign  远程调用相关依赖-->
     <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-openfeign</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-actuator</artifactId>
     </dependency>
     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-devtools</artifactId>
         <scope>runtime</scope>
         <optional>true</optional>
     </dependency>
     <dependency>
         <groupId>cn.hutool</groupId>
         <artifactId>hutool-all</artifactId>
         <version>4.6.3</version>
     </dependency>
     <dependency>
         <groupId>org.projectlombok</groupId>
         <artifactId>lombok</artifactId>
         <optional>true</optional>
     </dependency>
     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
     </dependency>
 </dependencies>

application.yml配置连接nacos注册中心和sentinel相关信息

server:
  port: 8401


#配置链接 nacos注册中心
#配置连接 sentinel去监控当前服务
spring:
  application:
    name: cloudalibaba-sentinel-service     #nacos中心会自动将此应用名称 读成服务提供者的名字
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848    #nacos注册中心的连接地址
    sentinel:
      transport:
        dashboard: localhost:8080
        port: 8719  #默认8719,应用与Sentinel控制台交互的端口,应用本地会起一个该端口占用HttpServer

management:
  endpoints:
    web:
      exposure:
        include: '*'    #设置匹配的路径规则

app启动类新增注解将当前服务注册到nacos注册中心

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication

//将当前服务提供者 注册到注册中心
@EnableDiscoveryClient
public class PaymentMain9001 {
    public static void main(String[] args) {
        SpringApplication.run(PaymentMain9001.class,args);
    }
}

controller层创建两个接口 测试降级 流簇等功能

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PaymentController {

    @Value("${server.port}")
    private String serverPort;

    @GetMapping(value = "/payment/nacos/{id}")
    public String getPayment(@PathVariable("id") Long id) {
        return "nacos registry, serverPort: "+ serverPort+"\t id"+id;
    }
}

下一篇文章 开始利用 sentinel控制台 实现服务提供者的 流控 降级 限流 熔断等功能