Spring Cloud Alibaba开发教程 :使用Nacos结合Gateway实现服务路由

关注程序员7歌,一块儿用技术改变世界java

Nacos介绍

Nacos 是阿里巴巴开源的一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台。程序员

Gateway介绍

Spring Cloud Gateway 是 Spring Cloud 的一个全新项目,该项目是基于 Spring 5.0,Spring Boot 2.0 和 Project Reactor 等技术开发的网关,它旨在为微服务架构提供一种简单有效的统一的 API 路由管理方式。spring

Spring Cloud Gateway 做为 Spring Cloud 生态系统中的网关,目标是替代 Netflix Zuul,其不只提供统一的路由方式,而且基于 Filter 链的方式提供了网关基本的功能,例如:安全,监控/指标,和限流。bootstrap

建立应用

建立一个名为alibaba-nacos-gateway的应用,引入cloud/nacos/gateway相关依赖,pom.xml配置以下:api

<properties> <java.version>1.8</java.version> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <cloud-version>Finchley.SR1</cloud-version> <alibaba-version>0.9.0.RELEASE</alibaba-version></properties>
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency></dependencies><dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${cloud-version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${alibaba-version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies></dependencyManagement>

在项目的resource下面新增bootstrap.yml配置文件,配置nacos服务地址及其网关信息,配置信息以下:
浏览器

server: port: 8905spring: application: name: alibaba-nacos-gateway cloud: nacos: discovery: server-addr: 127.0.0.1:8848 namespace: 0df79d5a-2432-4840-a254-e946a943a6c9 gateway: routes: - id: alibaba-nacos-server uri: lb://alibaba-nacos-server predicates: # 路径匹配,以 api 开头,直接配置是不生效的,看 filters 配置 - Path=/server/** #filters: # 前缀过滤,默认配置下,咱们的请求路径是 http://localhost:9000/myshop-service-consumer-item/** 这时会路由到指定的服务 # 此处配置去掉 1 个路径前缀,再配置上面的 Path=/api/**,就能按照 http://localhost:9000/api/** 的方式访问了 #- StripPrefix=1

配置解析:
安全

1.id:须要访问的目标服务,这里仍是以alibaba-nacos-server为提供的服务微信

2.url: lb:目标服务在注册中心的服务名。架构

3.predicates(断言):其主要的目的是能够路由到以server打头的全部接口方法。app

修改应用的启动类,添加注解@EnableDiscoveryClient,做用是将该服务注册到注册中心,代码以下:

package com.test.alibabanacosgateway;
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication@EnableDiscoveryClientpublic class AlibabaNacosGatewayApplication {
public static void main(String[] args) { SpringApplication.run(AlibabaNacosGatewayApplication.class, args); }
}

经过配置context-path的方式

修改alibaba-nacos-server的bootstrap.yml配置文件,添加应用上下文信息,便是server.servlet.context-path=/server,配置以下:

server: port: 8902 servlet: context-path: /serverspring: application: name: alibaba-nacos-server cloud: nacos: discovery: server-addr: 127.0.0.1:8848 namespace: 0df79d5a-2432-4840-a254-e946a943a6c9

接下来分别启动alibaba-nacos-server与alibaba-nacos-gateway服务,应用启动成功后,打开nacos能够看到两个服务注册成功,以下图:

紧接着打开浏览器访问:http://localhost:8905/server/test?msg=zengxueqi,能够看到浏览器正常访问alibaba-nacos-server服务接口。

访问过程:

页面输入http://localhost:8905/server/test?msg=zengxueqi地址,访问网关服务,服务根据bootstrap.yml路由配置/server进行服务转向,从而访问目标服务,以下图:

经过跳过固定前缀的方式

这种方式也是springcloud gateway推荐的方式,经过跳过固定前缀的方式来路由目标服务,先删除alibaba-nacos-server应用的bootstrap.yml配置文件配置的context-path,而后在alibaba-nacos-gateway应用的bootstrap.yml配置文件中国呢添加以下配置:

filters: # 前缀过滤,默认配置下,咱们的请求路径是 http://localhost:9000/myshop-service-consumer-item/** 这时会路由到指定的服务    # 此处配置去掉 1 个路径前缀,再配置上面的 Path=/api/**,就能按照 http://localhost:9000/api/** 的方式访问了    - StripPrefix=1

filters配置的-StripPrefix用于从指定从当前位置跳过,StripPrefix设置为1,由于url的前缀当前位置就是1,如url为:http://localhost:8905/server/test?msg=zengxueqi,则是从server位置跳过,能够访问sever后面结尾的全部服务。接下来重启两个服务应用,刷新浏览器能够看到依然正确输出内容,以下图:

按照这样的方式,之后增长相关服务时,无需配置应用的context-path,只须要在网关中配置固定前缀便可。


本文分享自微信公众号 - 程序猿的故事(zengxueqi-music)。
若有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一块儿分享。

相关文章
相关标签/搜索