nginx + spring gateway+spring 服务_nginx 转发到 gateway

背景

实践 配置 一套nginx +gateway+微服务的架构

架构

配置nginx

bash 复制代码
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8080;
        server_name  springboot.com;

        location / {
            root   html;
            index  index.html index.htm;
        }

        # 服务a转发到gateway
        location /provide-servicea/ {
            proxy_pass  http://127.0.0.1:8091;
        }
         # 服务b转发到gateway
        location /provide-serviceb/ {
            proxy_pass  http://127.0.0.1:8091;
        }


  
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
       
    }




  
    include servers/*;
}

gateway配置

bash 复制代码
server:
  port: 8091
spring:
  application:
    name: gateway
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
      config:
        server-addr: localhost:8848
    gateway:
      discovery:
        locator:
          enabled: true
          lower-case-service-id: true
#      routes:
#        - id: provide-service
#          uri: lb://provide-service
#          predicates:
#            - Path=/**
#          filters:
#            - StripPrefix=1

discovery.locator.enabled=true

会自动 拉取 nacos里的服务生成route,

gateway pom.xml

bash 复制代码
<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>open.source.test</groupId>
    <artifactId>nacos-discovery-test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>nacos-discovery-test</name>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath/>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

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

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
    </dependencies>

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

服务配置

生成A服务和B服务

bash 复制代码
spring.application.name=provide-serviceb
server.port=9093
nacos.discovery.server-addr=127.0.0.1:8848

服务 注册 到Nacos

服务请求

http://springboot.com:8080/provide-servicea/te/hello

http://springboot.com:8080/provide-serviceb/te/hello

总结

请求先到nginx,nginx会将请求转发到gateway,gateway根据route,把不同路径的请求转发到不同的服务

相关推荐
ping某1 天前
为什么 Nginx 明明监听了 80,转发后端时却用了 4xxxx 端口?
后端·nginx
码云数智-园园3 天前
C++20 Modules 模块详解
java·开发语言·spring
咖啡八杯3 天前
GoF设计模式——享元模式
java·spring·设计模式·享元模式
Flittly3 天前
【AgentScope Java新手村系列】(10)实战-多Agent天气助手
java·spring boot·spring
李少兄3 天前
从原理到实战:Spring IoC/DI 核心知识体系与高频面试题全解
java·后端·spring
shushangyun_3 天前
2026年快消品B2B系统推荐:支持终端门店订货、促销政策自动化的工具?
java·运维·网络·数据库·人工智能·spring·自动化
ofoxcoding3 天前
在AI API聚合平台配置DeepSeek V3.2提示词缓存实战:快速接入与成本优化指南
人工智能·spring·缓存·ai
一杯奶茶¥3 天前
水果销售网站 CRM客户信息管理系统 超市管理系 酒店管理系统 健身房管理系统 在线音乐网站 校园招聘系统
java·vue.js·spring boot·mysql·spring·java项目
難釋懷3 天前
Nginx反向代理中的容错机制
运维·nginx
bloglin999993 天前
Nginx高危漏洞CVE-2021-23017及配置样例
运维·nginx