当前位置:网站首页>Zipkin is easy to use
Zipkin is easy to use
2022-07-02 08:32:00 【Don't understand for a while】
List of articles
One 、 brief introduction
zipkin Official website :https://zipkin.io/
Zipkin Is a distributed link tracking system . It helps to collect the time data needed to solve the delay problem in the service architecture
1、Sleuth and Zipkin similarities and differences
Sleuth and Zipkin Are used for Distributed Link Tracking ,Zipkin Contains sleuth ( Refers to spring-cloud-starter-zipkin contain spring-cloud-starter-sleuth),Zipkin It is divided into server and client , The server provides a UI Monitoring interface , Client refers to every service
2、 Why do distributed systems need link tracking ?
In short : If service A -> B -> C -> D
Call to order , If A Service invocation failed , When troubleshooting the cause of failure, we can't quickly determine which service has made the error , Using distributed link tracking can quickly identify which service has an error and the reason for the error
3、traceid and spanid
In distributed link tracking traceid and spanid:https://blog.csdn.net/kevin_tech/article/details/117203420
Two 、 Installation and operation
1、 There are three ways to install and start
①、 Download executable jar
# Linux
curl -sSL https://zipkin.io/quickstart.sh | bash -s
nohup java -jar zipkin.jar >> out.log &
# window
Download address :https://search.maven.org/search?q=zipkin-server Note that you need to download exec.jar
start /min java -jar zipkin-server-2.23.16-exec.jar # start /min similar Linux Of nohup command
②、 Source download
# get the latest source
git clone https://github.com/openzipkin/zipkin
cd zipkin
# Build the server and also make its dependencies
./mvnw -DskipTests --also-make -pl zipkin-server clean install
# Run the server
java -jar ./zipkin-server/target/zipkin-server-*exec.jar
③、docker start-up
docker pull openzipkin/zipkin
docker run -d -p 9411:9411 openzipkin/zipkin
2、 Access test zipkin
zipkin Default port number 9411, You can zipkin Start the log to view the port number http://127.0.0.1:9411/zipkin/
3、 ... and 、 Configure and start three services
First start nacos、sentinel、zipkin Three servers ,nacos and sentinel You can read my previous articles if you are not clear
Simple test project flow chart , among sentinel Represents the place where current limiting fuse can be configured ,OpenFeign Choose services ,ribbon Select the called service through the load balancing algorithm
The following items springboot edition 2.3.12.RELEASE,nacos edition 2.2.7.RELEASE,springcloud edition Hoxton.SR12
1、Gateway project
spring-cloud-starter-gateway Simple understanding is the back-end web proxy server , Be similar to nginx
pom.xml
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- Exclude partial dependencies , Otherwise, the report will be wrong Spring MVC found on classpath, which is incompatible with Spring Cloud Gateway -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!-- Get rid of springmvc Related configuration information -->
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</exclusion>
<!-- Get rid of tomcat Related configuration -->
<exclusion>
<groupId>org.springframework.bootk</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-el</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-websocket</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>2.2.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
<version>2.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>2.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
<version>1.8.1</version>
</dependency>
from spring-cloud-starter-zipkin Dependency can be seen to include spring-cloud-starter-sleuth (Spring Cloud Distributed system service chain tracking component provided )
Solve mistakes
Report errors :Spring MVC found on classpath, which is incompatible with Spring Cloud Gateway.
The reason is that spring-boot-starter-web and spring-cloud-starter-gateway Conflict
Method ①
exclude spring-boot-starter-web Related dependencies , Above pom.xml As shown in the document ( Refer to the exclusion of other blogs gateway Of web Dependence and webflux Reliance still reports an error , Maybe it's related to the version ) Reference resources :https://blog.csdn.net/w1073459332/article/details/115857429
Method ②
A simpler way is to use only spring-cloud-starter-gateway modular , Don't use spring-boot-starter-web , spring-cloud-starter-gateway The module contains spring-boot-starter-webflux
nacos Console configuration gateway-127-21000
server:
port: 21000
spring:
application:
name: SPRING-CLOUD-GATEWAY-STUDY
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
username: nacos
password: nacos
namespace: 2022-4-1-prod
cluster-name: SPRINGBOOT
ephemeral: true
enabled: true
group: DEFAULT_GROUP
ip: 127.0.0.1
metadata:
username: admin
password: 123456
weight: 1
config:
server-addr: 127.0.0.1:8848
sentinel:
enabled: true
transport:
port: 7832
dashboard: 127.0.0.1:28080
clientIp: 127.0.0.1
log:
dir: I:\nacos colony \boot-21000-sentinel
datasource:
# Random names
ds:
nacos:
server-addr: 127.0.0.1:8848
username: nacos
password: nacos
data-id: sentinel-gateway-127-2100
group-id: DEFAULT_GROUP
namespace: 2022-4-24-sentinel
data-type: json
rule-type: gw-flow # The data source type of gateway flow control rules is gw-flow, If specified as flow Do not take effect
filter:
enabled: false # The default is true
url-patterns: /*
flow:
cold-factor: 3
scg:
fallback:
content-type: application/json
response-status: 429
response-body: spring-cloud-gateway-study Service error
redirect: /404
mode: response
gateway:
enabled: true
discovery:
locator:
enabled: true
lower-case-service-id: false
routes:
- id: service # Set the routing id( In theory, it can be written casually )
order: 0 # order The smaller it is , The higher the priority
uri: lb://SPRING-BOOT-SERVICE-DISCOVERY-CONSUMER # To set up a route url lb://service1( have access to nacos Service registration name ,lb Fixed writing represents registration ) http://localhost:3901( You can also use absolute addresses )
predicates:
- Path=/service/** # Path matching rules , Microservices must have a unified entry , Otherwise, the gateway cannot access ,yaml Profile add server.servlet.context-path=/service or @RequestMapping("/service")( inconvenient , Every request needs to be added )
- id: service1
order: 0
uri: lb://SPRING-CLOUD-SERVICE-OPENFEIGN
predicates:
- Path=/service1/** # If the front-end request path contains api, Then apply this routing rule
- After=2022-05-05T02:00:00.000+08:00[Asia/Shanghai] # The route takes effect after the specified time 、 There are other parameters (Before、Between)
filters:
# - RewritePath=/service1/(?<segment>.*), /$\{segment} # Rewrite the routing address ,http://127.0.0.1:21000/service1/hystrix/calculate It will be forwarded to http://127.0.0.1:20004/hystrix/calculate
- RewritePath=/service1/(?<segment>.*), /service1/$\{
segment}
- StripPrefix=0 # filter StripPrefix, The function is to remove the front of the request path n Cut off parts .StripPrefix=1 It means that the number of intercepted paths is 1, For example, the front end comes to ask /test/good/1/view, After matching successfully , The request path routed to the back end becomes http://localhost:8888/good/1/view
zipkin:
base-url: http://127.0.0.1:9411
sender:
type: web
locator:
discovery:
enabled: true
sleuth:
sampler:
probability: 1.0
nacos To configure sentinel Current limiting rules sentinel-gateway-127-21002 , Not familiar with sentinel Refer to my last blog
[
{
"resource": "calculate",
"limitApp": "default",
"grade": 1,
"count": 1,
"strategy": 0,
"controlBehavior": 0,
"clusterMode": false
}
]
bootstrap.yaml Read nacos To configure
spring:
cloud:
nacos:
config:
server-addr: 127.0.0.1:8848
group: DEFAULT_GROUP
namespace: 2022-4-1-dev
username: nacos
password: nacos
file-extension: yaml
refresh-enabled: true
prefix: ${
config-file-name}
Start project :idea Edit Configurations Configure startup parameters config-file-name=gateway-127-21000
(nacos The configuration file id), Or hit jar Add at run time --config-file-name=gateway-127-21000
2、SPRING-BOOT-SERVICE-DISCOVERY-CONSUMER service
The parent module depends on , Main designation nacos and cloud edition
<properties>
<java.version>1.8</java.version>
<springcloud.version>Hoxton.SR12</springcloud.version>
<springcloud.nacos.version>2.2.7.RELEASE</springcloud.nacos.version>
</properties>
<dependencyManagement>
<dependencies>
<!--spring cloud Hoxton.SR1-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${springcloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${springcloud.nacos.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
nacos Console add configuration boot-discovery-127-20001
server:
port: 20001
servlet:
context-path: /service # gateway gateway , If you don't configure ,controller Each interface of the control layer needs to add this path
spring:
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
username: nacos
password: nacos
namespace: 2022-4-1-prod
cluster-name: SPRINGBOOT
ephemeral: true
enabled: true
group: DEFAULT_GROUP
ip: 127.0.0.1
metadata:
username: admin
password: 123456
weight: 1
config:
server-addr: 127.0.0.1:8848
sentinel:
enabled: true
transport:
port: 8722
dashboard: 127.0.0.1:28080
clientIp: 127.0.0.1
log:
dir: I:\nacos colony \boot-20001-sentinel
datasource:
# Random names
ds:
nacos:
server-addr: 127.0.0.1:8848
username: nacos
password: nacos
data-id: sentinel-127-20001
group-id: DEFAULT_GROUP
namespace: 2022-4-24-sentinel
data-type: json
rule-type: flow
application:
name: SPRING-BOOT-SERVICE-DISCOVERY-CONSUMER
zipkin:
base-url: http://127.0.0.1:9411
sender:
type: web
locator:
discovery:
enabled: true
sleuth:
sampler:
probability: 1.0
nacos To configure sentinel Current limiting rules sentinel-127-20001
[
{
"resource": "/discovery/rule",
"limitApp": "default",
"grade": 1,
"count": 1,
"strategy": 0,
"controlBehavior": 0,
"clusterMode": false
}
]
bootstrap.yaml Read nacos To configure
spring:
cloud:
nacos:
config:
server-addr: 127.0.0.1:8848
group: DEFAULT_GROUP
namespace: 2022-4-1-dev
username: nacos
password: nacos
file-extension: yaml
refresh-enabled: true
prefix: ${
config-file-name}
controller layer /hystrix/calculate Interface
@RestController
@RequestMapping("/hystrix")
public class HystrixController {
@GetMapping("/calculate")
@ApiAnnotation
@SentinelResource(value = "calculate", blockHandler = "calculateHandleException",blockHandlerClass = ExceptionUtil.class,fallbackClass = FallbackUtil.class,fallback = "calculateFallback")
public JSONObject calculate(String a, String b, String type) throws InterruptedException {
TimeUnit.SECONDS.sleep(1);
int result = 0;
int num1 = Integer.parseInt(a);
int num2 = Integer.parseInt(b);
switch (type) {
case "+":result = num1 + num2;break;
case "-":result = num1 - num2;break;
case "*":result = num1 * num2;break;
case "/":result = num1 / num2;break;
}
JSONObject jsonObject = new JSONObject();
jsonObject.put("result",result);
return jsonObject;
}
}
Start the three SPRING-BOOT-SERVICE-DISCOVERY-CONSUMER
service , You need to add... At startup config-file-name= boot-discovery-127-20001
To configure , The three service port numbers are 10001,20001,30001
3、SPRING-CLOUD-SERVICE-OPENFEIGN service
The service didn't go nacos Reading configuration , Not used spring-cloud-starter-alibaba-nacos-config
The parent module depends on
<!-- After sub module inheritance , Provide a role : Lock version + Son module Do not write groupId and version-->
<dependencyManagement><!-- Define specification , But don't import -->
<dependencies>
<!--spring cloud Hoxton.SR1-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR12</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--spring cloud Alibaba -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.7.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-okhttp</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
nacos To configure sentinel Current limiting rules sentinel-openfeign-127-20004
[
{
"resource": "/discovery/rule",
"limitApp": "default",
"grade": 1,
"count": 1,
"strategy": 0,
"controlBehavior": 0,
"clusterMode": false
}
]
OpenFeign Specify service invocation
path = “/service” Express SPRING-BOOT-SERVICE-DISCOVERY-CONSUMER All interfaces of the service are added /service Prefix
@FeignClient(name = "SPRING-BOOT-SERVICE-DISCOVERY-CONSUMER",path = "/service")
@Service
public interface BootDiscoveryClient {
@GetMapping(value = "/hystrix/calculate")
String serviceDown(@SpringQueryMap Map map);
}
controller Control layer /hystrix/calculate Interface
@RestController
@RequestMapping("/hystrix")
public class HystrixController {
@Autowired
private BootDiscoveryClient bootDiscoveryClient;
@GetMapping(value = "/calculate")
@SentinelResource(value = "calculate")
public String serviceDown(String a,String b,String type) {
Map<String,String> map = new HashMap<>();
map.put("a",a);
map.put("b",b);
map.put("type",type);
return bootDiscoveryClient.serviceDown(map);
}
}
Four 、 Start the test
1、nacos Console
nacos The service list starts three SPRING-BOOT-SERVICE-DISCOVERY-CONSUMER
service
2、Sentinel Console
Sentinel The flow control list can be seen from nacos Loaded flow control rules
3、Zipkin Console
visit http://127.0.0.1:21000/service1/hystrix/calculate?a=10&b=5&type=/ Interface ( Use gateway Address of the project ), stay zipkin View link , Will find gateway According to your configuration routers Rule call SPRING-CLOUD-SERVICE-OPENFEIGN service , The service has been called 20001 Port of SPRING-BOOT-SERVICE-DISCOVERY-CONSUMER service
View dependencies ( Click on each service , You can see detailed statistics )
边栏推荐
- SQL operation database syntax
- Network security - summary and thinking of easy-to-use fuzzy tester
- OpenFeign 简单使用
- C language custom types - structure, bit segment (anonymous structure, self reference of structure, memory alignment of structure)
- Force buckle method summary: sliding window
- Honeypot attack and defense drill landing application scheme
- Simple implementation scheme of transcoding and streaming (I)
- Implementation of bidirectional linked list (simple difference, connection and implementation between bidirectional linked list and unidirectional linked list)
- Use the kaggle training model and download your own training model
- 什么是SQL注入
猜你喜欢
web安全--逻辑越权
Fundamentals of music theory (brief introduction)
HCIA—数据链路层
【无标题】
HCIA—應用層
sqli-labs第2关
Use Wireshark to grab TCP three handshakes
On November 24, we celebrate the "full moon"
When a custom exception encounters reflection
C language custom type enumeration, Union (clever use of enumeration, calculation of union size)
随机推荐
Constant pointer and pointer constant
Short video with goods source code, double-click to zoom in when watching the video
Use of OpenCV 6.4 median filter
sqli-labs第12关
Viewing JS array through V8
Development of digital collection trading website development of metauniverse digital collection
程序猿学英语-Learning C
HCIA—應用層
用数字 5,5,5,1 ,进行四则运算,每个数字当且仅当用一次,要求运算结果值为 24
Intelligent manufacturing solutions digital twin smart factory
OpenCV 6.4 中值滤波器的使用
C language custom types - structure, bit segment (anonymous structure, self reference of structure, memory alignment of structure)
Summary of one question per day: linked list (continuously updated)
St-link connection error invalid ROM table of STM32 difficult and miscellaneous diseases
What is SQL injection
One of the reasons for WCF update service reference error
Opencv common method source link (continuous update)
STM32-新建工程(参考正点原子)
Shortcut key to comment code and cancel code in idea
zipkin 简单使用