微服务 Spring Cloud Alibaba 项目搭建(五、gateway 网关子模块创建)

一、项目 - New - Module

二、选择Maven - jdk- Next

三、修改Name为gateway - 查看GroupId是否正确 - Finish

四、修改pom.xml文件


        
        
            com.bi.cloud
            common
            1.0-SNAPSHOT
            
            
                
                    mysql
                    mysql-connector-java
                
                
                    org.mybatis.spring.boot
                    mybatis-spring-boot-starter
                
                
                    com.alibaba
                    druid-spring-boot-starter
                
            
        
        
        
            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
        
        
            com.alibaba.csp
            sentinel-spring-cloud-gateway-adapter
        
        
        
            org.springframework.boot
            spring-boot-starter-webflux
        
        
            com.alibaba.cloud
            spring-cloud-starter-dubbo
        

        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-sentinel
        

        
        
            com.alibaba.csp
            sentinel-datasource-nacos
        
        
        
            com.alibaba.cloud
            spring-cloud-starter-dubbo
        
        
            com.alibaba.csp
            sentinel-apache-dubbo-adapter
        
        
        
            javax.servlet
            javax.servlet-api
        
    

完整pom.xml展示



    
        bi-cloud
        com.bi.cloud
        1.0-SNAPSHOT
    
    4.0.0

    gateway


    
        
        
            com.bi.cloud
            common
            1.0-SNAPSHOT
            
            
                
                    mysql
                    mysql-connector-java
                
                
                    org.mybatis.spring.boot
                    mybatis-spring-boot-starter
                
                
                    com.alibaba
                    druid-spring-boot-starter
                
            
        
        
        
            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
        
        
            com.alibaba.csp
            sentinel-spring-cloud-gateway-adapter
        
        
        
            org.springframework.boot
            spring-boot-starter-webflux
        
        
            com.alibaba.cloud
            spring-cloud-starter-dubbo
        

        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-sentinel
        
        
        
        
            com.alibaba.csp
            sentinel-datasource-nacos
        
        
        
            com.alibaba.cloud
            spring-cloud-starter-dubbo
        
        
            com.alibaba.csp
            sentinel-apache-dubbo-adapter
        
        
        
            javax.servlet
            javax.servlet-api
        
    

五、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)
返回测试数据,服务调用成功!!!

第六章 Sentinel 流量控制、熔断降级集成 https://www.jianshu.com/p/36873b6f70a6

你可能感兴趣的:(微服务 Spring Cloud Alibaba 项目搭建(五、gateway 网关子模块创建))