关于java:NginxZuul实现网关集群

Nginx

下载Nginx

下载nginx,这里作者选用的是 nginx/Windows-1.16.1版本。
解压后如下:

Nginx常用命令

  • 启动命令:start nginx
  • 进行命令:nginx.exe -s stop 或 nginx.exe -s quit
  • 查看Nginx版本:nginx -v

Nginx启动

这里咱们抉择cmd启动,关上cmd,切换到nginx目录

为了确保nginx的正确运行,咱们先不要批改nginx.conf文件,间接应用命令启动,看是否能失常拜访:localhost:80。

我的项目调整

  • TokenFilter 中把端口号打印进去
@Value("${server.port}")
private String serverPort;

Zuul网关

我的项目启动程序

  1. 启动 eureka-server服务(EurekaServer)
  2. 启动 config-server服务(cloud config分布式配置核心–gitte)
  3. 启动 app-member服务(会员服务)
  4. 启动 app-order服务(订单服务)
  5. cmd启动 zuul 81网关
  6. idea启动 zuul 82网关

我的项目启动后如下:

这里只对zuul网关集群服务做具体介绍。

cmd启动 zuul 81网关

  • 找到jar包门路

  • cmd 切换到target目录,执行 java -jar xxx.jar 命令

idea 启动 zuul 82网关

注:我这里启了83的网关。

  • 批改 zuul api网关端口为82(83)

  • idea启动 AppGateway
  • 查看 eureka server 服务注册列表

批改Nginx配置文件

  • 关上 nginx.conf 文件

  • 增加上游服务器
     #### 上游服务器 集群 默认轮询机制
    upstream backServer{
        server localhost:81;
        server localhost:83;
    }

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {

            #root   html;
            #index  index.html index.htm;
            #### 指定上游服务器 负载平衡服务器
            proxy_pass http://backServer;
            index  index.html;
        }

        #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;
        #}
    }
  • 应用命令 nginx.exe -s stop 进行 nginx 服务,再重启nginx。
  • 拜访 http://localhost/api-member/getMember?name=iswulongbo&userToken=cloud


能够看到 8183 的网关轮询拜访

证实网关实现了集群!

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理