从浅入深 学习 SpringCloud 微服务架构(九)

一、微服务网关引入

1、在微服务架构中,不同的微服务一般会有不同的网络地址,

如果让客户端直接与各个微服务通讯,可能会有很多问题,如:

1)客户端会请求多个不同的服务,需要维护不同的请求地址,增加开发难度。
2)在某些场景下存在跨域请求的问题。
3)加大身份认证的难度,每个微服务需要独立认证。

2、微服务网关,就是介于客户端与服务器之间的中间层,所有的外部请求都会先经过微服务网关。

客户端只需要与网关交互,只知道一个网关地址即可,这样简化了开发还有以下优点:

  • 易于监控。
  • 易于认证。
  • 减少了客户端与各个微服务之间的交互次数。

二、微服务网关的概述

1、什么是微服务网关?

API 网关是一个服务器,是系统对外的唯一入口。API 网关封装了系统内部架构,为每个客户端提供一个定制的 API。
API 网关方式的核心要点是,所有的客户端和消费端都通过统一的网关接入微服务,在网关层处理所有的非业务功能。
通常,网关也是提供 REST/HTTP 的访问 API。服务端通过A PI-GW 注册和管理服务。

2、微服务网关的作用和应用场景

网关具有的职责,如身份验证、监控、负载均衡、缓存、请求分片与管理、静态响应处理。
当然,最主要的职责还是与“外界联系”。

3、常见的 API 微服务网关实现方式

  • Kong

基于 Nginx+Lua 开发,性能高,稳定,有多个可用的插件(限流、鉴权等等)可以开箱即用。
问题:只支持Http协议;二次开发,自由扩展困难;提供管理AP1,缺乏更易用的管控、配置方式。

  • ZuuI

Netflix 开源,功能丰富,使用 JAVA 开发,易于二次开发;需要运行在 web 容器中,如 Tomcat。
问题:缺乏管控,无法动态配置;依赖组件较多;处理 Http 请求依赖的是 Web 容器,性能不如 Nginx 。

  • Traefik

Go 语言开发;轻量易用;提供大多数的功能:服务路由,负载均衡等等;提供WebUl。
问题:二进制文件部署,,二次开发难度大;UI更多的是监控,缺乏配置、管理能力。

  • Spring Cloud Gateway

SpringCloud提供的网关服务。

  • Nginx+lua 实现

使用 Nginx 的截(At+A)和负载均衡可实现对 api 服务器的负载均衡及高可用
问题:自注册的问题和网关本身的扩展性。

三、nginx 模拟 API 网关

1、Nginx 介绍:

Nginx 是一款自由的、开源的、高性能的 HTTP 服务器和反向代理服务器;同时也是一个 IMAP、POP3、SMTP 代理服务器。

Nginx可 以作为一个 HTTP 服务器进行网站的发布处理,另外 Nginx 可以作为反向代理进行负载均衡的实现。

2、测试登录 Nginx 服务:

先启动 Nginx 服务,浏览器地址栏输入:localhost:80 显示如下界面登录成功。

Spring Cloud Alibaba 微服务架构实战派电子书_微服务架构


如果启动 Nginx 不成功,请查看:

#启动 nginx 的时候屏幕一闪而过,报错 failed (1113: No mapping for the Unicode character exists…)

3、修改 Nginx 配置文件,模拟 API 网关服务。

在 Nginx 安装目录下,找到 conf 文件夹下的 nginx.conf,记事本打开编辑:
如: …\nginx-1.8.1\conf\nginx.conf

# D:\Program Files\nginx-1.8.1\conf\nginx.conf

#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       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
		
		
		# 路由到订单服务
		location  /api-product {
			proxy_pass http://127.0.0.1:9001/;
		}
		
		# 路由到商品服务
		location  /api-order {
			proxy_pass http://127.0.0.1:9002/;
		}
		
		

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

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


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

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

}

Spring Cloud Alibaba 微服务架构实战派电子书_eureka_02

4、修改完后,结束 Nginx 服务进程,重新启动,同时启动 service-eureka, service-prodcut, service-order, 三个启动类,进行测试。

Spring Cloud Alibaba 微服务架构实战派电子书_eureka_03


Spring Cloud Alibaba 微服务架构实战派电子书_微服务网关API_04

5、浏览器地址栏输入:

http://localhost/api-product/product/1 就相当于路由转发到 http://127.0.0.1:9001/product/1

Spring Cloud Alibaba 微服务架构实战派电子书_eureka_05

http://localhost/api-order/order/buy/1 就相当于路由转发到 http://127.0.0.1:9002/order/buy/1

Spring Cloud Alibaba 微服务架构实战派电子书_微服务架构_06

# 从浅入深 学习 SpringCloud 微服务架构(八)Sentinel(2)