{"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 <\/head>
\n <\/p>\n

\n

SpringCloud \u5206\u5e03\u5f0f\u8ddf\u8e2a<\/h1>\n<\/p><\/div>\n

\u5206\u5e03\u5f0f\u8ddf\u8e2a<\/h2>\n
\n \u5206\u5e03\u5f0f\u8ddf\u8e2a\u662f\u4e00\u79cd\u76d1\u89c6\u548c\u5206\u6790\u5e94\u7528\u7a0b\u5e8f\u7684\u6280\u672f\uff0c\u5c24\u5176\u662f\u4f7f\u7528\u5fae\u670d\u52a1\u4f53\u7cfb\u7ed3\u6784\u6784\u5efa\u7684\u5e94\u7528\u7a0b\u5e8f\u3002\u4e5f\u79f0\u4e3a\u5206\u5e03\u5f0f\u8bf7\u6c42\u8ddf\u8e2a\u3002\u5f00\u53d1\u4eba\u5458\u4f7f\u7528\u5206\u5e03\u5f0f\u8ddf\u8e2a\u6765\u8c03\u8bd5\u548c\u4f18\u5316\u4ee3\u7801\u3002\n <\/div>\n
\n \u5206\u5e03\u5f0f\u8ddf\u8e2a\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5730\u65b9\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\"\u7279\u5b9a\u8bf7\u6c42\u6b63\u5728\u53d1\u751f\u4ec0\u4e48\uff1f\u8fd9\u5f88\u91cd\u8981\uff0c\u56e0\u4e3a\u5fae\u670d\u52a1\u6d89\u53ca\u8bb8\u591a\u7ec4\u4ef6\u3002\n <\/div>\n
\n \u5982\u679c\u6211\u4eec\u8981\u89e3\u51b3\u95ee\u9898\u6216\u8c03\u8bd5\u95ee\u9898\uff0c\u5219\u9700\u8981\u4e00\u4e2a\u4e2d\u592e\u670d\u52a1\u5668\u3002\u56e0\u6b64\uff0c\u51fa\u73b0\u4e86
\n \u5206\u5e03\u5f0f\u8ddf\u8e2a<\/strong>\u4e00\u8bcd\u3002\n <\/div>\n
\n \u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u5c06
\n Spring <\/strong>
\n Cloud Sleuth <\/strong>\u4e0e
\n Zipkin <\/strong>\u3002 Spring Cloud Sleuth\u4e3a\u6211\u4eec\u63d0\u51fa\u7684\u6bcf\u4e2a\u8bf7\u6c42\u5206\u914d\u4e00\u4e2a
\n \u552f\u4e00ID <\/strong>\u3002\u6211\u4eec\u53ef\u4ee5\u5728\u6240\u6709\u7ec4\u4ef6\u4e2d\u57fa\u4e8e\u552f\u4e00\u7684ID\u8ddf\u8e2a\u6240\u6709\u8bf7\u6c42\u3002\n <\/div>\n

Spring Cloud Sleuth <\/h2>\n
\n Spring Cloud Sleuth\u662f
\n Spring Cloud\u5e93<\/strong> strong>\u901a\u8fc7\u5728\u9002\u5f53\u7684HTTP\u8bf7\u6c42\u6807\u5934\u4e0a\u6dfb\u52a0
\n trace <\/strong>\u548c
\n span ID <\/strong>\u6765\u63d0\u4f9b\u8ddf\u8e2a\u540e\u7eed\u5fae\u670d\u52a1\u8fdb\u5ea6\u7684\u529f\u80fd\u3002 Sleuth\u5e93\u57fa\u4e8e
\n MDC <\/strong>(\u6620\u5c04\u7684\u8bca\u65ad\u4e0a\u4e0b\u6587)\u6982\u5ff5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u5176\u4e2d\u8f7b\u677e\u63d0\u53d6\u503c\uff0c\u653e\u5165\u4e0a\u4e0b\u6587\u5e76\u5728\u65e5\u5fd7\u4e2d\u663e\u793a\u5b83\u4eec\u3002\n <\/div>\n

Zipkin <\/h2>\n
\n Zipkin\u662f\u57fa\u4e8eJava\u7684\u5f00\u6e90
\n \u5206\u5e03\u5f0f\u8ddf\u8e2a\u7cfb\u7edf<\/strong>\u3002\u5b83\u5177\u6709\u4e00\u4e2a\u7ba1\u7406\u63a7\u5236\u53f0\uff0c\u8be5\u63a7\u5236\u53f0\u63d0\u4f9b\u4e86\u4e00\u79cd\u673a\u5236\uff0c\u7528\u4e8e
\n \u53d1\u9001\uff0c\u63a5\u6536\uff0c\u5b58\u50a8<\/strong>\u548c
\n \u53ef\u89c6\u5316<\/strong>\u8ddf\u8e2a\u540e\u7eed\u670d\u52a1\u7684\u8be6\u7ec6\u4fe1\u606f\u3002\n <\/div>\n
\n \u501f\u52a9\u5728Zipkin\u670d\u52a1\u5668\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6240\u6709\u7ec4\u4ef6\u7684\u6240\u6709\u65e5\u5fd7\u90fd\u653e\u5728
\n MQ <\/strong>(RabbitMQ)\u4e2d\u3002\u6211\u4eec\u5c06\u65e5\u5fd7\u53d1\u9001\u5230\u5408\u5e76\u65e5\u5fd7\u7684Zipkin\u670d\u52a1\u5668\u3002\u5b8c\u6210\u6b64\u64cd\u4f5c\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u76d1\u89c6\u4e0d\u540c\u7684\u8bf7\u6c42\u3002\u6211\u4eec\u8fd8\u53ef\u4ee5\u627e\u5230\u7279\u5b9a\u8bf7\u6c42\u7684\u72b6\u6001\u5417\uff1f\n <\/div>\n

\u4f7f\u7528Spring Cloud Sleuth\u5b9e\u73b0\u5206\u5e03\u5f0f\u8ddf\u8e2a<\/h2>\n
\n \u5728\u8fd9\u4e00\u6b65\u4e2d\uff0c\u6211\u4eec\u5c06\u4e3a\u6240\u6709\u8bf7\u6c42\u6dfb\u52a0Spring Cloud Sleuth\u5fae\u670d\u52a1\u3002\u5b83\u4e3a\u6240\u6709\u8bf7\u6c42\u6dfb\u52a0\u552f\u4e00\u7684ID\u3002\u5b83\u7528\u4e8e\u751f\u6210
\n \u8ddf\u8e2aID <\/strong>\uff0c
\n \u8de8\u5ea6ID <\/strong>\u5e76\u5c06\u5176\u9644\u52a0\u5230\u65e5\u5fd7\uff0c\u4ee5\u4fbf\u5de5\u5177(Zipkin)\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9bID\u3002\n <\/div>\n

SpringCloud \u5206\u5e03\u5f0f\u8ddf\u8e2a_https:\/\/bianchenghao6.com_\u3010SpringCloud \u6559\u7a0b\u3011_\u7b2c1\u5f20 <\/p>\n

\n Spring Cloud Sleuth\u4ee4\u724c\u5177\u6709\u4ee5\u4e0b\u7ec4\u4ef6:\n <\/div>\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>
\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

\n \u4e0b\u56fe\u663e\u793a\u4e86Spring Cloud Sleuth\u4ee4\u724c\u3002\n <\/div>\n

SpringCloud \u5206\u5e03\u5f0f\u8ddf\u8e2a_https:\/\/bianchenghao6.com_\u3010SpringCloud \u6559\u7a0b\u3011_\u7b2c2\u5f20 <\/p>\n

\n \u8ba9\u6211\u4eec\u5728\u9879\u76ee\u4e2d\u5b9e\u65bd
\n Spring Cloud Sleuth <\/strong>\u3002\n <\/div>\n
\n \u6b65\u9aa41: <\/strong>\u9009\u62e9\u9879\u76ee
\n netflix-zuul -api-gateway-server <\/strong>\u3002\n <\/div>\n
\n \u6b65\u9aa42: <\/strong>\u6253\u5f00
\n pom.xml <\/strong>\u5e76\u6dfb\u52a0
\n Sleuth <\/strong>\u4f9d\u8d56\u6027\u3002\n <\/div>\n
\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

<\/code>\n <\/div>\n

\n \u73b0\u5728\uff0c\u6211\u4eec\u9700\u8981\u8ddf\u8e2a\u6240\u6709\u8bf7\u6c42\u3002\u5982\u679c\u8981\u8ddf\u8e2a\u6240\u6709\u8bf7\u6c42\uff0c\u5219\u9700\u8981\u521b\u5efa
\n ALWAYS_SAMPLE <\/strong>\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528
\n Bean <\/strong>\u521b\u5efa\u4e00\u4e2aSampler\u3002\n <\/div>\n
\n Sampler <\/strong>\n <\/div>\n
\n \u5206\u5e03\u5f0f\u8ddf\u8e2a\u53ef\u80fd\u5305\u542b\u5927\u91cf\u6570\u636e\uff0c\u56e0\u6b64\uff0c\u91c7\u6837\u5728\u5206\u5e03\u5f0f\u8ddf\u8e2a\u4e2d\u5f88\u91cd\u8981\u3002 Spring Cloud Sleuth\u63d0\u4f9b\u4e86
\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 \u65b0\u521b\u5efa\u7684\u8de8\u5ea6\u59cb\u7ec8\u6807\u8bb0\u4e3a
\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 \u5f53\u6211\u4eec\u5c06\u8303\u56f4\u6570\u636e\u5bfc\u51fa\u5230
\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 \u8bb0\u4f4f: <\/strong>\u5982\u679c\u60a8\u4f7f\u7528\u7684\u662f
\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
\n
\r\n@Bean\r\npublic <\/span>Sampler defaultSampler()\r\n{\r\nreturn <\/span>Sampler.ALWAYS_SAMPLE;\r\n}\r\n<\/pre>\n

<\/code>\n <\/div>\n

\n \u6b65\u9aa43: <\/strong>\u6253\u5f00
\n NetflixZuulApiGatewayServerApplication.java <\/strong>\u6587\u4ef6\u5e76\u5b9a\u4e49\u4e00\u4e2a
\n Bean <\/strong>\u3002\n <\/div>\n
\n NetflixZuulApiGatewayServerApplication\u3002 Java <\/strong>\n <\/div>\n
\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

<\/code>\n <\/div>\n

\n \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5df2\u5c06Spring Cloud Sleuth\u6dfb\u52a0\u5230Zuul API Gateway\u670d\u52a1\u5668\u3002\n <\/div>\n
\n \u73b0\u5728\uff0c\u6211\u4eec\u5fc5\u987b\u5728
\n currency-exchange-service <\/strong>\u548c<\n <\/div>\n
\n \u6b65\u9aa44: <\/strong>\u6253\u5f00
\n currency-exchange-service\u7684 pom.xml <\/strong> <\/strong>\u5e76\u6dfb\u52a0
\n Sleuth <\/strong>\u4f9d\u8d56\u9879\u3002\n <\/div>\n
\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

<\/code>\n <\/div>\n

\n \u6b65\u9aa45: <\/strong>\u6253\u5f00
\n CurrencyExchangeServiceApplication.java <\/strong>\u6587\u4ef6\u5e76\u5b9a\u4e49\u4e00\u4e2a
\n Bean <\/strong>\u3002\n <\/div>\n
\n CurrencyExchangeServiceApplication\u3002 Java <\/strong>\n <\/div>\n
\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

<\/code>\n <\/div>\n

\n \u6b65\u9aa46: <\/strong>\u7c7b\u4f3c\u5730\uff0c\u6253\u5f00
\n currency-conversion-service <\/strong>\u7684
\n pom.xml <\/strong>\u5e76\u6dfb\u52a0Sleuth\u4f9d\u8d56\u9879\u3002\n <\/div>\n
\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

<\/code>\n <\/div>\n

\n \u6b65\u9aa47: <\/strong>\u6253\u5f00
\n CurrencyConversionServiceApplication.java <\/strong>\u6587\u4ef6\u5e76\u5b9a\u4e49\u4e00\u4e2a
\n Bean <\/strong>\u3002\n <\/div>\n
\n CurrencyConversionServiceApplication\u3002 Java <\/strong>\n <\/div>\n
\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

<\/code>\n <\/div>\n

\n \u73b0\u5728\u6211\u4eec\u6709\u4e09\u4e2a\u8fde\u63a5\u5230Spring Cloud Sleuth\u7684\u5e94\u7528\u7a0b\u5e8f\u3002\n <\/div>\n
\n \u6b65\u9aa48: <\/strong>\u6309\u4ee5\u4e0b\u987a\u5e8f\u542f\u52a8\u5e94\u7528\u7a0b\u5e8f:\n <\/div>\n
\n netflix-eureka-naming-server <\/strong>\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>
\n SpringCloud \u5206\u5e03\u5f0f\u8ddf\u8e2a_https:\/\/bianchenghao6.com_\u3010SpringCloud \u6559\u7a0b\u3011_\u7b2c3\u5f20 <\/p>\n

\n currency-exchange-service <\/strong>(\u5728\u7aef\u53e38000\u4e0a)\n <\/div>\n
\n currency-conversion-service<\/strong>\n <\/div>\n
\n netflix-zuul-api-gateway-server <\/strong>\n <\/div>\n
\n \u8bb0\u4f4f: <\/strong> \u542f\u52a8\u6bcf\u4e2a\u670d\u52a1\u540e\uff0c\u5237\u65b0
\n Eureka\u670d\u52a1\u5668<\/strong>\u3002\n <\/div>\n
\n \u5b83\u663e\u793a\u5f53\u524d\u5df2\u5728Eureka\u670d\u52a1\u5668\u4e0a\u6ce8\u518c\u7684\u6240\u6709\u5b9e\u4f8b\u3002\n <\/div>\n

SpringCloud \u5206\u5e03\u5f0f\u8ddf\u8e2a_https:\/\/bianchenghao6.com_\u3010SpringCloud \u6559\u7a0b\u3011_\u7b2c4\u5f20 <\/p>\n

\n \u6b65\u9aa49: <\/strong>\u6253\u5f00
\n CurrencyExchangeController.java <\/strong>\u6587\u4ef6\uff0c\u5e76\u5728\u5176\u4e2d\u6dfb\u52a0
\n logger <\/strong>\u3002\n <\/div>\n
\n CurrencyExchangeController.java <\/strong>\n <\/div>\n
\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

<\/code>\n <\/div>\n

\n \u7c7b\u4f3c\u5730\uff0c\u6211\u4eec\u5c06\u8bb0\u5f55\u5668\u6dfb\u52a0\u5230CurrencyConversionContoller\u3002\n <\/div>\n
\n \u6b65\u9aa410: <\/strong>\u6253\u5f00
\n CurrencyConversionContoller <\/strong>\u3002
\n java <\/strong>\u6587\u4ef6\u5e76\u5728\u5176\u4e2d\u6dfb\u52a0
\n logger <\/strong>\u3002\n <\/div>\n
\n CurrencyConversionContoller <\/strong>\u3002
\n java <\/strong>\n <\/div>\n
\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

<\/code>\n <\/div>\n

\n \u6b65\u9aa412: <\/strong>\u6267\u884c\u8bf7\u6c42 http:\/\/localhost:8100\/currency-converter-feign\/from\/EUR\/to\/INR\/quantity\/100 \u3002\u5b83\u5c06\u8fd4\u56de\u4ee5\u4e0b\u54cd\u5e94\uff0c\u5982\u4e0b\u6240\u793a\u3002\n <\/div>\n

SpringCloud \u5206\u5e03\u5f0f\u8ddf\u8e2a_https:\/\/bianchenghao6.com_\u3010SpringCloud \u6559\u7a0b\u3011_\u7b2c5\u5f20 <\/p>\n

\n \u8ba9\u6211\u4eec\u5728\u63a7\u5236\u53f0\u4e2d\u67e5\u770b
\n currency-conversion-service <\/strong>\u7684\u65e5\u5fd7\u3002currency-conversion-service\u663e\u793a\u4ee5\u4e0b\u65e5\u5fd7:\n <\/div>\n

SpringCloud \u5206\u5e03\u5f0f\u8ddf\u8e2a_https:\/\/bianchenghao6.com_\u3010SpringCloud \u6559\u7a0b\u3011_\u7b2c6\u5f20 <\/p>\n

\n \u6211\u4eec\u8fd8\u53ef\u4ee5\u67e5\u770b
\n currency-exchange-service <\/strong>\u7684\u65e5\u5fd7\u3002\u8d27\u5e01\u5151\u6362\u670d\u52a1\u663e\u793a\u4ee5\u4e0b\u65e5\u5fd7:\n <\/div>\n

SpringCloud \u5206\u5e03\u5f0f\u8ddf\u8e2a_https:\/\/bianchenghao6.com_\u3010SpringCloud \u6559\u7a0b\u3011_\u7b2c7\u5f20 <\/p>\n

\n \u7c7b\u4f3c\u5730\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230
\n netflix-zuul-api-gateway-server\u7684\u65e5\u5fd7\u3002<\/strong>\n <\/div>\n

SpringCloud \u5206\u5e03\u5f0f\u8ddf\u8e2a_https:\/\/bianchenghao6.com_\u3010SpringCloud \u6559\u7a0b\u3011_\u7b2c8\u5f20 <\/p>\n

\n \u8ba9\u6211\u4eec\u4ed4\u7ec6\u770b\u4e00\u4e0b\u4ee5\u4e0a\u4e09\u79cd\u4e0d\u540c\u670d\u52a1\u7684\u65e5\u5fd7\u3002\u6211\u4eec\u53d1\u73b0\u8fd9\u4e09\u4e2a\u670d\u52a1\u90fd\u5177\u6709\u76f8\u540c\u7684
\n \u8ddf\u8e2aID(533f8d3966d8f4e7)<\/strong>\u3002\n <\/div>\n
\n Spring Cloud Sleuth\u5c06\u8ddf\u8e2aID\u5206\u914d\u7ed9\u8bf7\u6c42\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6b64ID\u8de8\u591a\u4e2a\u7ec4\u4ef6\u8ddf\u8e2a\u8bf7\u6c42\u3002\u4f46\u662f\u5b58\u5728\u4e00\u4e2a\u95ee\u9898\uff0c\u8be5\u65e5\u5fd7\u5206\u5e03\u5728\u591a\u4e2a\u4f4d\u7f6e\u3002\u6211\u4eec\u4f7f\u7528
\n Zipkin <\/strong>\u6765\u89e3\u51b3\u6b64\u95ee\u9898\u3002\u501f\u52a9Zipkin\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u65e5\u5fd7\u96c6\u4e2d\u5728\u4e00\u4e2a\u5730\u65b9\u3002\n <\/div>\n

\u4f7f\u7528Zipkin\u8fdb\u884c\u5206\u5e03\u5f0f\u8ddf\u8e2a<\/h2>\n
\n \u4f46\u662f\uff0c\u6211\u4eec\u5728\u8ddf\u8e2a\u65b9\u9762\u9762\u4e34\u6311\u6218\u3002\u5982\u679c\u8981\u8ddf\u8e2a\u8bf7\u6c42\uff0c\u5219\u5fc5\u987b\u68c0\u67e5\u5355\u4e2a\u5e94\u7528\u7a0b\u5e8f\u7684\u65e5\u5fd7\u3002\u89e3\u51b3\u6b64\u95ee\u9898\u7684\u65b9\u6cd5\u79f0\u4e3a
\n \u96c6\u4e2d\u5f0f\u65e5\u5fd7\u3002<\/strong>\n <\/div>\n
\n \u6211\u4eec\u9700\u8981\u96c6\u4e2d\u6240\u6709\u5fae\u670d\u52a1\u4e2d\u7684\u6240\u6709\u65e5\u5fd7\u3002\u6211\u4eec\u53ef\u4ee5\u641c\u7d22Spring Cloud Sleuth\u5206\u914d\u7684ID\u3002\u5728\u96c6\u4e2d\u7684\u5730\u65b9\uff0c\u6211\u4eec\u5c06\u80fd\u591f\u641c\u7d22\u5e76\u627e\u51fa\u7279\u5b9a\u8bf7\u6c42\u7684\u72b6\u51b5\u3002\n <\/div>\n
\n \u6709\u4ee5\u4e0b\u96c6\u4e2d\u65e5\u5fd7\u8bb0\u5f55\u7684\u89e3\u51b3\u65b9\u6848:\n <\/div>\n

ELK Stack (Elastic Search)<\/strong> <\/span>
\n Kibana<\/strong> <\/span>
\n Zipkin<\/strong> <\/span>
\n SpringCloud \u5206\u5e03\u5f0f\u8ddf\u8e2a_https:\/\/bianchenghao6.com_\u3010SpringCloud \u6559\u7a0b\u3011_\u7b2c9\u5f20 <\/p>\n

\n \u5728\u6b64\u5206\u5e03\u5f0f\u8ddf\u8e2a\u4e2d\uff0c\u6211\u4eec\u5c06\u4f7f\u7528
\n Zipkin\u5206\u53d1\u8ddf\u8e2a\u670d\u52a1\u5668<\/strong>\u3002\u5b83\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u6240\u6709\u5fae\u670d\u52a1\u7684\u7edf\u4e00\u89c6\u56fe\u3002\u6211\u4eec\u4ece\u5355\u4e2a\u5fae\u670d\u52a1\u83b7\u53d6\u6240\u6709\u65e5\u5fd7\u6d88\u606f\u3002 Zipkin\u670d\u52a1\u5668\u6536\u96c6\u65e5\u5fd7\u6d88\u606f\u3002\u6240\u6709\u5fae\u670d\u52a1\u90fd\u5c06\u65e5\u5fd7\u6d88\u606f\u653e\u5728\u540d\u4e3a
\n RabbitMQ <\/strong>\u7684\u961f\u5217\u4e2d\uff0c\u7136\u540eZipkin\u4eceRabbitMQ\u4e2d\u9009\u62e9\u8fd9\u4e9b\u65e5\u5fd7\u6d88\u606f\u3002 Zipkin\u8ddf\u8e2a\u670d\u52a1\u5668\u4e0e\u6570\u636e\u5e93\u8fde\u63a5\u3002\n <\/div>\n
\n \u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5185\u5b58\u6570\u636e\u5e93\u3002\u6211\u4eec\u5c06\u4ece\u6570\u636e\u5e93\u4e2d\u63d0\u53d6\u65e5\u5fd7\u6d88\u606f\u3002\u4e0b\u4e00\u6b65\uff0c\u6211\u4eec\u5c06\u5b89\u88c5RabbitMQ\u3002\n <\/div>\n
\n

<\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"SpringCloud \u5206\u5e03\u5f0f\u8ddf\u8e2azh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[106],"tags":[],"class_list":["post-647","post","type-post","status-publish","format-standard","hentry","category-bc_springcloud_jc"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/647"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=647"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/647\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=647"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=647"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=647"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}