alibaba的sentinel实现带context path的方式运行,以及nginx代理配置

2022-06-01 876点热度 0人点赞 0条评论

Alibaba的Sentinel运行的时候,例如java -jar sentinel-dashboard-1.8.4.jar,sentinel的下载地址:https://github.com/alibaba/Sentinel/releases (默认启动tomcat端口号8080)是不带context-path的,就是访问的时候是http://127.0.0.1:8080,但是有些场景下,是需要使用context-path的(例如对外端口不够用的情况等等),需要让它启动在context-path下,例如:http://127.0.0.1:8080/sentinel

以下是自定义运行sentinel的console的命令,实现了自定义端口号、自定义用户名、密码,以及自定义context-path(以/sentinel为例)

java -Dserver.port=8858 -Dcsp.sentinel.dashboard.server=localhost:8858 -Dproject.name=sentinel-dashboard -Dcsp.sentinel.heartbeat.api.path=/sentinel/registry/machine -Dsentinel.dashboard.auth.username=admin -Dsentinel.dashboard.auth.password="sentinel" -jar /opt/sentinel/sentinel-dashboard-1.8.4.jar --server.servlet.context-path=/sentinel

现在sentinel的管理界面就可以使用 http://127.0.0.1:8858/sentinel 来访问了,不过还有个问题,就是SpringBoot程序来连的时候(例如心跳包)到现在写文章的时候,还是无法支持context-path的。

不过既然为了加context-path来节约对外端口,一般还是用到了nginx,我们可以把SpringBoot中sentinel的心跳包通过转发(proxy_pass)的方式,让请求到达内部的sentinel控制台。

nginx代码如下:

# dashboard加上context-path的转发
location ^~ /sentinel/ {
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://127.0.0.1:8858;
}

# 客户端心跳包始终无法加api-context-path,看到github上有关issue的讨论,但是还没有处理,所以这里用nginx来做转发,给他自动加上context-path: /sentinel
location ^~ /registry/ {
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://127.0.0.1:8858/sentinel/registry/;
}

 

admin

这个人很懒,什么都没留下

文章评论

您需要 登录 之后才可以评论