目录
一、GateWay项目创建
二、子项目创建
三、测试调用
一、GateWay项目创建
首先启动本地nacos,具体可参考:Nacos Windows安装_雨欲语的博客-CSDN博客
新建工程项目spring_cloud_test,pom引入依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring_cloud_gateway</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
</dependencies>
创建启动类:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
新建配置文件:
server:
port: 9999
spring:
application:
name: service-gateway
cloud: # 配置Spring Cloud相关属性
gateway:
discovery: # 配置网关发现机制
locator: # 配置处理机制
# 只要请求地址符合规则: http://gatewayIP:gatewayPort/微服务名称/微服务请求地址
# 网关自动映射。把请求转发到 http://微服务名称/微服务请求地址
# 如:有微服务,命名是service-one
# 请求地址是: http://localhost:9999/service-one/nacos/test
# 自动转发到: http://service-one/nacos/test
# 商业开发中,enabled一般不设置,使用默认值false。避免不必要的自动转发规则。
enabled: false # 开启网关自动映射处理逻辑
lower-case-service-id: true # 开启小写转换
nacos:
# nacos用户名和密码
username: nacos
password: nacos
discovery:
# nacos地址
server-addr: 127.0.0.1
group: dev
namespace: dev
metadata:
version: v1.0.0
启动项目,然后在nacos中可看见服务注册成功:
二、子项目创建
新建子项目service_one,service_two。
pom引入依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>service_one</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--Spring Cloud Alibaba Nacos discovery -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
启动类:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class OneApplication {
public static void main(String[] args) {
SpringApplication.run(OneApplication.class, args);
}
}
controller:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/nacos")
public class NacosTestController {
@GetMapping("/test")
public String test(){
return "8080";
}
}
配置文件:
server:
port: 8080
spring:
application:
name: service-one
cloud:
nacos:
username: nacos
password: nacos
discovery:
enabled: true
register-enabled: true
server-addr: 127.0.0.1
group: dev
namespace: dev
metadata:
version: v1.0.0
另外一个服务同理,只是换一个port,然后启动,可在nacos看见服务已注册上去:
三、测试调用
本地调用:http://localhost:9999/service-one/nacos/test
可以看见由gateway进行了自动转发,并且是轮询进行。