背景架构配置nginxgateway配置gateway pom.xml服务配置服务注册到Nacos服务请求总结
背景实践配置一套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,
服务配置4.0.0 open.source.test nacos-discovery-test 1.0-SNAPSHOT nacos-discovery-test org.springframework.boot spring-boot-starter-parent 2.2.5.RELEASE org.springframework.boot spring-boot-starter-actuator org.springframework.cloud spring-cloud-starter-gateway com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery org.springframework.boot spring-boot-maven-plugin
生成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,把不同路径的请求转发到不同的服务



