Skip to content

Commit

Permalink
链路追踪demo
Browse files Browse the repository at this point in the history
  • Loading branch information
provenceee committed Feb 1, 2024
1 parent 69f0cf6 commit 9e33277
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,9 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.time.LocalDateTime;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

import javax.annotation.PostConstruct;

/**
Expand All @@ -27,51 +21,24 @@ public class ConsumerController {
@Value("${request.ip:127.0.0.1:8092}")
private String ip;

@Value("${request.path:/unit-provider/hello}")
private String path;

@Autowired
private RestTemplate restTemplate;

@PostConstruct
public void init() {
PROVIDER_URL = "http://" + ip + "/unit-provider/hello";
PROVIDER_URL = "http://" + ip + path;
}

/**
* 测试方法
*
* @param times 持续访问时间
* @return msg
* @throws InterruptedException ex
*/
@GetMapping("unit-consumer/retry")
public String retry(@RequestParam(value = "times", defaultValue = "60") int times) throws InterruptedException {
StringBuffer sb = new StringBuffer();
ExecutorService executorService = Executors.newSingleThreadExecutor();
AtomicInteger timer = new AtomicInteger();
executorService.execute(() -> {
int count = 1;
while (timer.get() < times) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
try {
String result = restTemplate.getForObject(PROVIDER_URL, String.class);
sb.append(count).append("|").append(LocalDateTime.now()).append("|info|").append(result);
} catch (Exception e) {
sb.append(count).append("|").append(LocalDateTime.now()).append("|error|").append(e.getMessage());
}
sb.append(System.lineSeparator());
count++;
}
});
while (timer.getAndIncrement() < times) {
Thread.sleep(1000);
}
executorService.shutdown();
while (!executorService.isTerminated()) {
Thread.sleep(100);
}
return sb.toString();
@GetMapping("unit-consumer/hello")
public String hello() {
return restTemplate.getForObject(PROVIDER_URL, String.class);
}
}
3 changes: 2 additions & 1 deletion unit-provider/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<artifactId>unit-demo</artifactId>
<groupId>com.huaweicloud.servicestage.demo</groupId>
<version>1.0.0</version>
<relativePath/>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand All @@ -24,7 +25,7 @@
</dependency>
<dependency>
<groupId>com.huaweicloud</groupId>
<artifactId>spring-cloud-starter-huawei-nacos</artifactId>
<artifactId>spring-cloud-starter-huawei-service-engine</artifactId>
</dependency>
</dependencies>

Expand Down
15 changes: 11 additions & 4 deletions unit-provider/src/main/resources/bootstrap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@ spring:
# 微服务名称,本示例使用固定值(可替换成自己想要的名字),因为微服务名称会被客户端使用,不能轻易变化。
name: unit-provider
cloud:
nacos:
servicecomb:
discovery:
enabled: true
server-addr: ${NACOS_SERVER_ADDRESS:http://127.0.0.1:8848}
# 应用名称,本示例固定值(可替换成自己想要的名字),因为只有应用名称相同的微服务才能够相互发现,不能轻易变化。
appName: unit-application
serviceName: ${spring.application.name}
# 注册中心地址,本示例使用ServiceStage环境变量。建议保留这种配置方式,部署的时候,不用手工修改地址。
address: ${PAAS_CSE_SC_ENDPOINT:http://127.0.0.1:30100}
# 微服务版本号,本示例使用ServiceStage环境变量。建议保留这种配置方式,部署的时候,不用手工修改版本号,防止契约注册失败。
version: ${CAS_INSTANCE_VERSION:0.0.1}
config:
server-addr: ${NACOS_SERVER_ADDRESS:http://127.0.0.1:8848}
file-extension: yaml
# 配置中心地址,本示例使用ServiceStage环境变量。建议保留这种配置方式,部署的时候,不用手工修改地址。
serverAddr: ${PAAS_CSE_CC_ENDPOINT:http://127.0.0.1:30110}
serverType: kie

0 comments on commit 9e33277

Please sign in to comment.