Simplify the code to only expose 2 endpoints
This commit is contained in:
@@ -1,30 +1,32 @@
|
||||
## Kubernetes Zipkin Example
|
||||
|
||||
This project demonstrates how a Spring Boot application generating statistics as Spring Cloud Sleuth Spans/traces can send them to a ZipKin server deployed in Kubernetes without the need to
|
||||
configure the baseUrl of the ZipKin server deployed as the server will be discovered. The spans/traces generated can be viewed within the zipkin dashboard under the `serviceName=sleuth-zipkin`
|
||||
This project demonstrates how a Spring Boot application generating statistics as Spring Cloud Sleuth Spans/Traces can send them to a ZipKin server deployed in Kubernetes without the need to
|
||||
configure the baseUrl of the ZipKin server deployed as the server will be discovered. The spans/traces generated can be viewed within the Zipkin dashboard under the `serviceName=sleuth-zipkin`
|
||||
|
||||
The Zipkin server is deployed according to the steps described within the Minishift or Minikube section.
|
||||
The Zipkin server is deployed according to the steps described within the `Minishift` or `Minikube` section.
|
||||
|
||||
The project exposes under the `TraceController` different endpoints that you can play with in order to generate traces in sync/async way, to associate a trace with a Span
|
||||
...
|
||||
The project exposes under the `TraceController` 2 endpoints `/` and `/hi` that you can play with in order to generate traces. When you call the root endpoint `/`, then
|
||||
it will issue a call against the second endpoint `/hi` and you will receive `/hi/hello` as response. If you look to the Zipkin dashboard, you will be able to get 2 traces recorded.
|
||||
|
||||
Here is an example of such implementation
|
||||
|
||||
```
|
||||
@RequestMapping("/traced")
|
||||
public String traced() throws InterruptedException {
|
||||
Span span = this.tracer.createSpan("http:customTraceEndpoint",
|
||||
new AlwaysSampler());
|
||||
int millis = this.random.nextInt(1000);
|
||||
log.info(String.format("Sleeping for [%d] millis", millis));
|
||||
Thread.sleep(millis);
|
||||
this.tracer.addTag("random-sleep-millis", String.valueOf(millis));
|
||||
@RequestMapping("/")
|
||||
public String say() throws InterruptedException {
|
||||
Thread.sleep(this.random.nextInt(1000));
|
||||
log.info("Home");
|
||||
String s = this.restTemplate.getForObject("http://localhost:" + this.port
|
||||
+ "/hi", String.class);
|
||||
return "hi/" + s;
|
||||
}
|
||||
|
||||
String s = this.restTemplate.getForObject("http://localhost:" + this.port
|
||||
+ "/call", String.class);
|
||||
this.tracer.close(span);
|
||||
return "traced/" + s;
|
||||
}
|
||||
@RequestMapping("/hi")
|
||||
public String hi() throws InterruptedException {
|
||||
log.info("hi");
|
||||
int millis = this.random.nextInt(1000);
|
||||
Thread.sleep(millis);
|
||||
this.tracer.addTag("random-sleep-millis", String.valueOf(millis));
|
||||
return "hello";
|
||||
}
|
||||
```
|
||||
|
||||
### Running the example
|
||||
@@ -90,9 +92,7 @@ like also the endpoint to call to generate traces
|
||||
```
|
||||
export ENDPOINT=$(minikube service kubernetes-zipkin --url)
|
||||
curl $ENDPOINT
|
||||
curl $ENDPOINT/call
|
||||
curl $ENDPOINT/traced
|
||||
curl $ENDPOINT/async
|
||||
curl $ENDPOINT/hi
|
||||
```
|
||||
|
||||
### Build/Deploy using Minishift
|
||||
@@ -106,7 +106,7 @@ we will install the circuit breaker and load balancing application
|
||||
oc new-project zipkin
|
||||
```
|
||||
|
||||
When using OpenShift, you must assign the `view` role to the *default* service account in the current project in orde to allow our Java Kubernetes Api to access
|
||||
When using OpenShift, you must assign the `view` role to the *default* service account in the current project in order to allow our Java Kubernetes Api to access
|
||||
the API Server :
|
||||
|
||||
```
|
||||
@@ -140,18 +140,16 @@ like also to deploy the application on the OpenShift platform in one maven line
|
||||
mvn clean install fabric8:deploy -Pkubernetes
|
||||
```
|
||||
|
||||
You can find the address of the zipkin server to be opened within your browser using this command
|
||||
You can find the address of the Zipkin server to be opened within your browser using this command
|
||||
|
||||
```
|
||||
minishift openshift service zipkin --url -n zipkin
|
||||
oc get route/zipkin --template='{{.spec.host}}'
|
||||
```
|
||||
|
||||
like also the endpoint to call to generate traces
|
||||
|
||||
```
|
||||
export ENDPOINT=$(minishift openshift service kubernetes-zipkin --url -n zipkin)
|
||||
export ENDPOINT=$(oc get route/kubernetes-zipkin --template='{{.spec.host}}')
|
||||
curl $ENDPOINT
|
||||
curl $ENDPOINT/call
|
||||
curl $ENDPOINT/traced
|
||||
curl $ENDPOINT/async
|
||||
curl $ENDPOINT/hi
|
||||
```
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2016 to the original authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.kubernetes.examples;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.sleuth.Tracer;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class SampleBackground {
|
||||
|
||||
@Autowired
|
||||
private Tracer tracer;
|
||||
@Autowired
|
||||
private Random random;
|
||||
|
||||
@Async
|
||||
public void background() throws InterruptedException {
|
||||
int millis = this.random.nextInt(1000);
|
||||
Thread.sleep(millis);
|
||||
this.tracer.addTag("background-sleep-millis", String.valueOf(millis));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -18,16 +18,13 @@
|
||||
package org.springframework.cloud.kubernetes.examples;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent;
|
||||
import org.springframework.cloud.sleuth.Span;
|
||||
import org.springframework.cloud.sleuth.SpanAccessor;
|
||||
import org.springframework.cloud.sleuth.Tracer;
|
||||
import org.springframework.cloud.sleuth.sampler.AlwaysSampler;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
@@ -44,77 +41,25 @@ ApplicationListener<EmbeddedServletContainerInitializedEvent> {
|
||||
@Autowired
|
||||
private Tracer tracer;
|
||||
@Autowired
|
||||
private SpanAccessor accessor;
|
||||
@Autowired
|
||||
private SampleBackground controller;
|
||||
@Autowired
|
||||
private Random random;
|
||||
private int port;
|
||||
|
||||
@RequestMapping("/")
|
||||
public String hi() throws InterruptedException {
|
||||
public String say() throws InterruptedException {
|
||||
Thread.sleep(this.random.nextInt(1000));
|
||||
log.info("Home page");
|
||||
log.info("Home");
|
||||
String s = this.restTemplate.getForObject("http://localhost:" + this.port
|
||||
+ "/hi2", String.class);
|
||||
+ "/hi", String.class);
|
||||
return "hi/" + s;
|
||||
}
|
||||
|
||||
@RequestMapping("/call")
|
||||
public Callable<String> call() {
|
||||
return new Callable<String>() {
|
||||
@Override
|
||||
public String call() throws Exception {
|
||||
int millis = TraceController.this.random.nextInt(1000);
|
||||
Thread.sleep(millis);
|
||||
TraceController.this.tracer.addTag("callable-sleep-millis", String.valueOf(millis));
|
||||
Span currentSpan = TraceController.this.accessor.getCurrentSpan();
|
||||
return "async hi: " + currentSpan;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@RequestMapping("/async")
|
||||
public String async() throws InterruptedException {
|
||||
log.info("async");
|
||||
this.controller.background();
|
||||
return "ho";
|
||||
}
|
||||
|
||||
@RequestMapping("/hi2")
|
||||
public String hi2() throws InterruptedException {
|
||||
log.info("hi2");
|
||||
@RequestMapping("/hi")
|
||||
public String hi() throws InterruptedException {
|
||||
log.info("hi");
|
||||
int millis = this.random.nextInt(1000);
|
||||
Thread.sleep(millis);
|
||||
this.tracer.addTag("random-sleep-millis", String.valueOf(millis));
|
||||
return "hi2";
|
||||
}
|
||||
|
||||
@RequestMapping("/traced")
|
||||
public String traced() throws InterruptedException {
|
||||
Span span = this.tracer.createSpan("http:customTraceEndpoint",
|
||||
new AlwaysSampler());
|
||||
int millis = this.random.nextInt(1000);
|
||||
log.info(String.format("Sleeping for [%d] millis", millis));
|
||||
Thread.sleep(millis);
|
||||
this.tracer.addTag("random-sleep-millis", String.valueOf(millis));
|
||||
|
||||
String s = this.restTemplate.getForObject("http://localhost:" + this.port
|
||||
+ "/call", String.class);
|
||||
this.tracer.close(span);
|
||||
return "traced/" + s;
|
||||
}
|
||||
|
||||
@RequestMapping("/start")
|
||||
public String start() throws InterruptedException {
|
||||
int millis = this.random.nextInt(1000);
|
||||
log.info(String.format("Sleeping for [%d] millis", millis));
|
||||
Thread.sleep(millis);
|
||||
this.tracer.addTag("random-sleep-millis", String.valueOf(millis));
|
||||
|
||||
String s = this.restTemplate.getForObject("http://localhost:" + this.port
|
||||
+ "/call", String.class);
|
||||
return "start/" + s;
|
||||
return "hello";
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -44,16 +44,4 @@ public class ZipkinApplication {
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
||||
// Use this for debugging (or if there is no Zipkin server running on port 9411)
|
||||
@Bean
|
||||
@ConditionalOnProperty(value = "sample.zipkin.enabled", havingValue = "false")
|
||||
public ZipkinSpanReporter spanCollector() {
|
||||
return new ZipkinSpanReporter() {
|
||||
@Override
|
||||
public void report(zipkin.Span span) {
|
||||
log.info(String.format("Reporting span [%s]", span));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user