SpringCloud

本文介绍了如何使用Nacos进行配置管理,包括创建配置文件,导入依赖,设置bootstrap.yml,以及抽取公共配置。接着详细讲解了Sentinel的服务限流和降级功能,包括Sentinel的安装,整合到项目中,配置限流规则,以及熔断降级的实现。同时提到了Sentinel与OpenFeign的集成,用于服务间的降级处理。
摘要由CSDN通过智能技术生成

一.Nacos配置管理

1.nacos创建配置文件:拷贝yml配置到云端(云端就是指nacos这个官网)

2.项目导入配置依赖:nacos-config(代码如下)

<!--        配置中心客户端-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>

3.项目中创建bootstrap.yml(这个配置的权限比application.yml的权限更高)

3.1配置环境:

spring:
  profiles:
    active: dev

3.2配置nacos.config的地址:

server-addr: 127.0.0.1:8848

3.3配置前缀:后面跟的你在nacos上面取得名字,这部分就是前缀

prefix: application-order

3.4.配置后缀

file-extension: yml

完整的bootstrap.yml配置代码展示

spring:
  profiles:
    active: dev
  cloud:
    nacos:
      config:
        file-extension: yml
        prefix: application-order
        server-addr: 127.0.0.1:8848
        shared-configs:
              - application-dev.yml

4.抽取公共配置

4.1在nacos创建一个公共配置文件

4.2.项目bootstrap.yaml中使用:shared-configs: 配置公共的配置文件名

        shared-configs:
              - application-dev.yml

 二.Sentinel

1.服务安装

1.1下载jar包

点我下载地址

下载之后通过命令启动:

port是设置端口号:然后通过本地访问来访问----localhost:port(端口号)

命令写到我标红的地方,用更多属性来打开,选择一种打开方式,然后把命令写到里面

启动的话只需双击它,后缀为cmd是指Windows

java -jar -Dserver.port=1111 sentinel-dashboard-1.6.0.jar

2.整合sentinel

2.1导入sentinel的依赖

<dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>

2.2.yml配置:配置sentinel的dashboard地址

    sentinel:
      transport:
        dashboard: localhost:8888

2.3

代码方法上:@SentinelResource(value="资源名" , blockHandler="降级方法")

这个blockHandler="降级方法"的降级方法名要跟你写的方法名一致

@GetMapping("/user/{id}")
    @SentinelResource(value = "user",blockHandler = "blockHandler",fallback = "fallback")
    public User getUserById(@PathVariable("id") Long id){
        //int i=1/0;
        return new User(id,"ni测试"+notify+"共享资源"+redis);
    }

    public User blockHandler(@PathVariable("id") Long id, BlockException e){
        e.printStackTrace();
        return new User(id,"已限流");
    }

3.通过dashboard做限流

簇点链路   ->  流控 :创建限流的规则--->这个是指你在nacos网址上有一个流控的选项,然后你需要去设置阙值。然后你再去测试,就是你测试的IP地址

导入依赖:

<!--    限流和gataway使用-->
<dependency>
   <groupId>com.alibaba.cloud</groupId>
   <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
 </dependency>
 <dependency>	
   <groupId>com.alibaba.cloud</groupId>
   <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
 </dependency>

修改yml配置:

spring:
	cloud:
      sentinel:
          transport:
            dashboard: localhost:8888

写一个配置类:

@Configuration
public class SentinelConfig {
    public SentinelConfig(){
        GatewayCallbackManager.setBlockHandler(new BlockRequestHandler() {
            @Override
            public Mono<ServerResponse> handleRequest(ServerWebExchange serverWebExchange, Throwable throwable) {
                return ServerResponse.ok().body(Mono.just("限流啦,请求太频繁"),String.class);
            }
        });
    }
}

1基于nacos持久化

1.1导入持久化依赖:

 <!--Sentinel和Nacos做持久的-->
    <dependency>
        <groupId>com.alibaba.csp</groupId>
        <artifactId>sentinel-datasource-nacos</artifactId>
        <version>1.5.2</version>
     </dependency>

1.2yml配置持久化:

    sentinel:
      datasource:
        flow:
          nacos: #限流持久配置
            server-addr: localhost:8848    #使用nacos的持久
            dataId: application-user-flow-dev    #获取限流的数据源的dataId
            groupId: DEFAULT_GROUP
            rule-type: flow #类型:限流

1.3在nacos创建配置文件:

需要注意的是上面dataId的名字需要跟你nacos的配置文件名一致

 还需要注意的是配置上面的数据,选择json格式

下面是对它的解释

- resource:对那个资源进行限流
- limitApp:这个是流控的调用者,default 代表不区分调用者
- grade:限流方式0是根据并发数量限流,1是表根据QPS来限流
- count:限流阈值,达到这个阈值就被限流,触发降级。
- strategy:基于调用链的流控制策略。0 直接,1 关联 2 链路
- controlBehavior:流控效果,0 直接拒绝,1是Warm Up,2是匀速排队
- clusterMode:是否为集群

4.sentinel熔断降级

4.1.代码方法上:@SentinelResource(value="资源名" , fallback="降级方法"

 // 熔断降级,参数和返回值与源方法一致
   public User getByIdfallback(@PathVariable Long id){
        System.out.println(notify);
        return new User(id,"zs:"+id, "熔断托底了");
    }
    
    @GetMapping("/user/{id}")
    //限流降级
    @SentinelResource(value="user",blockHandler="exceptionHandler",fallback = "getByIdfallback")
    public User getById(@PathVariable Long id){
        int i = 1 / 0;	//方法异常,触发熔断
        return new User(id,"zs:"+id, "我是zs");
    }

4.2.通过dashboard配置熔断规则

5.OpenFeign降级

5.1.导入sentinel包

 <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>

5.2.yaml开启降级

feign:
  sentinel:
    enabled: true #熔断

5.3.Feign接口写降级

@FeignClient(value = "user-server",fallbackFactory = UserClientFactory.class)
public interface UserClient {

    @GetMapping("/user/{id}")
     User getUserById(@PathVariable("id") Long id);

}
@Component
public class UserClientFactory implements FallbackFactory<UserClient> {


    @Override
    public UserClient create(Throwable throwable) {
        return new UserClient() {
            @Override
            public User getUserById(Long id) {
                throwable.printStackTrace();
                return new User(id,"降级啦");
            }
        };
    }
}

值得注意的是你的类名要一致,还有一种方法fallback的写法

@FeignClient(value = "user-server",fallback = UserClientFallback.class)
public interface UserClient {

    @GetMapping("/user/{id}")
    User getById(@PathVariable Long id);
}
@Component
public class UserClientFallback implements UserClient {    
	@Override    
	public User getById(Long id) {        
		return new User(-1L,"无此用户","无此用户");    
	}
}

记得要交给spring管理哦,就是加注解

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值