Skip to content

Commit

Permalink
重试demo
Browse files Browse the repository at this point in the history
  • Loading branch information
provenceee committed Dec 9, 2023
1 parent 8351522 commit 69f0cf6
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 95 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.14</version>
<version>2.7.10</version>
<relativePath/>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand All @@ -23,8 +23,8 @@
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<spring.cloud.version>2020.0.6</spring.cloud.version>
<spring.cloud.huawei.version>1.10.4-2020.0.x</spring.cloud.huawei.version>
<spring.cloud.version>2021.0.5</spring.cloud.version>
<spring.cloud.huawei.version>1.11.2-2021.0.x</spring.cloud.huawei.version>
</properties>

<dependencyManagement>
Expand Down
4 changes: 0 additions & 4 deletions unit-consumer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.huaweicloud</groupId>
<artifactId>spring-cloud-starter-huawei-service-engine</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

Expand All @@ -29,7 +28,6 @@ public static void main(String[] args) {
* @return RestTemplate
*/
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
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.util.HashMap;
import java.util.Map;
import java.time.LocalDateTime;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

import javax.annotation.PostConstruct;

/**
* controller
Expand All @@ -17,36 +22,56 @@
*/
@RestController
public class ConsumerController {
private static final String PROVIDER_URL = "http://unit-provider/unit-provider/hello";

@Value("${spring.application.name}")
private String name;

@Value("${spring.cloud.servicecomb.discovery.datacenter.name}")
private String datacenterName;
private static String PROVIDER_URL;

@Value("${spring.cloud.servicecomb.discovery.datacenter.region}")
private String region;

@Value("${spring.cloud.servicecomb.discovery.datacenter.availableZone}")
private String availableZone;
@Value("${request.ip:127.0.0.1:8092}")
private String ip;

@Autowired
private RestTemplate restTemplate;

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

/**
* 测试方法
*
* @param times 持续访问时间
* @return msg
* @throws InterruptedException ex
*/
@GetMapping("unit-consumer/hello")
public Map<String, Object> hello() {
Map<String, String> msg = new HashMap<>();
msg.put("datacenterName", datacenterName);
msg.put("region", region);
msg.put("availableZone", availableZone);
Map<String, Object> map = new HashMap<>(restTemplate.getForObject(PROVIDER_URL, Map.class));
map.put(name, msg);
return map;
@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();
}
}
7 changes: 6 additions & 1 deletion unit-consumer/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
server:
port: 8091
port: 8091

spring:
application:
# 微服务名称,本示例使用固定值(可替换成自己想要的名字),因为微服务名称会被客户端使用,不能轻易变化。
name: unit-consumer
28 changes: 1 addition & 27 deletions unit-consumer/src/main/resources/bootstrap.yaml
Original file line number Diff line number Diff line change
@@ -1,30 +1,4 @@
spring:
application:
# 微服务名称,本示例使用固定值(可替换成自己想要的名字),因为微服务名称会被客户端使用,不能轻易变化。
name: unit-consumer
cloud:
servicecomb:
discovery:
# 应用名称,本示例固定值(可替换成自己想要的名字),因为只有应用名称相同的微服务才能够相互发现,不能轻易变化。
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}
# 启用数据中心亲和
enabledZoneAware: true
# 本单元无实例时,也不允许跨单元调用
denyCrossZoneLoadBalancing: true
datacenter:
name: ou1
region: chengdu.ou1
availableZone: az1.du1
instance:
properties:
"[cse.io/ou]": ou1
"[cse.io/du]": du1
config:
# 配置中心地址,本示例使用ServiceStage环境变量。建议保留这种配置方式,部署的时候,不用手工修改地址。
serverAddr: ${PAAS_CSE_CC_ENDPOINT:http://127.0.0.1:30110}
serverType: kie
name: unit-consumer
2 changes: 1 addition & 1 deletion unit-provider/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</dependency>
<dependency>
<groupId>com.huaweicloud</groupId>
<artifactId>spring-cloud-starter-huawei-service-engine</artifactId>
<artifactId>spring-cloud-starter-huawei-nacos</artifactId>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.huaweicloud.servicestage.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.commons.util.InetUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.PostConstruct;

/**
* controller
*
Expand All @@ -16,19 +20,20 @@
*/
@RestController
public class ProviderController {
@Autowired
private InetUtils inetUtils;

@Value("${spring.application.name}")
private String name;

@Value("${spring.cloud.servicecomb.discovery.datacenter.name}")
private String datacenterName;

@Value("${spring.cloud.servicecomb.discovery.datacenter.region}")
private String region;
private boolean ex;

@Value("${spring.cloud.servicecomb.discovery.datacenter.availableZone}")
private String availableZone;
private String ip;

private boolean ex;
@PostConstruct
public void init() {
ip = inetUtils.findFirstNonLoopbackAddress().getHostAddress();
}

/**
* 测试方法
Expand All @@ -42,16 +47,16 @@ public Map<String, Object> hello() throws Exception {
throw new Exception("exception");
}
Map<String, String> msg = new HashMap<>();
msg.put("datacenterName", datacenterName);
msg.put("region", region);
msg.put("availableZone", availableZone);
msg.put("ip", ip);
Map<String, Object> map = new HashMap<>();
map.put(name, msg);
return map;
}

/**
* 测试方法
*
* @param enabled enabled
*/
@GetMapping("unit-provider/ex")
public void ex(@RequestParam("enabled") boolean enabled) {
Expand Down
28 changes: 5 additions & 23 deletions unit-provider/src/main/resources/bootstrap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,10 @@ spring:
# 微服务名称,本示例使用固定值(可替换成自己想要的名字),因为微服务名称会被客户端使用,不能轻易变化。
name: unit-provider
cloud:
servicecomb:
nacos:
discovery:
# 应用名称,本示例固定值(可替换成自己想要的名字),因为只有应用名称相同的微服务才能够相互发现,不能轻易变化。
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}
# 启用数据中心亲和
enabledZoneAware: true
# 本单元无实例时,也不允许跨单元调用
denyCrossZoneLoadBalancing: true
datacenter:
name: ou1
region: chengdu.ou1
availableZone: az1.du1
instance:
properties:
ou: ou1
du: du1
enabled: true
server-addr: ${NACOS_SERVER_ADDRESS:http://127.0.0.1:8848}
config:
# 配置中心地址,本示例使用ServiceStage环境变量。建议保留这种配置方式,部署的时候,不用手工修改地址。
serverAddr: ${PAAS_CSE_CC_ENDPOINT:http://127.0.0.1:30110}
serverType: kie
server-addr: ${NACOS_SERVER_ADDRESS:http://127.0.0.1:8848}
file-extension: yaml

0 comments on commit 69f0cf6

Please sign in to comment.