{"id":647,"date":"2023-03-23T21:42:05","date_gmt":"2023-03-23T13:42:05","guid":{"rendered":""},"modified":"2023-03-23T21:42:05","modified_gmt":"2023-03-23T13:42:05","slug":"SpringCloud \u5206\u5e03\u5f0f\u8ddf\u8e2a","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/647.html","title":{"rendered":"SpringCloud \u5206\u5e03\u5f0f\u8ddf\u8e2a"},"content":{"rendered":"
\n
<\/p>\n
\u5e94\u7528\u7a0b\u5e8f\u540d\u79f0: <\/strong>\u5728\u5c5e\u6027<\/strong>\u6587\u4ef6\u4e2d\u5b9a\u4e49\u7684\u5e94\u7528\u7a0b\u5e8f\u7684\u540d\u79f0\u3002<\/span> <\/code>\n <\/div>\n <\/code>\n <\/div>\n <\/code>\n <\/div>\n <\/code>\n <\/div>\n <\/code>\n <\/div>\n <\/code>\n <\/div>\n <\/code>\n <\/div>\n \u6253\u5f00\u6d4f\u89c8\u5668\u5e76\u8c03\u7528URL http:\/\/localhost:8761 \u3002\u5b83\u8fd4\u56deEureka\u754c\u9762\uff0c\u5982\u4e0b\u6240\u793a\u3002<\/span> <\/code>\n <\/div>\n <\/code>\n <\/div>\n
\n \u8ddf\u8e2aID: <\/strong>\u4fa6\u63a2\u5c06\u6dfb\u52a0\u8ddf\u8e2aID\u3002\u5bf9\u4e8e\u7ed9\u5b9a\u7684\u8bf7\u6c42\uff0c\u5728\u6240\u6709\u670d\u52a1\u4e2d\u90fd\u4fdd\u6301\u4e0d\u53d8\u3002<\/span>
\n \u8de8\u5ea6ID: <\/strong>\u4fa6\u63a2\u8fd8\u6dfb\u52a0\u4e86\u8de8\u5ea6ID\u3002\u5bf9\u4e8e\u4e00\u4e2a\u7ed9\u5b9a\u7684\u8bf7\u6c42\uff0c\u5b83\u5728\u4e00\u4e2a\u5de5\u4f5c\u5355\u5143\u4e2d\u4fdd\u6301\u4e0d\u53d8\uff0c\u4f46\u662f\u5bf9\u4e8e\u4e0d\u540c\u7684\u670d\u52a1\u800c\u8a00\u5374\u662f\u4e0d\u540c\u7684\u3002<\/span>
\n Zipkin\u5bfc\u51fa\u6807\u5fd7: <\/strong>\u8868\u793a\u5e03\u5c14\u503c\u3002\u5b83\u53ef\u4ee5\u662f true <\/strong>\u6216<\/span> <\/p>\n <\/p>\n
\n Spring Cloud Sleuth <\/strong>\u3002\n <\/div>\n
\n netflix-zuul -api-gateway-server <\/strong>\u3002\n <\/div>\n
\n pom.xml <\/strong>\u5e76\u6dfb\u52a0
\n Sleuth <\/strong>\u4f9d\u8d56\u6027\u3002\n <\/div>\n\r\n<dependency>\r\n<groupId>org.springframework.cloud<\/groupId>\r\n<artifactId>spring-cloud-starter-sleuth<\/artifactId>\r\n<\/dependency>\r\n<\/pre>\n
\n ALWAYS_SAMPLE <\/strong>\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528
\n Bean <\/strong>\u521b\u5efa\u4e00\u4e2aSampler\u3002\n <\/div>\n
\n \u91c7\u6837\u5668<\/strong>\u7b56\u7565\u3002\u501f\u52a9Sampler\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u65bd\u63d0\u4f9b\u7b97\u6cd5\u63a7\u5236\u7684\u91c7\u6837\u7b97\u6cd5\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5982\u679c
\n span <\/strong>
\n (\u76f8\u5173\u6027: \u662f\u4e00\u4e2a\u5355\u72ec\u7684\u64cd\u4f5c)<\/strong>\u5df2\u7ecf\u5904\u4e8e\u6d3b\u52a8\u72b6\u6001\uff0c\u6211\u4eec\u5c06\u83b7\u5f97\u4e00\u4e2a\u8fde\u7eed\u6267\u884c\u8ddf\u8e2a\u7684\u8fc7\u7a0b\u3002\n <\/div>\n
\n \u4e0d\u53ef\u5bfc\u51fa<\/strong>\u3002\u5982\u679c\u6240\u6709\u5e94\u7528\u7a0b\u5e8f\u90fd\u4e0eSampler\u4e00\u8d77\u8fd0\u884c\uff0c\u5219\u53ef\u4ee5\u5728\u65e5\u5fd7\u4e2d\u770b\u5230\u8ddf\u8e2a(\u7531\u8de8\u5ea6\u7ec4\u6210\u7684\u7aef\u5230\u7aef\u5ef6\u8fdf\u56fe)\uff0c\u800c\u4e0d\u662f\u5728\u4efb\u4f55\u8fdc\u7a0b\u4f4d\u7f6e\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cSpring Cloud Sleuth\u4f1a\u5c06\u6240\u6709\u8303\u56f4\u8bbe\u7f6e\u4e3a
\n \u4e0d\u53ef\u5bfc\u51fa<\/strong>\u3002\n <\/div>\n
\n Zipkin <\/strong>\u6216
\n Spring Cloud Stream\u65f6<\/strong>\uff0cSpring Cloud Sleuth\u63d0\u4f9b\u4e86
\n AlwaysSampler <\/strong>\u7c7b\uff0c\u8be5\u7c7b\u5c06\u6240\u6709\u5185\u5bb9\u5bfc\u51fa\u5230Zipkin\u3002\u5b83\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e2a
\n PercentageBasedSampler <\/strong>\u7c7b\uff0c\u8be5\u7c7b\u5bf9\u56fa\u5b9a\u8303\u56f4\u7684\u8de8\u5ea6\u8fdb\u884c\u91c7\u6837\u3002\n <\/div>\n
\n Spring 2.0.0 <\/strong>\u6216\u66f4\u9ad8\u7248\u672c\uff0c\u8bf7\u4f7f\u7528\u4ee5\u4e0b\u91c7\u6837\u5668\u3002\u4e4b\u6240\u4ee5\u4f7f\u7528\u76f8\u540c\u7684\u540d\u79f0\uff0c\u662f\u56e0\u4e3a\u6211\u4eec\u4f7f\u7528\u7684\u662fSpring\u7248\u672c
\n 2.2.1\u3002<\/strong>\n <\/div>\n\r\n@Bean\r\npublic <\/span>Sampler defaultSampler()\r\n{\r\nreturn <\/span>Sampler.ALWAYS_SAMPLE;\r\n}\r\n<\/pre>\n
\n NetflixZuulApiGatewayServerApplication.java <\/strong>\u6587\u4ef6\u5e76\u5b9a\u4e49\u4e00\u4e2a
\n Bean <\/strong>\u3002\n <\/div>\n\r\npackage <\/span>com.lidihuo.microservices.netflixzuulapigatewayserver;\r\nimport org.springframework.boot.SpringApplication;\r\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\r\nimport org.springframework.cloud.client.discovery.EnableDiscoveryClient;\r\nimport org.springframework.cloud.netflix.zuul.EnableZuulProxy;\r\nimport org.springframework.context.annotation.Bean;\r\nimport brave.sampler.Sampler;\r\n@SpringBootApplication\r\n@EnableDiscoveryClient\r\n@EnableZuulProxy\r\npublic <\/span>class <\/span>NetflixZuulApiGatewayServerApplication \r\n{\r\npublic <\/span>static void <\/span>main(String[] args) \r\n{\r\nSpringApplication.run<\/span>(NetflixZuulApiGatewayServerApplication.class, args);\r\n}\r\n\/\/creating a bean\r\n@Bean\r\n\/\/creating a sampler called \r\npublic <\/span>Sampler defaultSampler()\r\n{\r\nreturn <\/span>Sampler.ALWAYS_SAMPLE;\r\n}\r\n}\r\n<\/pre>\n
\n currency-exchange-service <\/strong>\u548c<\n <\/div>\n
\n currency-exchange-service\u7684 pom.xml <\/strong> <\/strong>\u5e76\u6dfb\u52a0
\n Sleuth <\/strong>\u4f9d\u8d56\u9879\u3002\n <\/div>\n\r\n<dependency>\r\n<groupId>org.springframework.cloud<\/groupId>\r\n<artifactId>spring-cloud-starter-sleuth<\/artifactId>\r\n<\/dependency>\r\n<\/pre>\n
\n CurrencyExchangeServiceApplication.java <\/strong>\u6587\u4ef6\u5e76\u5b9a\u4e49\u4e00\u4e2a
\n Bean <\/strong>\u3002\n <\/div>\n\r\npackage <\/span>com.lidihuo.microservices.currencyexchangeservice;\r\nimport org.springframework.boot.SpringApplication;\r\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\r\nimport org.springframework.cloud.client.discovery.EnableDiscoveryClient;\r\nimport org.springframework.context.annotation.Bean;\r\nimport brave.sampler.Sampler;\r\n@SpringBootApplication\r\n@EnableDiscoveryClient\r\npublic <\/span>class <\/span>CurrencyExchangeServiceApplication \r\n{\r\npublic <\/span>static void <\/span>main(String[] args) \r\n{\r\nSpringApplication.run<\/span>(CurrencyExchangeServiceApplication.class, args);\r\n}\r\n@Bean\r\n\/\/creating a sampler called always sampler\r\npublic <\/span>Sampler defaultSampler()\r\n{\r\nreturn <\/span>Sampler.ALWAYS_SAMPLE;\r\n}\r\n}\r\n<\/pre>\n
\n currency-conversion-service <\/strong>\u7684
\n pom.xml <\/strong>\u5e76\u6dfb\u52a0Sleuth\u4f9d\u8d56\u9879\u3002\n <\/div>\n\r\n<dependency>\r\n<groupId>org.springframework.cloud<\/groupId>\r\n<artifactId>spring-cloud-starter-sleuth<\/artifactId>\r\n<\/dependency>\r\n<\/pre>\n
\n CurrencyConversionServiceApplication.java <\/strong>\u6587\u4ef6\u5e76\u5b9a\u4e49\u4e00\u4e2a
\n Bean <\/strong>\u3002\n <\/div>\n\r\npackage <\/span>com.lidihuo.microservices.currencyconversionservice;\r\nimport org.springframework.boot.SpringApplication;\r\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\r\nimport org.springframework.cloud.client.discovery.EnableDiscoveryClient;\r\nimport org.springframework.cloud.openfeign.EnableFeignClients;\r\nimport org.springframework.context.annotation.Bean;\r\nimport brave.sampler.Sampler;\r\n@SpringBootApplication\r\n@EnableFeignClients(\"com.lidihuo.microservices.currencyconversionservice\"<\/span>)\r\n@EnableDiscoveryClient\r\npublic <\/span>class <\/span>CurrencyConversionServiceApplication \r\n{\r\npublic <\/span>static void <\/span>main(String[] args) \r\n{\r\nSpringApplication.run<\/span>(CurrencyConversionServiceApplication.class, args);\r\n}\r\n@Bean\r\n\/\/creating a sampler called always sampler\r\npublic <\/span>Sampler defaultSampler()\r\n{\r\nreturn <\/span>Sampler.ALWAYS_SAMPLE;\r\n}\r\n}\r\n<\/pre>\n
\n <\/p>\n
\n Eureka\u670d\u52a1\u5668<\/strong>\u3002\n <\/div>\n <\/p>\n
\n CurrencyExchangeController.java <\/strong>\u6587\u4ef6\uff0c\u5e76\u5728\u5176\u4e2d\u6dfb\u52a0
\n logger <\/strong>\u3002\n <\/div>\n\r\npackage <\/span>com.lidihuo.microservices.currencyexchangeservice;\r\nimport org.slf4j.Logger;\r\nimport org.slf4j.LoggerFactory;\r\nimport org.springframework.beans.factory.annotation.Autowired;\r\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\r\nimport org.springframework.core.env.Environment;\r\nimport org.springframework.web.bind.annotation.GetMapping;\r\nimport org.springframework.web.bind.annotation.PathVariable;\r\nimport org.springframework.web.bind.annotation.RestController;\r\n@SpringBootApplication\r\n@RestController \r\npublic <\/span>class <\/span>CurrencyExchangeController \r\n{\r\nprivate <\/span>Logger logger=LoggerFactory.getLogger<\/span>(this<\/span>.getClass<\/span>());\r\n@Autowired\r\nprivate <\/span>Environment environment;\r\n@Autowired\r\nprivate <\/span>ExchangeValueRepository repository;\r\n@GetMapping(\"\/currency-exchange\/from\/{from}\/to\/{to}\"<\/span>) \/\/where {from} and {to} are path variable\r\npublic <\/span>ExchangeValue retrieveExchangeValue(@PathVariable String from, @PathVariable String to) \/\/from map to USD and to map to INR\r\n{ \r\nExchangeValue exchangeValue = repository.findByFromAndTo<\/span>(from, to);\r\n\/\/setting the port\r\nexchangeValue.setPort<\/span>(Integer.parseInt<\/span>(environment.getProperty<\/span>(\"local.server.port\"<\/span>)));\r\nlogger.info<\/span>(\"{}\"<\/span>, exchangeValue);\r\nreturn <\/span>exchangeValue;\r\n}\r\n}\r\n<\/pre>\n
\n CurrencyConversionContoller <\/strong>\u3002
\n java <\/strong>\u6587\u4ef6\u5e76\u5728\u5176\u4e2d\u6dfb\u52a0
\n logger <\/strong>\u3002\n <\/div>\n
\n java <\/strong>\n <\/div>\n\r\npackage <\/span>com.lidihuo.microservices.currencyconversionservice;\r\nimport java.math.BigDecimal;\r\nimport java.util.HashMap;\r\nimport java.util.Map;\r\nimport org.slf4j.Logger;\r\nimport org.slf4j.LoggerFactory;\r\nimport org.springframework.beans.factory.annotation.Autowired;\r\nimport org.springframework.http.ResponseEntity;\r\nimport org.springframework.web.bind.annotation.GetMapping;\r\nimport org.springframework.web.bind.annotation.PathVariable;\r\nimport org.springframework.web.bind.annotation.RestController;\r\nimport org.springframework.web.client.RestTemplate;\r\n@RestController\r\npublic <\/span>class <\/span>CurrencyConversionController \r\n{\r\nprivate <\/span>Logger logger=LoggerFactory.getLogger<\/span>(this<\/span>.getClass<\/span>());\r\n@Autowired\r\nprivate <\/span>CurrencyExchangeServiceProxy proxy;\r\n@GetMapping(\"\/currency-converter\/from\/{from}\/to\/{to}\/quantity\/{quantity}\"<\/span>) \/\/where {from} and {to} represents the column \r\n\/\/returns a bean back\r\npublic <\/span>CurrencyConversionBean convertCurrency(@PathVariable String from, @PathVariable String to, @PathVariable BigDecimal quantity)\r\n{\r\n\/\/setting variables to currency exchange service\r\nMap<String, String> uriVariables=new HashMap<>();\r\nuriVariables.put<\/span>(\"from\"<\/span>, from);\r\nuriVariables.put<\/span>(\"to\"<\/span>, to);\r\n\/\/calling the currency exchange service\r\nResponseEntity<CurrencyConversionBean> responseEntity=new RestTemplate().getForEntity<\/span>(\"http:\/\/localhost:8000\/currency-exchange\/from\/{from}\/to\/{to}\"<\/span>, CurrencyConversionBean.class, uriVariables);\r\nCurrencyConversionBean response=responseEntity.getBody<\/span>();\r\n\/\/creating a new <\/span>response bean and getting the response back and taking it into Bean\r\nreturn new <\/span>CurrencyConversionBean(response.getId<\/span>(), from, to, response.getConversionMultiple<\/span>(), quantity, quantity.multiply<\/span>(response.getConversionMultiple<\/span>()), response.getPort<\/span>());\r\n}\r\n\/\/mapping for <\/span>currency-converter-feign service\r\n@GetMapping(\"\/currency-converter-feign\/from\/{from}\/to\/{to}\/quantity\/{quantity}\"<\/span>) \/\/where {from} and {to} represents the column \r\n\/\/returns a bean \r\npublic <\/span>CurrencyConversionBean convertCurrencyFeign(@PathVariable String from, @PathVariable String to, @PathVariable BigDecimal quantity)\r\n{\r\nCurrencyConversionBean response=proxy.retrieveExchangeValue<\/span>(from, to);\r\nlogger.info<\/span>(\"{}\"<\/span>, response);\r\n\/\/creating a new <\/span>response bean\r\n\/\/getting the response back and taking it into Bean\r\nreturn new <\/span>CurrencyConversionBean(response.getId<\/span>(), from, to, response.getConversionMultiple<\/span>(), quantity, quantity.multiply<\/span>(response.getConversionMultiple<\/span>()), response.getPort<\/span>());\r\n}\r\n}\r\n<\/pre>\n