SpringCloud Alibaba学习(三)

OpenFeign

一个声明式的Web服务客户端,让便携而Web服务客户端变的非常容易,只需要一个接口加注解

1.导入依赖

2.服务端写被消费者调用的接口

3.写OpenFeign接口

4.写消费者对外接口-调用OpenFeign接口 

<dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-openfeign</artifactId>
       <version>2.2.6.RELEASE</version>
</dependency>
@Service
@FeignClient(value = "provider")
//value值服务提供者在nacos注册中心的服务名。
public interface OpenFeign {
    @RequestMapping("/index")
    public String index();
}
@RestController
public class OpenFeignController {
    @Resource
    private OpenFeign openFeign;

    @RequestMapping("/consumer/index")
    public String index(){
        return openFeign.index();
    }
    @RequestMapping("/test")
    public String test(){
        return "test";
    }
}

最后别忘了在启动类加OpenFeign的注解 

@SpringBootApplication
@EnableFeignClients
public class FeignConsumerApplication {

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

}

 

OpenFeign和Feign和RestTemplate区别

在Feign的基础上支持了SpringMVCD的注解,如@RequestMapping等。OpenFeign的@FeignClient可以解析SpringMVC的@RequestMapping注解下的接口,并通过动态代理的方法产生实现类,实现类中的做负载均衡并调用其他服务 

Feign是SpringCloud组件中的一个轻量级Restful的HTPP服务客户端Feign内置了Ribbon,用来做客户端负载均衡,去调用服务祖册中心的服务。步骤:使用Feign的注解定义接口,调用这个接口,就可以调用服务注册中心的服务

OpenFeign超时控制

提供者controller方法,故意写个暂停程序3秒

消费者controller方法,OpenFeign默认只等待一秒,超时后报错

 如需等待-解决:设置Feign客户端的超时控制

Properties文件中配置

OpenFeign日志打印功能

提供日志打印功能,可以通过配置来调整日志级别,从而来了解Feign中HTTP请求的细节。即对Feign接口调用情况进行监控和输出

NONE:默认的,不显示任何日志

BASIC:仅记录请求方法、URL、响应状态及执行时间

HEADERS:除了BASIC中定义的信息外,还有请求和响应的头信息

FULL:除了HEADERS中定义的信息外,还有请求和响应的正文及元数据 

配置类

 

Gateway网关路由映射

第一代网关——Zuul

第二代网关——Gateway基于Netty实现,跟Servlet不兼容

所以一定不能出现Spring web的依赖

server.port=8181
spring.application.name=gateway
spring.cloud.gateway.discovery.locator.enabled=true
spring.cloud.gateway.routes.id=provider_route
spring.cloud.gateway.routes.uri=http://localhost:8081
spring.cloud.gateway.routes.predicates=Path=/provider/**
spring.cloud.gateway.routes.filters=StripPrefix=1

routes:即配置第一个映射信息,第二个则routes[1]

将localhost:8081的映射成/provder/**

即provider代表localhost:8081

filters配置把第x个前缀去掉(provider)

第二种:基于nacos因为其已经配好了映射,所以不需要配置路由

引入nacos依赖,注释配置文件的路由配置

<dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.2.1.RELEASE</version>
        </dependency>

网关限流

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.2.1.RELEASE</version>
        </dependency>
@Configuration
public class GatewayConfiguration {
    private final List<ViewResolver> viewResolvers;
    private final ServerCodecConfigurer serverCodecConfigurer;
    public GatewayConfiguration(ObjectProvider<List<ViewResolver>> viewResolversProvider, ServerCodecConfigurer serverCodecConfigurer) {
        this.viewResolvers = viewResolversProvider.getIfAvailable(Collections::emptyList);
        this.serverCodecConfigurer = serverCodecConfigurer;
    }
    //配置限流的异常处理
    @Bean
    @Order(Ordered.HIGHEST_PRECEDENCE)
    public SentinelGatewayBlockExceptionHandler
    sentinelGatewayBlockExceptionHandler() {
        return new SentinelGatewayBlockExceptionHandler(viewResolvers, serverCodecConfigurer);
    }
    //配置初始化的限流参数
    @PostConstruct
    public void initGatewayRules(){
        Set<GatewayFlowRule> rules = new HashSet<>();rules.add(new GatewayFlowRule("provider_route")
                        .setCount(1)
                        .setIntervalSec(1)
        );
        GatewayRuleManager.loadRules(rules);
    }
    //初始化限流过滤器
    @Bean
    @Order(Ordered.HIGHEST_PRECEDENCE)
    public GlobalFilter sentinelGatewayFilter(){
        return new SentinelGatewayFilter();
    }
//自定义限流异常页面
    @PostConstruct
    public void initBlockHandlers(){
        BlockRequestHandler blockRequestHandler = new BlockRequestHandler() {
            @Override
            public Mono<ServerResponse>
            handleRequest(ServerWebExchange serverWebExchange, Throwable throwable) {
                Map map = new HashMap();
                map.put("code",0);
                map.put("msg","被限流了");
                return ServerResponse.status(HttpStatus.OK)
                                .contentType(MediaType.APPLICATION_JSON)
                                .body(BodyInserters.fromObject(map));
            }
        };
        GatewayCallbackManager.setBlockHandler(blockRequestHandler);
    }
}

导入依赖:Sentinel和gateway的,因为映射不能直接限流,所以要从网关入手;

配置类(直接复制,修改你需要的信息)

需要注意的是Gateway限流只是限制封装后的网址,不会限制原网址

基于API分组限流(路由多的情况,不分的话需要一个个限流)

Provider服务中进行分组,例如1./api/index 2./api/port ==1和2为一个组

 

 基于Nacos进行限流

加入nacos依赖,注释配置文件的路由配置。

注意:如果在nacos的服务名修改了,限流配置类也需要修改,访问网址也需要修改

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值