第一步:搭建父项目,创建一个Maven项目,父项目不写代码,直接删除src
第二步:父项目需要的包
// 打包方式 <packaging>pom</packaging>
<!--SpringBoot,帮子项目管理依赖的版本号--> <parent> <groupId> org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.5.RELEASE</version> </parent>
<!--SpringCloudalibaba需要的依赖--> <dependencyManagement> <dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.2.1.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR3</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
<!--一些公共的依赖--> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.58</version> </dependency> </dependencies>
第三步:创建子项目springcloudalibaba-user-server
user项目功能1.项目启动时要注册到nacos,启动nacos的方式,到下载的bin目录下,输入命令:startup.cmd -m standalone启动,功能2:限流,熔断,功能4:限流持久化。功能5:nacos的远程配置中心,功能6:三层架构
1.user模块需要导包
<dependencies> <dependency> <groupId>com.alibaba.cloud </groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 配置中心客户端--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> <!--sentinel的包--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> <!--Sentinel和Nacos做持久的--> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-datasource-nacos</artifactId> <version>1.5.2</version> </dependency> <!-- 数据库的jar包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.26</version> </dependency> <!--mybaties,和spring的集合包--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> </dependencies>
2.yaml配置
2.1 bootstrap.yml
server: port: 1010 spring: application: name: user-server cloud: sentinel: transport: dashboard: localhost:1111 #senticel服务控制台地址 nacos: discovery: server-addr: 127.0.0.1:8848 #注册中心地址 config: server-addr: localhost:8848 #配置中心 file-extension: yaml #配置文件格式 prefix: application-user #配置前缀 ,默认使用sring.application.name group: DEFAULT_GROUP #默认分组 namespace: 1415d50f-943a-469c-af50-10ca5f8dc91b #命名空间的id profiles: active: dev
2.2 application.yaml
spring: cloud: sentinel: transport: dashboard: localhost:1111 #senticel服务控制台地址 datasource: flow: nacos: #限流持久配置 server-addr: localhost:8848 #使用nacos的持久 dataId: application-user-dev #获取限流的数据源的dataId groupId: DEFAULT_GROUP rule-type: flow #类型:限流 namespace: 1415d50f-943a-469c-af50-10ca5f8dc91b # 因为我们在test里放的这个,持久化限流机制
连接数据库的配置 spring: datasource: password: 364911 driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql:///movie?useUnicode=true&characterEncoding=utf-8 username: root mybatis: type-aliases-package: cn.zwj.domain
2.启动类
@SpringBootApplication public class UserStart { public static void main(String[] args) { SpringApplication.run(UserStart.class,args); } }
3.controller层代码
@RefreshScope //刷新配置 @RestController public class UserController { @Value("${testTement}") private String testTement; @GetMapping("/user/{id}") // blockHandler里有许多规则,包括限流等 @SentinelResource(value = "这就是取一个名字", blockHandler = "getBlockHandlerUserById", fallback = "getfallback") public User getUserById(@PathVariable Long id) { int i = 1/0; // 设置一个异常 return User.builder().name("陈涛").intro("又在打来福了" + testTement).build(); } // 限流与阻塞处理 : 参数要和 被降级的方法参数一样 ,注意限流的优先级比熔断要高,比如,同时有限流和熔断,一定会执行限流 public User getBlockHandlerUserById(@PathVariable Long id, BlockException ex) { // 来判断究竟是哪个异常 if (ex instanceof FlowException) { return User.builder().name("流控异常").build(); } else if (ex instanceof DegradeException) { return User.builder().name("熔断异常").build(); } else if (ex instanceof ParamFlowException) { return User.builder().name("热点异常").build(); } else if (ex instanceof SystemBlockException) { return User.builder().name("系统异常").build(); } else if (ex instanceof AuthorityException) { return User.builder().name("授权异常").build(); } return User.builder().name("陈涛").intro("把来福咬了,我要限流" + testTement).id(id).build(); } public User getfallback(@PathVariable Long id, Throwable ex) { ex.printStackTrace(); return User.builder().name("这里是熔断处理").build(); } }
具体步骤:user模块的远程配置中心和持久化限流如何配置
1.要配置远程中心:第一步点击+,如果需要配置命名空间,可以先创建一个命名空间,命名空间id可以不写,可以使用默认的,然后再在这个命名空间添加配置文件如application-user-dev.yaml
2.配置限流持久化
2.1 到安装目录下输入 java -jar -Dserver.port=1111 sentinel-dashboard-1.6.0.jar
2.2 第一步:在配置列表增加配置如下:就点击+号
[
{
"resource": "这就是取一个名字",
"limitApp": "default",
"grade": 1,
"count": 1,
"strategy": 0,
"controlBehavior": 0,
"clusterMode": false
}
]
其中resouce就是要找对那个资源进行限流,
就是@SentinelResource这个注解的value值
第四步:springcloudalibaba-order-server模块
模块功能:1.需要配置远程配置 2.要通过openfeign掉user时做负载均衡和熔断处理
1.导包
<dependencies> <dependency> <groupId>com.alibaba.cloud </groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 配置中心客户端--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> <dependency> <groupId>cn.zwj</groupId> <artifactId>springcloudalibaba-user-common</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <!--sentinel的包--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> </dependencies>
2.启动类
@SpringBootApplication @EnableFeignClients public class OrderStart { public static void main(String[] args) { SpringApplication.run(OrderStart.class,args); } }
3.controller层
@RestController @RefreshScope public class OrderController { @Autowired private UserFeign userFeign; @GetMapping("/order/{id}") public User getUserById(@PathVariable Long id) { return userFeign.getUserById(id); } }
为了通过openfeign用负载均衡的方式,给user发请求。所有我们要创建一个接口如userfeign
打上@feignclient注解
@FeignClient(value = "user-server", fallbackFactory = FallbackFactoryOrder.class) public interface UserFeign { @GetMapping("/user/{id}") User getUserById(@PathVariable Long id); }
value为我们要发请求服务名,GetMapping为对方user要接收的参数@PathVariable Long id,形参也是,我们在conreoller层发请求时,先把这个接收注入,再给他传值。
要实现熔断fallbackFactory,写一个类去继承
FallbackFactory<UserFeign>的泛型,泛型就是你发请求的接口
@Component public class FallbackFactoryOrder implements FallbackFactory<UserFeign> { @Override public UserFeign create(Throwable throwable) { return new UserFeign() { @Override public User getUserById(Long id) { return User.builder().name("返回兜底数据").build(); } }; } }
必须要加@Component注解,这样才能给spring管理