应用程序名称: 在属性文件中定义的应用程序的名称。
跟踪ID: 侦探将添加跟踪ID。对于给定的请求,在所有服务中都保持不变。
跨度ID: 侦探还添加了跨度ID。对于一个给定的请求,它在一个工作单元中保持不变,但是对于不同的服务而言却是不同的。
Zipkin导出标志: 表示布尔值。它可以是 true 或
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-sleuth</artifactId> </dependency>
@Bean public Sampler defaultSampler() { return Sampler.ALWAYS_SAMPLE; }
package com.lidihuo.microservices.netflixzuulapigatewayserver; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; import org.springframework.context.annotation.Bean; import brave.sampler.Sampler; @SpringBootApplication @EnableDiscoveryClient @EnableZuulProxy public class NetflixZuulApiGatewayServerApplication { public static void main(String[] args) { SpringApplication.run(NetflixZuulApiGatewayServerApplication.class, args); } //creating a bean @Bean //creating a sampler called public Sampler defaultSampler() { return Sampler.ALWAYS_SAMPLE; } }
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-sleuth</artifactId> </dependency>
package com.lidihuo.microservices.currencyexchangeservice; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.context.annotation.Bean; import brave.sampler.Sampler; @SpringBootApplication @EnableDiscoveryClient public class CurrencyExchangeServiceApplication { public static void main(String[] args) { SpringApplication.run(CurrencyExchangeServiceApplication.class, args); } @Bean //creating a sampler called always sampler public Sampler defaultSampler() { return Sampler.ALWAYS_SAMPLE; } }
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-sleuth</artifactId> </dependency>
package com.lidihuo.microservices.currencyconversionservice; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.context.annotation.Bean; import brave.sampler.Sampler; @SpringBootApplication @EnableFeignClients("com.lidihuo.microservices.currencyconversionservice") @EnableDiscoveryClient public class CurrencyConversionServiceApplication { public static void main(String[] args) { SpringApplication.run(CurrencyConversionServiceApplication.class, args); } @Bean //creating a sampler called always sampler public Sampler defaultSampler() { return Sampler.ALWAYS_SAMPLE; } }
打开浏览器并调用URL http://localhost:8761 。它返回Eureka界面,如下所示。
package com.lidihuo.microservices.currencyexchangeservice; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.core.env.Environment; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController public class CurrencyExchangeController { private Logger logger=LoggerFactory.getLogger(this.getClass()); @Autowired private Environment environment; @Autowired private ExchangeValueRepository repository; @GetMapping("/currency-exchange/from/{from}/to/{to}") //where {from} and {to} are path variable public ExchangeValue retrieveExchangeValue(@PathVariable String from, @PathVariable String to) //from map to USD and to map to INR { ExchangeValue exchangeValue = repository.findByFromAndTo(from, to); //setting the port exchangeValue.setPort(Integer.parseInt(environment.getProperty("local.server.port"))); logger.info("{}", exchangeValue); return exchangeValue; } }
package com.lidihuo.microservices.currencyconversionservice; import java.math.BigDecimal; import java.util.HashMap; import java.util.Map; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController public class CurrencyConversionController { private Logger logger=LoggerFactory.getLogger(this.getClass()); @Autowired private CurrencyExchangeServiceProxy proxy; @GetMapping("/currency-converter/from/{from}/to/{to}/quantity/{quantity}") //where {from} and {to} represents the column //returns a bean back public CurrencyConversionBean convertCurrency(@PathVariable String from, @PathVariable String to, @PathVariable BigDecimal quantity) { //setting variables to currency exchange service Map<String, String> uriVariables=new HashMap<>(); uriVariables.put("from", from); uriVariables.put("to", to); //calling the currency exchange service ResponseEntity<CurrencyConversionBean> responseEntity=new RestTemplate().getForEntity("http://localhost:8000/currency-exchange/from/{from}/to/{to}", CurrencyConversionBean.class, uriVariables); CurrencyConversionBean response=responseEntity.getBody(); //creating a new response bean and getting the response back and taking it into Bean return new CurrencyConversionBean(response.getId(), from, to, response.getConversionMultiple(), quantity, quantity.multiply(response.getConversionMultiple()), response.getPort()); } //mapping for currency-converter-feign service @GetMapping("/currency-converter-feign/from/{from}/to/{to}/quantity/{quantity}") //where {from} and {to} represents the column //returns a bean public CurrencyConversionBean convertCurrencyFeign(@PathVariable String from, @PathVariable String to, @PathVariable BigDecimal quantity) { CurrencyConversionBean response=proxy.retrieveExchangeValue(from, to); logger.info("{}", response); //creating a new response bean //getting the response back and taking it into Bean return new CurrencyConversionBean(response.getId(), from, to, response.getConversionMultiple(), quantity, quantity.multiply(response.getConversionMultiple()), response.getPort()); } }
ELK Stack (Elastic Search)
Kibana
Zipkin