1、spring-cloud-alibaba
pom文件:
pom
org.springframework.boot spring-boot-starter-parent2.2.5.RELEASE
com.alibaba.cloud spring-cloud-alibaba-dependencies2.2.1.RELEASE pom import org.springframework.cloud spring-cloud-dependenciesHoxton.SR3 pom import
junit junit4.12
log4j log4j1.2.12
org.projectlombok lombok
2、子服务:
spring-cloud-alibaba-user
pom文件:
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
org.springframework.boot
spring-boot-starter-web
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-config
com.alibaba.cloud
spring-cloud-starter-alibaba-sentinel
com.alibaba.csp
sentinel-datasource-nacos
1.5.2
org.springframework.cloud
spring-cloud-starter-openfeign
com.alibaba.cloud
spring-cloud-starter-alibaba-sentinel
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.1.1
mysql
mysql-connector-java
5.1.26
启动类:
@SpringBootApplication
//开启feign客服端
@EnableFeignClients
//注册客户端
@EnableDiscoveryClient
//扫描包路径
@MapperScan("com.jd.mapper")
public class UserStarter {
public static void main(String[] args) {
SpringApplication.run(UserStarter.class);
}
}
file-extension: yaml # 配置文件的后缀 只有两种可能 properties yaml
file-extension: yaml做持久化,如果连接数据库需要配置四大金刚
spring:
cloud:
sentinel:
transport:
#修改yml配置,添加senticel服务控制台地址
dashboard: localhost:1111
datasource:
flow: #限流的配置
nacos: #限流持久配置
#使用nacos的持久
server-addr: localhost:8848
#获取限流的数据源的dataId
dataId: application-store-dev
groupId: DEFAULT_GROUP
#类型:限流
rule-type: flow
datasource:
username: root
password: java
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql:///itsourcesstore?useUnicode=true&characterEncoding=UTF-8
# //配置别名
mybatis:
type-aliases-package: com.jd.domain
#配置sentinel:熔断
feign:
sentinel:
enabled: true #熔断
bootstrap.yml配置文件:
spring:
cloud:
nacos:
server-addr: 127.0.0.1:8848 #nacos的地址
config:
name: user-serve # 配置文件的dataId的前面的部分
file-extension: yaml # 配置文件的后缀 只有两种可能 properties yaml
group: DEFAULT_GROUP #文件的分组 默认DEFAULT_GROUP
# namespace: AricChen #namespace如果不写 那么默认会读取public里面的配置文件
profiles:
active: dev # 环境名
controller配置:
//刷新范围
@RefreshScope
@RestController
//获取远程配置文件的值,看需求写
@Value("${testTemp}")
private String testTemp;
@GetMapping("/getUserById/{id}")
@SentinelResource(value = "根据用户ID获取用户信息"
//限流,配置规则
,blockHandler="getUserByIdBlockHandler",
//熔断
fallback = "getUserByFallback")
public User getUserById(@PathVariable Long id){
int i = 1/0;
return new User(id,"xx","xx"+testTemp);
}
//限流,配置规则
public User getUserByFallback(@PathVariable Long id,Throwable e){
return new User(-1L, "xx", "xx(fallback)");
}
//限流,配置规则
public User getUserByIdBlockHandler(@PathVariable Long id, BlockException ex){
//打印异常
ex.printStackTrace();
//FlowException流异常 instanceof实列
if (ex instanceof FlowException) {
return new User(-1L,"xx","xx1(FlowException)");
//降级异常DegradeException
}else if (ex instanceof DegradeException){
return new User(-1L,"xx","xx2(DegradeException)");
//参数流异常ParamFlowException
}else if (ex instanceof ParamFlowException){
return new User(-1L,"xx","xx3(ParamFlowException)");
//系统块异常SystemBlockException
}else if (ex instanceof SystemBlockException){
return new User(-1L,"xx","xx4(ParamFlowException)");
//权限例外
}else if (ex instanceof AuthorityException){
return new User(-1L,"xx","x5(AuthorityException)");
}
return new User(-1L, "xx", "xx6(block)");
}



