文章目录

1、nacos下载安装1.1、启动服务器1.2、关闭服务器1.3、服务注册&发现和配置管理接口

2、代码示例2.1、app1工程代码2.2、app2工程代码2.3、gateway网关工程代码

3、动态配置网关路由3.1、配置动态路由3.2、配置为负载模式

4、gateway配置规则4.1、请求转发,转发指定地址4.2、去掉指定的前缀路径4.3、正则匹配重写路径

Spring Cloud Alibaba官方:https://sca.aliyun.com/zh-cn/ Spring Cloud官网:https://spring.io/projects/spring-cloud

Spring Cloud与Spring Cloud Alibaba版本对应说明:https://sca.aliyun.com/zh-cn/docs/2022.0.0.0/overview/version-explain

1、nacos下载安装

下载地址:https://github.com/alibaba/nacos/releases 下载编译压缩并解压:nacos-server-2.2.3.zip。

1.1、启动服务器

注:Nacos的运行需要以至少2C4g60g*3的机器配置下运行。

#启动命令(standalone代表着单机模式运行,非集群模式):

#Linux/Unix/Mac

sh startup.sh -m standalone

#如果您使用的是ubuntu系统,或者运行脚本报错提示[[符号找不到,可尝试如下运行:

bash startup.sh -m standalone

#Windows

startup.cmd -m standalone

1.2、关闭服务器

#Linux/Unix/Mac

sh shutdown.sh

#Windows

shutdown.cmd

#或者双击shutdown.cmd运行文件。

1.3、服务注册&发现和配置管理接口

#服务注册

curl -X POST 'http://127.0.0.1:8848/nacos/v1/ns/instance?serviceName=nacos.naming.serviceName&ip=20.18.7.10&port=8080'

#服务发现

curl -X GET 'http://127.0.0.1:8848/nacos/v1/ns/instance/list?serviceName=nacos.naming.serviceName'

#发布配置

curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos.cfg.dataId&group=test&content=HelloWorld"

#获取配置

curl -X GET "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos.cfg.dataId&group=test"

参考自官方安装说明:https://nacos.io/zh-cn/docs/quick-start.html

2、代码示例

示例代码分为3个工程:app1(服务1工程),app2(服务2工程),gateway(网关工程),使用的依赖包版本:

com.alibaba.cloud:2022.0.0.0

org.springframework.cloud:2022.0.4

org.springframework.boot:3.0.9

app1,app2都提供个接口:goods(商品信息接口),user(用户信息接口)

goods接口通过网关,app1和app2提供负载模式访问

user接口通过网关,代理方式访问

2.1、app1工程代码

pom.xml

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.penngo.app1

gateway-app1

1.0-SNAPSHOT

17

17

UTF-8

org.springframework.boot

spring-boot-starter-web

com.alibaba.cloud

spring-cloud-starter-alibaba-nacos-discovery

com.alibaba.cloud

spring-cloud-alibaba-dependencies

2022.0.0.0

pom

import

org.springframework.cloud

spring-cloud-dependencies

2022.0.4

pom

import

org.springframework.boot

spring-boot-dependencies

3.0.9

pom

import

配置文件:application.yml

spring:

application:

name: app-service

cloud:

nacos:

discovery:

server-addr: localhost:8848

locator:

lower-case-service-id: true

server:

port: 9091

servlet:

encoding:

force: true

charset: UTF-8

enabled: true

业务代码:AppMain.java

package com.penngo.app1;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

import java.util.Map;

@SpringBootApplication

@EnableDiscoveryClient

public class AppMain {

public static void main(String[] args) {

SpringApplication.run(AppMain.class, args);

}

@RestController

public class HelloController {

@GetMapping("/goods")

public Map goods(){

Map data = new HashMap<>();

data.put("name", "手机");

data.put("service", "app1");

return data;

}

@GetMapping("/user")

public Map user(){

Map data = new HashMap<>();

data.put("user", "test");

data.put("service", "app1");

return data;

}

}

}

2.2、app2工程代码

pom.xml与app1工程一样。 配置文件:application.yml,与app2区分不同的端口

spring:

application:

name: app-service

cloud:

nacos:

discovery:

server-addr: localhost:8848

locator:

lower-case-service-id: true

server:

port: 9091

servlet:

encoding:

force: true

charset: UTF-8

enabled: true

2.3、gateway网关工程代码

pom.xml

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.penngo.gateway

gateway-nacos

1.0-SNAPSHOT

17

17

UTF-8

com.alibaba.cloud

spring-cloud-starter-alibaba-nacos-discovery

com.alibaba.cloud

spring-cloud-starter-alibaba-nacos-config

org.springframework.cloud

spring-cloud-starter-gateway

org.springframework.cloud

spring-cloud-starter-loadbalancer

org.projectlombok

lombok

com.alibaba.cloud

spring-cloud-alibaba-dependencies

2022.0.0.0

pom

import

org.springframework.cloud

spring-cloud-dependencies

2022.0.4

pom

import

org.springframework.boot

spring-boot-dependencies

3.0.9

pom

import

配置application.yml

server:

port: 9090

spring:

application:

name: gatewayapp

cloud:

nacos:

discovery:

server-addr: localhost:8848

locator:

lower-case-service-id: true

config:

server-addr: localhost:8848

# 加载 dataid 配置文件的后缀,默认是 properties

file-extension: yml

# 配置组,默认就是 DEFAULT_GROUP

group: DEFAULT_GROUP

# 配置命名空间,此处写的是 命名空间的id 的值,默认是 public 命名空间

# namespace:

# data-id 的前缀,默认就是 spring.application.name 的值

prefix: ${spring.application.name}

GatewayMain.java

package com.penngo.gateway;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class GatewayMain {

public static void main(String[] args) {

SpringApplication.run(GatewayMain.class, args);

}

}

3、动态配置网关路由

三个工程启动后,nacos的服务列表

3.1、配置动态路由

spring:

cloud:

gateway:

routes:

- id: app1

uri: http://localhost:9091/

predicates:

- Path=/app1/**

filters:

- StripPrefix=1

- id: app2

uri: http://localhost:9092/

predicates:

- Path=/app2/**

filters:

- StripPrefix=1

配置后,可以通过网关的端口9090和地址访问

http://localhost:9090/app1/user

http://localhost:9090/app2/user

3.2、配置为负载模式

spring:

cloud:

gateway:

routes:

- id: app1

uri: http://localhost:9091/

predicates:

- Path=/app1/**

filters:

- StripPrefix=1

- id: app2

uri: http://localhost:9092/

predicates:

- Path=/app2/**

filters:

- StripPrefix=1

- id: app

uri: lb://app-service

predicates:

- Path=/app/**

filters:

- StripPrefix=1

配置后,可以通过同一个地址访问到两个服务返回的数据

http://localhost:9090/app/goods

4、gateway配置规则

参数说明

id: 路由ID

uri: 目标地址,可以是服务,如果服务Spring推荐用全大写,实际调用大小写不敏感,都可以调通。

predicates: 匹配路径,以浏览器请求的端口号后面的第一级路径为起始。

filters: 过滤器,包含Spring Gateway 内置过滤器,可以自定义过滤器。

4.1、请求转发,转发指定地址

routes:

# 跳转URL

- id: 163_route

uri: http://localhost:9091/

predicates:

- Path=/app

访问地址:http://localhost:9090/app/index真实地址:http://localhost:9091/app/index

4.2、去掉指定的前缀路径

- id: app1

uri: http://localhost:9091/

predicates:

- Path=/app1/**

filters:

- StripPrefix=1

去掉第1层的路径前缀app1

访问地址:http://localhost:9090/app1/user真实地址:http://localhost:9091/user

4.3、正则匹配重写路径

- id: test

uri: lb://app-service

predicates:

- Path=/test/**

filters:

- RewritePath=/test/(?.*), /$\{path}

去掉第1层的路径前缀app1

访问地址:http://localhost:9090/app/goods真实地址:http://localhost:9091/goods 或 http://localhost:9091/goods

源码下载

精彩内容

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: