目录
下载sentinel-dashboard-1.8.0.jar包
先说一下我的项目搭建环境,我在阿里云服务器部署了docker丶docker-compose,然后又通过docker安装了nacos丶mysql丶sentinel,但微服务项目是在本机运行的,项目环境一切都正常,可就是监控不到自己注册的服务。
问题
docker运行sentinel容器,控制台监控不到任何服务请求信息
查看日志
docker logs -f sentinel
发现报错:
java.util.concurrent.ExecutionException: java.net.ConnectException: Connection timed out
at java.util.concurrent.CompletableFuture.reportGet(CompletableFuture.java:357) ~[na:1.8.0_202]
at java.util.concurrent.CompletableFuture.get(CompletableFuture.java:1895) ~[na:1.8.0_202]
at com.alibaba.csp.sentinel.dashboard.client.SentinelApiClient.fetchItems(SentinelApiClient.java:374) [classes!/:na]
at com.alibaba.csp.sentinel.dashboard.client.SentinelApiClient.fetchResourceOfMachine(SentinelApiClient.java:441) [classes!/:na]
at com.alibaba.csp.sentinel.dashboard.controller.ResourceController.fetchResourceChainListOfMachine(ResourceController.java:70) [classes!/:na]
at sun.reflect.GeneratedMethodAccessor130.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_202]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_202]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209) [spring-web-5.0.9.RELEASE.jar!/:5.0.9.RELEASE]
2022-06-19 13:01:09.974 ERROR 1 --- [pool-2-thread-1] c.a.c.s.dashboard.metric.MetricFetcher : Failed to fetch metric from <http://192.168.249.12:8719/metric?startTime=1655614734000&endTime=1655614740000&refetch=false> (ConnectionException: Connection timed out)
解决方案
在服务器上同步一下时间
ntpdate ntp1.aliyun.com
在application.yaml配置文件,transport下加上 clientIp: localhost在重启服务即可
重启后还是看不到自己注册接口服务?
如下图只能看到本地服务却看不到自己注册的服务
原因:Sentinel是 懒加载机制所以呢,需要访问一下接口即可再去访问Sentinel 就有数据了
因为我的项目是在本机,而配置的Sentinel是在云服务器中,所以我们访问的接口应该是云服务的地址,如果访问的本机地址,将还是会没有数据,将项目部署到云服务器上将不会出现此效果。
此时自己注册的服务被监控到了!
问题解决!
我的配置文件
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">
<parent>
<artifactId>springcloudalibaba</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>account-service</artifactId>
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>common-service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--alibaba-nacos服务注册与发现的依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--添加sentinel-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
application.yaml
spring:
application:
name: account-service
#需要将这个服务注册到Nacos中
cloud:
nacos:
discovery:
server-addr: xx.xxx.xxx.xx:8848 #nacos的服务注册中心
sentinel:
transport:
# sentinel服务端地址
dashboard: xx.xxx.xxx.xx:8858 #sentinel的控制台
clientIp: localhost
eager: true # 取消延迟加载
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://xx.xxx.xxx.xx:3307/onlinebank?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
username: root
password: 123456
server:
port: 8082
mybatis-plus:
mapper-locations: classpath*:/mapper/*Mapper.xml
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
map-underscore-to-camel-case: true
type-aliases-package: com.hzf.common.entity
docker-compose.yaml
version: "3"
services:
mysql:
container_name: mysql
image: mysql:5.7
environment:
- MYSQL_ROOT_PASSWORD=123456
volumes:
- /app/cloud/mysql/data:/var/lib/mysql
ports:
- "3307:3306"
restart: always
nacos:
image: nacos/nacos-server:1.4.1
container_name: nacos
hostname: nacos
restart: always
environment:
- MODE=standalone
- TZ=Asia/Shanghai
- NACOS_SERVER_PORT=8848
- SPRING_DATASOURCE_PLATFORM=mysql
- MYSQL_SERVICE_HOST=xx.xxx.xxx.xx
- MYSQL_SERVICE_PORT=3307
- MYSQL_SERVICE_DB_NAME=nacos_config
- MYSQL_SERVICE_USER=root
- MYSQL_SERVICE_PASSWORD=123456
- PREFER_HOST_MODE=hostname
volumes:
- /app/cloud/nacos/logs:/home/nacos/logs
ports:
- "8848:8848"
sentinel:
image: bladex/sentinel-dashboard:1.8.0
container_name: sentinel
volumes:
- /etc/timezone:/etc/timezone
- /etc/localtime:/etc/localtime
ports:
- "8858:8858"
restart: always
controller
/**
* 功能描述:
* 添加@SentinelResource注解,让sentinel能够识别这个资源
* @Param: [id]
* @Return: com.hzf.common.response.Result
* @Author: Mr.Huang
* @Date: 2022/6/19 13:44
**/
@GetMapping("/getAccountByUserId/{id}")
@SentinelResource("getAccountByUserId")
public Result getAccountByUserId(@PathVariable("id") Integer id){
LambdaQueryWrapper<Account> wrapper=new LambdaQueryWrapper<>();
wrapper.eq(Account::getUserId,id);
List<Account> list = accountService.list();
return Result.ok().data("accounts",list).data("port",port);
}
引发新的问题
这样做只能解决服务的监控问题,并没有真正的解决问题,服务的限流丶熔断丶降级将无法操作。
因为我们的微服务是在本机,而sentinel是在云服务器上运行,所以解决方案只有两个:
1:将本机的微服务项目部署到云服务上
2:在本机安装一个sentinel运行
这里我使用第二种方式
下载sentinel-dashboard-1.8.0.jar包
下载地址:https://github.com/alibaba/Sentinel/releases/tag/v1.8.0
运行
java -jar -Dserver.port=8858 sentinel-dashboard-1.8.0.jar
修改配置文件
重启服务运行结果
至此问题完全解决