本示例项目采用在maven父工程SpringCloud下面创建子module(sentinel_order_service_rest)形式。
文件清单列表:
- 父工程pom.xml
- 子工程pom.xml
- 子工程配置文件application.properties
- 子工程启动类
- 子工程controller
一、父工程pom文件 pom.xml 如下:(其中包含的其他子模块可以忽略)
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xiaohui.springCloud</groupId>
<artifactId>SpringCloud</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
</parent>
<modules>
<module>product_service</module>
<module>order_service</module>
<module>cloud-api-common</module>
<module>eureka_server</module>
<module>eureka_server2</module>
<module>cloud-import-test</module>
<module>consul_product_service</module>
<module>consul_order_service</module>
<module>openfeign_order_service</module>
<module>hystrix_order_service_ipconn</module>
<module>hystrix_order_service_rest</module>
<module>hystrix-turbine</module>
<module>sentinel_order_service_rest</module>
<module>sentinel_order_service_feign</module>
</modules>
<!-- 统一jar包管理 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.version>4.12</junit.version>
<log4j.version>1.2.17</log4j.version>
<lombok.version>1.16.18</lombok.version>
<mysql.version>5.1.47</mysql.version>
<druid.version>1.1.16</druid.version>
<mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version>
</properties>
<!-- 子模块继承之后,提供作用:锁定版本+子模块不用写groupId和version -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- springcloud alibaba -->
<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>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.spring.boot.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<optional>true</optional>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<!-- maven 插件 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<addResources>true</addResources>
</configuration>
</plugin>
</plugins>
</build>
</project>
二、子模块(sentinel_order_service_rest)pom.xml文件如下
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>SpringCloud</artifactId>
<groupId>com.xiaohui.springCloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>sentinel_order_service_rest</artifactId>
<dependencies>
<dependency>
<groupId>com.xiaohui.springCloud</groupId>
<artifactId>cloud-api-common</artifactId>
<version>${project.version}</version>
</dependency>
<!-- web依赖开始 -->
<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>
<!-- web依赖结束 -->
<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>
<!-- 引入 alibaba sentinel 断路器-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
</dependencies>
</project>
sentinel maven 依赖只引入单独一个maven依赖spring-cloud-starter-alibaba-sentinel即可
三、子工程配置文件application.properties
server:
port: 80 #服务端口
spring:
application:
name: cloud-order-service #服务名称
cloud:
sentinel:
transport:
dashboard: 127.0.0.1:8080
logging:
level:
root: debug
在此配置文件中配置了应用端口、以及应用名称、以及sentinel的监控平台地址。
四、SpringBoot 启动类
package com.xiaohui.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class,args);
}
}
五、controller接口
package com.xiaohui.springcloud.controller;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.xiaohui.springcloud.entities.CommonResult;
import com.xiaohui.springcloud.entities.Payment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class OrderController {
@SentinelResource
@GetMapping("/consumer/product/get2")
public CommonResult<Payment> getPaymentById(Long id){
CommonResult<Payment> commonResult = null;
try{Thread.sleep(2000);}catch (Exception e){}
if(id>1L){
int i = 10/0;
}else {
commonResult = new CommonResult<>(401,"正常方法调用",null);
}
return commonResult;
}
}
在服务接口上需要添加 @SentinelResource注解,添加后并访问后才能在监控平台看到改接口信息(默认懒加载模式)。
启动访问 http://127.0.0.1/consumer/product/get2?id=1 打印正常
监控平台界面