nginx + spring gateway+spring 服务
文章目录背景架构配置nginxgateway配置gateway pom.xml服务配置服务注册到Nacos服务请求总结背景实践配置一套nginx +gateway+微服务的架构架构配置nginx#usernobody;worker_processes1;#error_loglogs/error.log;#error_loglogs/error.lognotice;#error_loglogs/er
·
背景
实践配置一套nginx +gateway+微服务的架构
架构
配置nginx
#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配置
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
<?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服务
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,把不同路径的请求转发到不同的服务
更多推荐
已为社区贡献2条内容
所有评论(0)