1.从简单demo 搭建开始

使用 springcloud + nacos(注册配置中心) + gateway(网关)

cloud_gateWay 网关

pom如下

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">

    <parent>
        <groupId>org.caolinfeng.demo</groupId>
        <artifactId>cloud_parent_impor</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <artifactId>cloud_gateway</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>cloud_gateway</name>
    <description>Demo project for Spring Boot</description>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>${nacos.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            <version>${nacos.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-openfeign -->
 <!--       <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>${openfeing.version}</version>
        </dependency>-->
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-loadbalancer -->
       <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-loadbalancer</artifactId>
            <version>${loadbalancer.version}</version>
        </dependency>

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

配置文件: bootstrap.yml

server:
  port: 1888
logging:
  level:
    org.springframework.cloud.gateway: TRACE
    org.springframework.http.server.reactive: DEBUG
    org.springframework.web.reactive: DEBUG
    reactor.ipc.netty: DEBUG
spring:
  application:
    name: cloud-gateway
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.0.104:8848
        namespace: 9b06e3f1-1772-45b9-9db9-3c912339c6ec
        group: demo
      config:
        server-addr: 192.168.0.104:8848
        namespace: 9b06e3f1-1772-45b9-9db9-3c912339c6ec
        group: demo
        prefix: ${spring.application.name}
        file-extension: yaml  # 配置文件类型
        refresh-enabled: true  # 是否开启自动刷新
    gateway:
      discovery:
        locator:
          enabled: true
#      routes:
#        - id: order-servcer
#          uri: lb://order-servcer
#          #uri: http://192.168.0.104:89 # 服务端 service_id
#          predicates:
#            - Path=/order/**
#          filters: # 过滤器,请求在传递过程中可以通过过滤器对其进行一定的修改
#            - StripPrefix=1 # 转发之前去掉1层路径

问题一: bootstrap.yml和 application.yml 文件区别。

配置文件 bootstrap.yml。当时用 application.yml 时 如果不使用nacos 作为配置中心,启动成功,如果使用nacos 作为配置中心 启动失败,改用 bootstrap.yml 时才能顺利加载配置

网关 使用gateway 路由到其他服务时 两种方式
1.指定路由
例如 http://192.168.0.104:89
2.加载配置做负载均衡
例如 lb://order-servcer

问题二:lb://xx 客户端负载均衡。

在gateWay 也必须引入如下依赖 才会调用到 lb://order-servcer成功否则503

   <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-loadbalancer</artifactId>
        <version>${loadbalancer.version}</version>
    </dependency>

cloud_order

pom如下

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">

       <parent>
        <groupId>org.caolinfeng.demo</groupId>
        <artifactId>cloud_parent_impor</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>


    <modelVersion>4.0.0</modelVersion>
    <artifactId>cloud_order</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>spring-boot-order</name>
    <description>Demo project for Spring Boot</description>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            <version>${nacos.version}</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>${nacos.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-openfeign -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>${openfeing.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-loadbalancer -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-loadbalancer</artifactId>
            <version>${loadbalancer.version}</version>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>

    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

配置文件: bootstrap.yml

nacos:
  addr: 192.168.0.104:8848
  namespace: 9b06e3f1-1772-45b9-9db9-3c912339c6ec
  group: demo
spring:
  application:
    name: order-servcer
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.0.104:8848
        namespace: 9b06e3f1-1772-45b9-9db9-3c912339c6ec
        group: demo
      config:
        server-addr: 192.168.0.104:8848
        namespace: 9b06e3f1-1772-45b9-9db9-3c912339c6ec
        prefix: ${spring.application.name}
        group: demo
        file-extension: yaml  # 配置文件类型
        refresh-enabled: true  # 是否开启自动刷新
server:
  port: 1890

测试接口

@RestController
@RequestMapping("/order")
public class OrderController {
    @Value("${usernamex}")
    private String usernamex;

    @GetMapping("/get/{id}")
    public String getOrderId(@PathVariable("id") Long id) {

        return String.format(usernamex + "%s%s", "   ——", id.toString());
    }
}

项目图如下
在这里插入图片描述

思考

gateway 首重路由转发,类似nginx 反向代理,对外暴露统一入口,根据路由规则转发到不同服务。

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐