栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springcloud hystrix限流(springcloud限流组件是哪个)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springcloud hystrix限流(springcloud限流组件是哪个)

1、首先创建一个ParentMoudle的java pom文件项目进行服务项目管理,其中要注意的是需要将配置成如下的dependencyManagement格式,否则子项目会报错;


        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Greenwich.RELEASE
                pom
                import
            
        

2、依次创建eurekaServer、provider、feignClient(主要是对外暴露),每个项目的parent设置成如下格式

   
        com.yangfan
        springcloud_feign_parent
        0.0.1-SNAPSHOT
    

创建eurekaserver项目

        1⃣️pom文件中引入spring-cloud-starter-netflix-eureka-server


        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
            2.1.0.RELEASE
        

       2⃣️ 配置yml文件:核心是设置eurekaClient的注册中心的地址

server:
  port: 9010

spring:
  application:
    name: eureka-server

eureka:
  client:
    service-url:
      #eureka 服务地址,如果是集群的话;需要指定其他集群eureka地址
      defaultZone: http://127.0.0.1:9010/eureka
      # 不注册自己
    registry-with-eureka: false
    #不拉服务
    fetch-registry: false

3⃣️在启动类中开启@EnableEurekaServer注解,开启eurekaServer服务

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }

}

创建provider项目

1⃣️项目引入spring-boot-starter-web项目、spring-cloud-starter-netfix-eureka-client依赖

 
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
            2.1.0.RELEASE
        
    

2⃣️设置eurekaServer的注册地址为:http://127.0.0.1:9010/eureka

server:
  port: 9011

spring:
  application:
    name: sentinel-feign-provider

eureka:
  client:
    service-url:
      #eureka 服务地址,如果是集群的话;需要指定其他集群eureka地址
      defaultZone: http://127.0.0.1:9010/eureka #需要注册到注册中心的地址
      # 不注册自己

3⃣️在启动类中开启@EnableEurekaClient注解,表示开启Eureka客户端发现功能

@EnableEurekaClient //开启Eureka客户端发现功能
@SpringBootApplication
public class SentinelFeignProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(SentinelFeignProviderApplication.class, args);
    }

}

创将feign项目

1⃣️项目中引入spring-cloud-starter-netfix-eureka-client依赖、feign的起步依赖spring-cloud-starter-openfeign、sentinel依赖spring-cloud-starter-alibaba-sentinel

 

        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
            2.1.0.RELEASE
        

        
        
            org.springframework.cloud
            spring-cloud-starter-openfeign
            2.1.0.RELEASE
        

        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-sentinel
            2.1.0.RELEASE
        
    

2⃣️yml文件配置:开启sentinel对feign的支持、设置sentinel本地的dashboard地址、向eureka注册中心注册feign服务

server:
  port: 9012

spring:
  application:
    name: sentinel-feign-client
  cloud:
    sentinel:
      transport:
        dashboard: localhost:9000 #开启 sentinel对feign的支持
#激活sentinel的支持
feign:
  sentinel:
    enabled: true

eureka:
  client:
    service-url:
      #eureka 服务地址,如果是集群的话;需要指定其他集群eureka地址
      defaultZone: http://127.0.0.1:9010/eureka #需要注册到注册中心的地址
      # 不注册自己

3⃣️开启@EnableEurekaClient 注解表示向注册中心注册自己@EnableFeignClients表示开启FeignClient服务

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class SentinelFeignClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(SentinelFeignClientApplication.class, args);
    }

}

 4⃣️创建一个FallbackService实现自FeignAgent并实现hello方法

@Component
public class FallbackService implements FeignAgent {
    @Override
    public String hello() {
        return "系统繁忙请稍后!";
    }
}

5⃣️在Feign中新建一个Controller,并引入FeignClient,在hello方法中调用feign的hello方法

@RestController
public class TestController {

    @Autowired
    private FeignAgent feignAgent;

    @GetMapping("/hello")
    public String hello(){
        return feignAgent.hello();
    }
}

6⃣️配置sentinel dashboard的流控规则,流控规则的编写形式为:http:请求方式:协议://服务名/请求路径跟参数 (GET:http://sentinel-feign-provider/hello)

最终效果如下

 

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/773581.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号