一、项目 - New - Module
二、选择Maven - jdk- Next
三、修改Name为engine - 查看GroupId是否正确 - Finish
四、修改pom.xml文件
<dependencies>
<!--集成公共模块-->
<dependency>
<groupId>com.bi.cloud</groupId>
<artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version>
<!--gateway忽略mysql,不直接操作数据库-->
<exclusions>
<exclusion>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</exclusion>
<exclusion>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</exclusion>
<exclusion>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--nacos service discovery client依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--nacos config client 依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!--GateWay 网关-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
</dependency>
<!--引入webflux-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
<!--sentinel 核心环境 依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!-- Sentinel支持采用 Nacos 作为规则配置数据源,引入该适配依赖 -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<!--spring cloud+dubbo 依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-apache-dubbo-adapter</artifactId>
</dependency>
<!-- Java Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
</dependencies>
完整pom.xml展示
<?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">
<parent>
<artifactId>bi-cloud</artifactId>
<groupId>com.bi.cloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>gateway</artifactId>
<dependencies>
<!--集成公共模块-->
<dependency>
<groupId>com.bi.cloud</groupId>
<artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version>
<!--gateway忽略mysql,不直接操作数据库-->
<exclusions>
<exclusion>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</exclusion>
<exclusion>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</exclusion>
<exclusion>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--nacos service discovery client依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--nacos config client 依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!--GateWay 网关-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
</dependency>
<!--引入webflux-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
<!--sentinel 核心环境 依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!-- Sentinel支持采用 Nacos 作为规则配置数据源,引入该适配依赖 -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<!--spring cloud+dubbo 依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-apache-dubbo-adapter</artifactId>
</dependency>
<!-- Java Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
</dependencies>
</project>
五、java - New - Java Class 新增启动类 com.bi.cloud.GatewayApplication
package com.bi.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
六、在resources下新增bootstrap.yml 配置文件
bootstrap.yml 配置文件展示
spring:
profiles:
active: test
---
server:
port: 8008
spring:
profiles: test
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
namespace: a60273f4-07fb-4568-82eb-d078a3b02107
config:
server-addr: 127.0.0.1:8848
namespace: a60273f4-07fb-4568-82eb-d078a3b02107
group: DEFAULT_GROUP # 默认分组就是DEFAULT_GROUP,如果使用默认分组可以不配置
file-extension: yml #默认properties
gateway:
# 路由数组[路由 就是指定当请求满足什么条件的时候转到哪个微服务]
routes:
- id: bi-cloud-oauth # 当前路由的标识, 要求唯一
uri: lb://bi-cloud-oauth # lb指的是从 nacos 中按照名称获取微服务,并遵循负载均衡策略
predicates: # 断言(就是路由转发要满足的条件)
- Path=/oauth/** # 当请求路径满足Path指定的规则时,才进行路由转发
# 我们⾃定义的路由 ID,保持唯⼀
- id: bi-cloud-gateway
# ⽬标服务地址(部署多实例)
uri: lb://bi-cloud-gateway
# gateway⽹关从服务注册中⼼获取实例信息然后负载后路由
# 断⾔:路由条件,Predicate 接受⼀个输⼊参数,返回⼀个布尔值结果。该接⼝包含多种默认⽅法来将 Predicate 组合成其他复杂的逻辑(⽐如:与,或,⾮)。
predicates:
- Path=/bi-gateway/api/**
filters: # 过滤器,请求在传递过程中可以通过过滤器对其进行一定的修改
- StripPrefix=1 # 转发之前去掉1层路径
application:
name: bi-cloud-gateway
---
server:
port: 8008
spring:
profiles: pre
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
namespace: c60d2198-0b2f-46c1-82cb-4c2f20fb8123
config:
server-addr: 127.0.0.1:8848
namespace: c60d2198-0b2f-46c1-82cb-4c2f20fb8123
group: DEFAULT_GROUP # 默认分组就是DEFAULT_GROUP,如果使用默认分组可以不配置
file-extension: yml #默认properties
gateway:
# 路由数组[路由 就是指定当请求满足什么条件的时候转到哪个微服务]
routes:
- id: bi-cloud-oauth # 当前路由的标识, 要求唯一
uri: lb://bi-cloud-oauth # lb指的是从 nacos 中按照名称获取微服务,并遵循负载均衡策略
predicates: # 断言(就是路由转发要满足的条件)
- Path=/oauth/** # 当请求路径满足Path指定的规则时,才进行路由转发
# 我们⾃定义的路由 ID,保持唯⼀
- id: bi-cloud-gateway
# ⽬标服务地址(部署多实例)
uri: lb://bi-cloud-gateway
# gateway⽹关从服务注册中⼼获取实例信息然后负载后路由
# 断⾔:路由条件,Predicate 接受⼀个输⼊参数,返回⼀个布尔值结果。该接⼝包含多种默认⽅法来将 Predicate 组合成其他复杂的逻辑(⽐如:与,或,⾮)。
predicates:
- Path=/bi-gateway/api/**
filters: # 过滤器,请求在传递过程中可以通过过滤器对其进行一定的修改
- StripPrefix=1 # 转发之前去掉1层路径
application:
name: bi-cloud-gateway
---
server:
port: 8008
spring:
profiles: prd
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
namespace: 0be74aa4-00e5-4c48-ae8c-34965c327212
config:
server-addr: 127.0.0.1:8848
namespace: 0be74aa4-00e5-4c48-ae8c-34965c327212
group: DEFAULT_GROUP # 默认分组就是DEFAULT_GROUP,如果使用默认分组可以不配置
file-extension: yml #默认properties
gateway:
# 路由数组[路由 就是指定当请求满足什么条件的时候转到哪个微服务]
routes:
- id: bi-cloud-oauth # 当前路由的标识, 要求唯一
uri: lb://bi-cloud-oauth # lb指的是从 nacos 中按照名称获取微服务,并遵循负载均衡策略
predicates: # 断言(就是路由转发要满足的条件)
- Path=/oauth/** # 当请求路径满足Path指定的规则时,才进行路由转发
# 我们⾃定义的路由 ID,保持唯⼀
- id: bi-cloud-gateway
# ⽬标服务地址(部署多实例)
uri: lb://bi-cloud-gateway
# gateway⽹关从服务注册中⼼获取实例信息然后负载后路由
# 断⾔:路由条件,Predicate 接受⼀个输⼊参数,返回⼀个布尔值结果。该接⼝包含多种默认⽅法来将 Predicate 组合成其他复杂的逻辑(⽐如:与,或,⾮)。
predicates:
- Path=/bi-gateway/api/**
filters: # 过滤器,请求在传递过程中可以通过过滤器对其进行一定的修改
- StripPrefix=1 # 转发之前去掉1层路径
application:
name: bi-cloud-gateway
七、Nacos配置(下图为test环境 配置展示,pre、prd配置方式一致)
唯一要注意的是,每个环境的后缀都是不同的
dubbo:
registry:
# 挂载到 Spring Cloud 注册中心
address: spring-cloud://localhost
cloud:
# 订阅服务提供方的应用列表,订阅多个服务提供者使用 "," 连接
subscribed-services: bi-cloud-engine
八、gateway服务功能测试
在common通用模块下pojo创建实体,service创建接口
User
package com.bi.cloud.pojo;
import lombok.Data;
import java.io.Serializable;
@Data
public class User implements Serializable {
private String username;
private String password;
}
UserService
package com.bi.cloud.service;
import com.bi.cloud.pojo.User;
public interface UserService {
User userInfo();
}
在engine模块下创建UserServiceImpl.class实现
package com.bi.cloud.service.Impl;
import com.bi.cloud.pojo.User;
import com.bi.cloud.service.UserService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;
@Service
@Component
public class UserServiceImpl implements UserService {
@Override
public User userInfo() {
User user = new User();
user.setUsername("admin");
user.setPassword("123456");
return user;
}
}
在gateway模块下创建com.bi.cloud.controller.UserController.class
UserController
package com.bi.cloud.controller;
import com.bi.cloud.pojo.User;
import com.bi.cloud.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/user")
public class UserController {
@Reference
private UserService userService;
@PostMapping("/userInfo")
public User userInfo(){
return userService.userInfo();
}
}
项目 - Maven - Reload project 重新编译,启动engine服务、gateway服务
postman工具测试接口(接口地址:http://localhost:8008/bi-gateway/api/user/userInfo)
返回测试数据,服务调用成功!!!
网友评论