• 微服务项目,请求从发出到后端处理器的历程


    在这里插入图片描述
    点击登录按钮,发出

    http://localhost:8803/service_6001/admin/login/in
    
    • 1

    请求,这是一个由nginx配置的前端项目
    查看配置文件,该条请求会被映射形成对http://localhost:51603/admin/login/in的post请求

    upstream heima-admin-gateway
    {
        server localhost:51603;
    }
    server {
        listen 8803;
        server_name 127.0.0.1;
        location / {
            root F:\\BaiduNetdiskDownload\\heima-leadnews\\admin-web;
            index index.html;
        }
    
        location ~/service_6001/(.*)
        {
            proxy_pass http://heima-admin-gateway/$1;
            proxy_set_header HOST $host;
            proxy_pass_request_body on;
            proxy_pass_request_headers on;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $Proxy_add_x_forwarded_for;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    而localhost:51603地址开启了一个网关服务
    查看网关服务的配置文件

    spring:
      cloud:
        gateway:
          globalcors:
            cors-configurations:
              '[/**]': # 匹配所有请求
                allowedOrigins: "*" #跨域处理 允许所有的域
                allowedMethods: # 支持的方法
                - GET
                - POST
                - PUT
                - DELETE
          routes:
          # 平台管理
          - id: admin
            uri: lb://leadnews-admin
            predicates:
            - Path=/admin/**
            filters:
            - StripPrefix= 1
          - id: user
            uri: lb://leadnews-user
            predicates:
            - Path=/user/**
            filters:
            - StripPrefix= 1
          # 自媒体
          - id: wemedia
            uri: lb://leadnews-wemedia
            predicates:
            - Path=/wemedia/**
            filters:
            - StripPrefix= 1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    通过网关请求地址变成了http://leadnews-admin/login/in,
    发送spring.application.name=leadnews-admin的项目的
    /login/in的请求处理器中进行处理

    微服务leadnews-admin加入nacos-discovery的dependency,在启动类上加载@EnableDiscoveryClient
    注册到nacos的服务发现中心,

    服务注册依赖

            <dependency>
                <groupId>com.alibaba.cloudgroupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
            dependency>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    ps:
    请求进入到网关但是没有进入到微服务:

    • 服务是否允许被发现
    • 网关配置服务名是否与服务中心想要的一样
    • 关于post请求,处理器使用了@RequestBody AdUserDTO dto,时要注意请求体的json文件键是否与类对象中的成员变量相匹配
      如果请求体为
    {
    "name":"guest",
    "password":"guest"
    }
    
    • 1
    • 2
    • 3
    • 4

    而AdUserDTO的成员为

    private String username;
    private String password;
    
    • 1
    • 2

    则这条请求不会进入此处理器

    • 测试请求是否进入到微服务可以增加一个拦截所有请求的拦截器,进行断点调试
  • 相关阅读:
    Git 基本操作(入职亲体验)
    深度学习时代的视频理解综述
    Mathematica清除全局变量以及避免与内置命令冲突
    【python】PyQt5可视化开发,鼠标键盘实现联动界面交互逻辑与应用实战
    iris(golang)连接mysql数据库
    vue点击事件
    可视化大屏的几种屏幕适配方案,总有一种是你需要的
    2.1程序的顺序执行与并发执行的特征
    Spring Cloud CLI简介
    计算机毕业设计ssm婚庆公司管理系统8b4of系统+程序+源码+lw+远程部署
  • 原文地址:https://blog.csdn.net/qq_51050526/article/details/134253891