diff --git a/src/adservice-v2/README.md b/src/adservice-v2/README.md index 553d9a239f..779cf4cf8f 100644 --- a/src/adservice-v2/README.md +++ b/src/adservice-v2/README.md @@ -12,6 +12,12 @@ This repo re-implements opentelemetry-demo-webstore's adservice with nacos regis - [x] nacos - [x] sentinel +## get data from dataservice + +if `-Dspring.dataService.enabled` is true, adservice will call dataservice to get Ad data, +if `-Dspring.cloud.nacos.config.enabled` is true, adservice use service name registry in nacos as addr to call dataservice, +otherwise it uses host:port as addr. + ## curl ```shell diff --git a/src/adservice-v2/src/main/java/org/daocloud/springcloud/adservice/controller/AdController.java b/src/adservice-v2/src/main/java/org/daocloud/springcloud/adservice/controller/AdController.java index f29a288c47..01533eb138 100644 --- a/src/adservice-v2/src/main/java/org/daocloud/springcloud/adservice/controller/AdController.java +++ b/src/adservice-v2/src/main/java/org/daocloud/springcloud/adservice/controller/AdController.java @@ -21,6 +21,10 @@ public AdController(final RestTemplate restTemplate) { @Value("${data_service_name:dataservice}") private String dataServiceName; + @GetMapping("/ad-key/{adKey}") + public ResponseEntity findByAdKey(@PathVariable String key) { + return this.restTemplate.getForEntity("http://" + dataServiceName + "ad/ad-key/{key}", Advertise[].class, key); + } @GetMapping("/{id}") public ResponseEntity findById(@PathVariable Long id) { return this.restTemplate.getForEntity("http://" + dataServiceName + "/ad/{id}", Advertise.class, id); diff --git a/src/adservice-v2/src/main/java/org/daocloud/springcloud/adservice/dto/Advertise.java b/src/adservice-v2/src/main/java/org/daocloud/springcloud/adservice/dto/Advertise.java index a647fc24dc..f829b1fb4f 100644 --- a/src/adservice-v2/src/main/java/org/daocloud/springcloud/adservice/dto/Advertise.java +++ b/src/adservice-v2/src/main/java/org/daocloud/springcloud/adservice/dto/Advertise.java @@ -3,21 +3,42 @@ public class Advertise { private Integer id; + private String adKey; + + + private String redirectURL; + private String content; public Integer getId() { return id; } - public void setId(final Integer id) { + public void setId(Integer id) { this.id = id; } + public String getAdKey() { + return adKey; + } + + public void setAdKey(String adKey) { + this.adKey = adKey; + } + + public String getRedirectURL() { + return redirectURL; + } + + public void setRedirectURL(String redirectURL) { + this.redirectURL = redirectURL; + } + public String getContent() { return content; } - public void setContent(final String content) { + public void setContent(String content) { this.content = content; } } diff --git a/src/adservice-v2/src/main/java/org/daocloud/springcloud/adservice/service/AdServiceGrpcService.java b/src/adservice-v2/src/main/java/org/daocloud/springcloud/adservice/service/AdServiceGrpcService.java index a508eadf8a..8bda9edce5 100644 --- a/src/adservice-v2/src/main/java/org/daocloud/springcloud/adservice/service/AdServiceGrpcService.java +++ b/src/adservice-v2/src/main/java/org/daocloud/springcloud/adservice/service/AdServiceGrpcService.java @@ -18,10 +18,14 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.daocloud.springcloud.adservice.Interceptor.MeterInterceptor; +import org.daocloud.springcloud.adservice.controller.AdController; +import org.daocloud.springcloud.adservice.dto.Advertise; import org.daocloud.springcloud.adservice.meter.Meter; import org.ejml.simple.SimpleMatrix; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.ResponseEntity; +import org.springframework.web.client.RestTemplate; import oteldemo.AdServiceGrpc; import oteldemo.Demo.Ad; import oteldemo.Demo.AdRequest; @@ -38,6 +42,9 @@ public class AdServiceGrpcService extends AdServiceGrpc.AdServiceImplBase { @Autowired private Meter meterProvider; + @Autowired + private AdController adController; + @Value("${spring.extraAdLabel}") private String text; @@ -47,6 +54,12 @@ public class AdServiceGrpcService extends AdServiceGrpc.AdServiceImplBase { @Value("${spring.matrixRow}") private int matrixRow; + @Value("${spring.dataService.enabled}") + private boolean dataServiceEnabled; + + @Value("${spring.cloud.nacos.config.enabled}") + private boolean nacosEnabled; + @Override public void getAds(AdRequest req, StreamObserver responseObserver) { // get the current span in context @@ -61,26 +74,56 @@ public void getAds(AdRequest req, StreamObserver responseObserver) { // do matrixCalculate matrixCalculate(matrixRow); - if(randomError){ - //throw random error - Random random = new Random(); - int r = random.nextInt(100)+1; - if(r > 50){ - throw new StatusRuntimeException(Status.INTERNAL.withDescription("connect Canceled randomly")); + if(dataServiceEnabled){ + logger.info("data service is enabled, get Ad data from dataservice"); + ResponseEntity advertiseResponseEntity = null; + if(nacosEnabled){ + logger.info("nacos is enabled, call dataservice by nacos registry"); + for (int i = 0; i < req.getContextKeysCount(); i++){ + advertiseResponseEntity = adController.findByAdKey(req.getContextKeys(i)); + } + }else{ + logger.info("nacos is disabled, call dataservice by raw http"); + String dataServiceAddr = System.getenv("DATA_SERVICE_ADDR"); + RestTemplate restTemplate = new RestTemplate(); + for (int i = 0; i < req.getContextKeysCount(); i++){ + advertiseResponseEntity = restTemplate.getForEntity("http://"+dataServiceAddr+"/ad/ad-key/{key}",Advertise[].class,req.getContextKeys(i)); + } } - } - if (req.getContextKeysCount() > 0) { - for (int i = 0; i < req.getContextKeysCount(); i++) { - Collection ads = getAdsByCategory(req.getContextKeys(i)); - allAds.addAll(ads); + assert advertiseResponseEntity != null; + Advertise[] advertises = advertiseResponseEntity.getBody(); + + assert advertises != null; + for(Advertise a: advertises){ + Ad ad = Ad.newBuilder() + .setRedirectUrl(a.getRedirectURL()) + .setText(a.getContent()) + .build(); + allAds.add(ad);; + } + }else { + if(randomError){ + //throw random error + Random random = new Random(); + int r = random.nextInt(100)+1; + if(r > 50){ + throw new StatusRuntimeException(Status.INTERNAL.withDescription("connect Canceled randomly")); + } + } + + if (req.getContextKeysCount() > 0) { + for (int i = 0; i < req.getContextKeysCount(); i++) { + Collection ads = getAdsByCategory(req.getContextKeys(i)); + allAds.addAll(ads); + } + } else { + allAds = getRandomAds(); + } + if (allAds.isEmpty()) { + // Serve random ads. + allAds = getRandomAds(); } - } else { - allAds = getRandomAds(); - } - if (allAds.isEmpty()) { - // Serve random ads. - allAds = getRandomAds(); } List allAdsLabeled = addLabel(allAds); diff --git a/src/adservice-v2/src/main/resources/bootstrap.yml b/src/adservice-v2/src/main/resources/bootstrap.yml index 8a9540a73a..14a193c59e 100644 --- a/src/adservice-v2/src/main/resources/bootstrap.yml +++ b/src/adservice-v2/src/main/resources/bootstrap.yml @@ -146,3 +146,5 @@ spring: extraAdLabel: Daocloud randomError: true matrixRow: 200 + dataService: + enabled: false