【XBLOG】简单使用SpringCloudGateway+Nacos搭建一个网关

网关

  • 想参考蘑菇Blog做一个微服务博客,起名XBLOG,现在简单使用SpringCloudGateway + Nacos 搭建起一个网关。

1. 安装Nacos

  • Nacos 是用来干嘛的?
    • 在XBlog中是作为注册中心与配置中心。
    • 注册中心:与 Spring Cloud Gateway 搭配使用,实现负载均衡
    • 配置中心:热发布配置管理、与 Spring Cloud Gateway 搭配使用,实现网关的动态路由
  • 在Linux 下安装下载启动。
  • 下载 解压 参考:https://nacos.io/zh-cn/docs/quick-start.html
  • 启动:在nacos 的 bin 目录下 运行 sh [startup.sh](http://startup.sh) -m standalone
  • 访问 Naocs: http://ip地址:8848/nacos
    • 默认用户名和密码:nacos
  • 关闭:运行 sh shutdown.sh

2. 先简单搭建一个网关

2.1 路由转发 + Nacos 注册中心

  • 为什么要整合Nacos 注册中心
    • 因为直接使用Spring Cloud Gateway 进行转发,需要填写URL,无法实现负载均衡等功能,因此整合 Nacos 注册中心,这样可以使用服务在Nacos 上注册的名称来代替 URL。

2.1.1 网关

  1. 网关作为一个服务,所以先创建一个Modules。

  2. 导入相关依赖,XBLOG 使用 Spring Cloud Gateway 和 SpringCloudAlibaba 的 Nacos 的注册中心。

    <!--
         Spring Cloud Gateway 网关
         为微服务架构提供一种简单有效的API路由管理方式
         并基于Filter的方式提供网关的基本功能(安全认证、监控、限流)
    -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-gateway</artifactId>
    </dependency>
    
    <!--
        Spring Cloud Alibaba Nacos 服务发现、注册中心
    -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    
  3. 配置 boostrap.yml

    • 配置Nacos server的地址。

    • 使用 LoadBalancer 方式请求。

      Spring Cloud Nacos 2021 后就用LoadBalancer 作为负载均衡器了(取代Ribbon),实现了轮询 (RoundRobinLoadBalancer) 和随机 (RandomLoadBalancer) 两种方式;如果引入了 NacosDiscovery 那么还可以使用 NacosLoadBalancer的方式。

    server:
      port: 18012                    # 网关服务使用的接口
    
    spring:
      application:
        name: xblog-gateway         # 当前应用的名称
      cloud:
        gateway: # config the routes for gateway
          routes:
            - id: xblog-admin         #将/xblog-admin/开头的请求转发到 xblog-admin 上
              # 采用 LoadBalanceClient 方式请求,以 lb:// 开头,后面是注册在 Nacos 上的服务名
              uri: lb://xblog-admin
              predicates:
                - Path=/xblog-admin/**
              filters:
                - StripPrefix=1               #表明前缀/provider1需要截取掉
        nacos:
          discovery:
            # 将注册中心指定为 Nacos Server 的地址,下面填 nacos server 的地址
            server-addr: [你的NacosServerIP]:8848
    
  4. 开发服务网关启动类 GatewayApplication

    @EnableDiscoveryClient
    @SpringBootApplication
    public class GatewayApplication {
        public static void main(String[] args) {
            SpringApplication.run(GatewayApplication.class, args);
        }
    }
    
    • 使用了 Spring Cloud 原生注释 @EnableDiscoveryClient 开启服务注册发现功能。

2.1.2 服务

  1. 也是像网关那样引入 Spring Cloud Gateway 和 Nacos 服务发现的依赖 至 pom.xml。

  2. 开发服务启动类。

    1. 也是使用 @EnableDiscoveryClient 开启服务注册发现功能。
  3. 开发 Controller 接口。

    @RestController
    @RequestMapping("/blog")
    public class BlogController {
        @Autowired
        private BlogService blogService;
    
        /*
            博客列表
         */
        @GetMapping("/getList")
        public String getList(){
            return "获得博客列表";
        }
    }
    
  4. 配置 boostrap.yml

    server:
      port:
        8081
    
    spring:
      application:
        name: xblog-admin
      cloud:
        nacos:
          server-addr: [你的Nacos Server IP]:8848
    

2.1.3 测试

  1. 打开 Nacos 页面,可以查看到有两个注册的服务。
    在这里插入图片描述

  2. 在浏览器中输入 [localhost:18012/xblog-admin/blog/getList](http://localhost:18012/xblog-admin/blog/getList) 查看结果。
    在这里插入图片描述

2.1.4 遇到的问题

  1. 连接远程 Nacos 却提示 http://localhost:8848, err: connect timed out。

    1. 要注意的第一点是:Nacos 服务注册的配置需要写在 bootstrap.yml 而不是 application.yml

    2. 但是我写在bootstrap.yml 了,还是会报错,注意的第二点是:因为在整合注册中心时还没有用到 Nacos 配置中心,所以要把Nacos 配置中心的依赖删除。

      						<!--
      						      nacos 微服务注册发现配置中心
      						      SpringCloud Alibaba 集成了 nacos
      						  -->
      <!--        <dependency>-->
      <!--            <groupId>com.alibaba.cloud</groupId>-->
      <!--            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>-->
      <!--        </dependency>-->
      
      • 删除后还是报错,然后想到可能是pom 依赖缓存还在。因此重新 sync 一下,就好了。
  2. 在想要测试gateway 路由是否成功时,发现浏览器输入 url 无法成功访问写的接口。

    @RestController
    @RequestMapping("/blog")
    public class BlogController {
        @Autowired
        private BlogService blogService;
    
        /*
            博客列表
         */
        @GetMapping("/getList")
        public String getList(){
            return "获得博客列表";
        }
    }
    
    • 发现是Controller 上的注释一开始使用了 @Controller ,这样表明 getList 后面 return 的跳转地址,而我们想要的是显示我们想要的内容(返回JSON,XML或自定义mediaType 内容到页面),应该使用 @RestController@RestBody + @Controller )。

2.2 整合 Nacos 配置中心实现动态路由

  1. 在Nacos 中创建服务配置,把 bootstrap.yml 里面 spring cloud gateway 的路由配置剪切到Nacos 的配置中。

    1. data-id: 最好是服务名称+环境。
    2. 格式:这里选择的是 yaml,我们平常使用中后缀名 yml 是YAML 的缩写,在此处创建时为了保持格式选择的一致性,我们后缀名也填写为 yaml。
  2. 在服务中 Nacos 配置中心。

    1. 配置 Nacos 配置中心地址。由于Nacos 注册中心和配置中心都是同个地址,我们将其提出来,写在 spring → cloud → nacos 下面了。
    2. config 中配置文件后缀名 file-extension。
    server:
      port: 18012
    
    spring:
      application:
        name: xblog-gateway         # 当前应用的名称
      cloud:
        nacos:
          server-addr: [NacosIP]:8848
          config:
            file-extension: yaml
    
  3. 测试:删除原有的 spring cloud gateway 配置,依旧可以进行路由转发,可以看出成功使用Nacos 拉取了配置。

2.3 使用命名空间

  • 什么是 Nacos 命名空间:相当于是一个独立的环境。
  • Nacos 增加命名空间:
    • 在Nacos 中添加命名空间。
    • 产生命名空间的的id。
    • 在服务中进行配置。 namespace: [你的命名空间id]

Reference

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值