1.概要
1.1 说明 微服务统一网关实验,这里简单实验一下路由的功能
1.2 实验步骤,使用下面这个工程作为基础工程添加了一个gateWay做如下使用
简单实践 spring cloud nacos nacos-server-2.3.0-CSDN博客
2 代码
2.1 工程文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.xjc.springcloundtest</groupId>
<artifactId>demo21</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>getWay</artifactId>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
</project>
2.2 配置文件
server:
port: 8083
spring:
application:
name: gatWayserver
cloud:
nacos:
discovery:
server-addr: localhost:8848
gateway:
routes:
- id: user service
uri: http://localhost:8080/fun
predicates:
- Path=/fun
2.3 主函数类
package com.xjc.springcloundtest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class);
System.out.println("Hello world!");
}
}
3 运行结果