2、子工程导入nacos注册中心discovery包com.alibaba.cloud spring-cloud-alibaba-dependencies 2.1.0.RELEASE pom import
3、yml配置 (1)服务端配置com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery
spring:
application:
name: nacos-provider
cloud:
nacos:
discovery:
server-addr: 192.168.253.201:8848
server:
port: 9002
management:
endpoints:
web:
exposure:
include: '*'
(2)客户端配置
server:
port: 81
spring:
application:
name: nacos-consumer
cloud:
nacos:
discovery:
server-addr: 192.168.253.201:8848
server-url:
nacos-user-service: http://nacos-provider
4、启动类添加注解
@SpringBootApplication
@EnableDiscoveryClient //开启nacos服务注册中心
public class NacosProviderApplication9001 {
public static void main(String[] args) {
SpringApplication.run(NacosProviderApplication9001.class, args);
}
}
客户端在配置类
@Configuration
public class SpringConfig {
@Bean
@LoadBalanced //开启负载均衡
public RestTemplate restTemplate(RestTemplateBuilder builder){
return builder.build();
}
}
二、nacos配置中心(自带动态刷新功能,无需手动刷新)
1、导入nacos-Config的包依赖
2、编写yml配置文件 (1)、bootstrap.ymlcom.alibaba.cloud spring-cloud-starter-alibaba-nacos-config com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-devtools runtime true org.projectlombok lombok org.springframework.boot spring-boot-starter-test test
server:
port: 3001
spring:
application:
name: nacos-config
cloud:
nacos:
config:
server-addr: 192.168.253.201:8848
file-extension: yaml #指定配置中心使用的配置文件格式
shared-dataids: nacos-config-dev.yaml #支持多个共享data id的配置,要写后缀名,file-extension配置项对此项配置无效
refreshable-dataids: nacos-config-dev.yaml #指定哪些共享配置是需要支持自动刷新的,默认没有刷新
namespace: 25ab3d68-80dd-4f92-b04f-fa9f0b0ff8ee #指定配置文件在配置中心的工作空间,不设置默认为public
group: DEFAULT-GROUP #组名,不指定默认为DEFAULT-GROUP
#配置中心文件命名格式(注意在图形化界面中创建文件时,文件后缀必须为yaml,yml无法识别)
# ${spring.application.name}-${spring.profile.active}.${spring.cLoud.nacos.config.fiLe-extension}
(2)、application.yml
spring:
profiles:
active: dev
3、编写主启动类
@SpringBootApplication
@EnableDiscoveryClient
public class NacosConfigApplication3001 {
public static void main(String[] args) {
SpringApplication.run(NacosConfigApplication3001.class,args);
}
}
4、完成业务类的编写
@RestController
@RefreshScope //开启nacos动态刷新的功能
public class NacosConfigController {
@Value("${config.info}")
private String configInfo;
@GetMapping("/getConfigInfo")
public String getConfigInfo(){
return configInfo;
}
}
5、在图形化配置中心添加配置文件
三、nacos config集群持久化
nacos config 默认使用derby数据库,是一个嵌入式的小型数据库,如果在集群中使用会出现配置信息不一致的问题,因此需要在linux上搭建mysql(目前仅支持mysql)来给nacos config保存相关信息,以达到集群配置信息的统一。
1、mysql配置 四、Sentinel服务降级和限流处理 1、pom包引入资源2、yml配置信息com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery com.alibaba.cloud spring-cloud-starter-alibaba-sentinel org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-devtools runtime true org.projectlombok lombok org.springframework.boot spring-boot-starter-test test
server:
port: 8401
spring:
application:
name: alibaba-sentinel8401
cloud:
nacos:
discovery:
server-addr: 192.168.253.201:1111 #通过nginx注册到nacos集群
sentinel:
transport:
dashboard: localhost:8080 #通过Sentinel的dashboard监控服务
port: 8719 #默认8719端口,如果被占用则依次加一向后查找未被占用的端口
management:
endpoints:
web:
exposure:
include: '*'
3、启动类
@SpringBootApplication
@EnableDiscoveryClient
public class SentinelApplication8401 {
public static void main(String[] args) {
SpringApplication.run(SentinelApplication8401.class,args);
}
}
4、逻辑类
@RestController
public class SentinelController {
@GetMapping("/testA")
public String testA(){
return "sentinel-------------testA";
}
@GetMapping("/testB")
public String testB(){
return "sentinel-------------testB";
}
}
5、dashboard监控(需要先提前访问自己的业务端口,dashboard是懒加载)
6、通过可视化界面对服务进行限流处理
直接快速失败
关联快速失败
直接预热方式
直接排队等待
服务降级
RT
异常比例
异常数
热点规则(不处理相应接口应本身异常出现的错误)
@GetMapping("/testHotKey")
@SentinelResource(value = "badtestHotKey",blockHandler = "badtestHotKey")
public String tetsHotKey(@RequestParam(value = "content",required = false) String content){
return "testHotKey------"+content;
}
public String badtestHotKey(String content, BlockException exception){//必须跟随BlockException异常
return "badtestHotKey----------o(╥﹏╥)o";
}
自定义处理类
在需要处理服务限流的方法上添加注解
@SentinelResource(value = “testBlockHandler”,blockHandlerClass = SentinelHandlersClass.class, blockHandler = “myHandler1”,exceptionsToIgnore = {IllegalargentException.class})
blockHandlerClass:指定兜底的类
blockHandler :指定兜底的方法
exceptionsToIgnore :指定排除的异常,当发生指定异常时不对改异常做降级处理
兜底方法一定要是静态static修饰的,否则会找不到处理的兜底方法
public class SentinelHandlersClass {
public static CommonResult myHandler1(BlockException blockException){
return CommonResult.failed("myHandler1------"+blockException.getMessage());
}
}
@SentinelResource(value = “testBlockHandler”,fallback = handlerFallback")
可与blockHandler 配套使用,当程序发生运行时异常就会到blockHandler 指定的方法处理,如果违背用户自定义的限流规则将到fallback进行处理
7、sentinel 限流规则持久化到nacos pom包ymlcom.alibaba.csp sentinel-datasource-nacos
spring:
cloud:
sentinel:
transport:
dashboard: localhost:8080 #通过Sentinel的dashboard监控服务
port: 8719 #默认8719端口,如果被占用则依次加一向后查找未被占用的端口
datasource:
ds1:
nacos:
server-addr: 192.168.253.201:8848
dataId: cloudAlibaba-sentinel-service
groupId: DEFAULT_GROUP
data-type: json
rule-type: flow
rd监控服务
port: 8719 #默认8719端口,如果被占用则依次加一向后查找未被占用的端口
datasource:
ds1:
nacos:
server-addr: 192.168.253.201:8848
dataId: cloudAlibaba-sentinel-service
groupId: DEFAULT_GROUP
data-type: json
rule-type: flow



