Spring Cloud Nacos 作为注册和配置中心 + Spring Cloud Gateway作为网关 + Sentinel 作为熔断服务

依赖

因为要整合gateway所以(版本的选择为spring官方的推荐搭配)

springcloud 的版本选择  Finchley.SR2

springboot 的版本选择   2.0.6.RELEASE

这里nacos的starter选择用以下版本

<nacos.version>0.2.1.RELEASE</nacos.version>

 

NACOS启动

首先去github下载源码,这里我下载后的版本选择0.8.0RELEASE

配置好后启动即可

 

注册中心整合

和注册中心的整合比较简单,

首先添加依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    <version>${nacos.version}</version>
</dependency>

其次在启动类上加上注解 

@EnableDiscoveryClient

最后在配置文件配置

server.port=12000

spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

management.endpoints.web.exposure.include=*

这样注册要注册中心了http://localhost:8848/nacos/index.html(nacos默认登录用户名是nacos密码也是nacos)

 

整合配置中心

 

这回首先要在nacos中配置相关的配置

加载依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    <version>${nacos.version}</version>
</dependency>

然后是在配置文件(bootstrap.properties)中加

# 配置的前缀ata-id
spring.application.name=client

spring.cloud.nacos.config.server-addr=127.0.0.1:8848
# 配置的后缀
spring.cloud.nacos.config.file-extension=properties

最后就和原来使用springcloud一样通过@RefreshScope 和@Value注解即可

 

整合网关

加载依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

加入以下配置

server:
  port: 13000

spring:
  application:
    name: gateway
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true  #表明gateway开启服务注册和发现的功能,并且spring cloud gateway自动根据服务发现为每一个服务创建了一个router,这个router将以服务名开头的请求路径转发到对应的服务。
          lowerCaseServiceId: true   #是将请求路径上的服务名配置为小写(因为服务注册的时候,向注册中心注册时将服务名转成大写的了),比如以/service-hi/*的请求路径被路由转发到服务名为service-hi的服务上。
          filters:
            - StripPrefix=1
    nacos:
      config:
        server-addr: 127.0.0.1:8848
      discovery:
        server-addr: 127.0.0.1:8848

 

 

结果展示

网关

http://localhost:13000/client/hello

-> client

http://localhost:12000/client/hello

client中的配置属性来自nacos

 

 

返回的结果通过调用client2

结果如下

 

Sentinel 整合

首先下载sentinel的工程,然后找到里面的dashboard启动

依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    <version>${nacos.version}</version>
</dependency>

 

因为目前sentinel没有直接支持gateway,所以这里仅仅在client里进行配置和应用,

具体配置如下

@Bean
@LoadBalanced
@SentinelRestTemplate(blockHandler = "handleException", blockHandlerClass = ExceptionUtil.class)
RestTemplate restTemplate(){
    return new RestTemplate();
}
@RequestMapping(value = "/get", method = GET)
@ResponseBody
@SentinelResource(blockHandler = "handleException", blockHandlerClass = ExceptionUtil.class)
public List<Instance> get(@RequestParam String serviceName) throws NacosException {
    return namingService.getAllInstances(serviceName);
}

这样就能做到服务调用的限流以及服务资源提供的限流,可以再dashboard上看到

 

遇到的坑

在整合过程中遇到比较多的坑, 比如nacos的data-id要配置后缀名,不然找不到配置, 以及在网关配置的时候需要配置成 '*', 不能直接写出*.

当然其实经历过eureka+springcloud config 的搭建以及,用zookeeper作为配置中心的搭建, 所以替换nacos也是比较简单.

如果要开启数据存储配置的话需要在以下文件中配置相关属性

 

 

对比

与eureka对比

从官网来看nacos 的注册的实例数是大于eureka的,然后因为nacos使用的raft协议,所以一致性要远大于eureka.

与springcloud config 

nacos的配置中心感觉和apollo类似,都有使用长轮询, 然后配置变动后通知非常的迅速, 秒杀springcloud原来的config几条街,毕竟原来的config是基于git, 不提供可视化界面, 动态变更还需要依赖bus来通过所有的客户端变化. 

与hystrix

sentinel更加的轻量级,并且支持动态的限流调整,更加友好的界面ui

 

更多的功能等待发掘...

 

github地址: https://github.com/cdy1996/sample-springcloud/tree/nacos

 

 

  • 4
    点赞
  • 70
    收藏
    觉得还不错? 一键收藏
  • 13
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值