{"id":416798,"date":"2024-06-30T01:10:21","date_gmt":"2024-06-30T01:10:21","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=416798"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=416798","title":{"rendered":"<span>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f gRPC \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e Java \u0438 Spring Boot<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0425\u043e\u0440\u043e\u0448\u043e \u0431\u044b \u043f\u043e\u043d\u0438\u043c\u0430\u0442\u044c \u0440\u0430\u0437\u043b\u0438\u0447\u0438\u044f \u043c\u0435\u0436\u0434\u0443 HTTP\/1.1 \u0438 HTTP\/2, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 gRPC \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 HTTP\/2 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e.<\/p>\n<h3>HTTP\/1.1 vs HTTP\/2<\/h3>\n<p>\u0425\u0430\u0440\u0430\u043a\u0442\u0435\u0440\u0438\u0441\u0442\u0438\u043a\u0438 HTTP\/1.1:<\/p>\n<ul>\n<li>\n<p>\u0422\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0439 \u0444\u043e\u0440\u043c\u0430\u0442<\/p>\n<\/li>\n<li>\n<p>\u0417\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0438 \u0432 \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u043e\u043c \u0444\u043e\u0440\u043c\u0430\u0442\u0435<\/p>\n<\/li>\n<li>\n<p>TCP-\u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u00ab\u0442\u0440\u0435\u0445\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0433\u043e \u0440\u0443\u043a\u043e\u043f\u043e\u0436\u0430\u0442\u0438\u044f\u00bb (three-way handshake) \u2014 \u043e\u0434\u0438\u043d \u0437\u0430\u043f\u0440\u043e\u0441 \u0438 \u043e\u0442\u0432\u0435\u0442 \u0441 \u043e\u0434\u043d\u0438\u043c \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c TCP-\u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435\u043c.<\/p>\n<\/li>\n<\/ul>\n<p>\u0425\u0430\u0440\u0430\u043a\u0442\u0435\u0440\u0438\u0441\u0442\u0438\u043a\u0438 HTTP\/2:<\/p>\n<ul>\n<li>\n<p>\u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u0444\u043e\u0440\u043c\u0430\u0442<\/p>\n<\/li>\n<li>\n<p>\u0421\u0436\u0430\u0442\u0438\u0435 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u043e\u0432<\/p>\n<\/li>\n<li>\n<p>\u0423\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043f\u043e\u0442\u043e\u043a\u043e\u043c<\/p>\n<\/li>\n<li>\n<p>\u041c\u0443\u043b\u044c\u0442\u0438\u043f\u043b\u0435\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 (\u043e\u0434\u043d\u043e \u0438 \u0442\u043e \u0436\u0435 TCP-\u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u043e \u0434\u043b\u044f \u043c\u0443\u043b\u044c\u0442\u0438\u043f\u043b\u0435\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f. \u041f\u043e\u0442\u043e\u043a\u043e\u0432\u0430\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 \u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u2014 \u043f\u043e\u0442\u043e\u043a\u043e\u0432\u0430\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 \u043e\u0442 \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u2014 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u0430 \u0434\u0432\u0443\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u0430\u044f \u043f\u043e\u0442\u043e\u043a\u043e\u0432\u0430\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0430)<\/p>\n<\/li>\n<\/ul>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/b59\/c23\/fa4\/b59c23fa49a6144899ef5c7ede4bd64d.png\" width=\"720\" height=\"714\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/b59\/c23\/fa4\/b59c23fa49a6144899ef5c7ede4bd64d.png\"\/><\/figure>\n<p>\u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c, \u043a\u0430\u043a \u0432\u0435\u0434\u0435\u0442 \u0441\u0435\u0431\u044f \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u043a\u0430\u0436\u0434\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u0434\u043b\u044f HTTP1 \u0438 HTTP2 (\u043c\u0443\u043b\u044c\u0442\u0438\u043f\u043b\u0435\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435) \u043c\u043e\u0436\u043d\u043e <a href=\"https:\/\/http2.akamai.com\/demo\"><u>\u043f\u043e \u044d\u0442\u043e\u0439 \u0441\u0441\u044b\u043b\u043a\u0435<\/u><\/a>.<\/p>\n<p>\u0412 \u044d\u0442\u043e\u043c \u0440\u0430\u0437\u0434\u0435\u043b\u0435 \u043f\u043e\u0441\u0442\u0430\u0440\u0430\u0435\u043c\u0441\u044f \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0432 \u043f\u0440\u0438\u0447\u0438\u043d\u0430\u0445, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u043f\u043e\u0432\u043b\u0438\u044f\u0442\u044c \u043d\u0430 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043e \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0435 \u0441 REST \u0438 JSON \u043a gRPC \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c Protocol Buffers.<\/p>\n<h3>JSON vs gRPC (\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 Protocol Buffers)<\/h3>\n<p>JSON<\/p>\n<ul>\n<li>\n<p>\u041d\u0435\u0442 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0441\u0445\u0435\u043c\u044b \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430.<\/p>\n<\/li>\n<li>\n<p>\u0422\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0439 \u0444\u043e\u0440\u043c\u0430\u0442 (\u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f\/\u0434\u0435\u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e \u0438 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u0437\u0430\u0442\u0440\u0430\u0442 \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432).<\/p>\n<\/li>\n<li>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0432 REST.<\/p>\n<\/li>\n<\/ul>\n<p>gPRC<\/p>\n<ul>\n<li>\n<p>\u0421\u0442\u0440\u043e\u0433\u043e\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0441\u0445\u0435\u043c\u044b \u0438 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u044c \u0442\u0438\u043f\u043e\u0432 (IDL: \u044f\u0437\u044b\u043a \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 \u0434\u043b\u044f API).<\/p>\n<\/li>\n<li>\n<p>\u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439, \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442 \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e\/\u0434\u0435\u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0431\u044b\u0441\u0442\u0440\u0435\u0435.<\/p>\n<\/li>\n<li>\n<p>\u0410\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430, \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u0430\u044f \u0434\u043b\u044f \u043c\u0435\u0436\u0441\u0435\u0440\u0432\u0438\u0441\u043d\u043e\u0433\u043e \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f.<\/p>\n<\/li>\n<li>\n<p>HTTP\/2 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043c\u0443\u043b\u044c\u0442\u0438\u043f\u043b\u0435\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435.<\/p>\n<\/li>\n<\/ul>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/6fd\/ef9\/d0f\/6fdef9d0fc621a393675f29a0d1effda.png\" alt=\"gPRC \u043a\u043b\u0438\u0435\u043d\u0442-\u0441\u0435\u0440\u0432\u0438\u0441\u043d\u0430\u044f \u043a\u043e\u043c\u043c\u0443\u043d\u0438\u043a\u0430\u0446\u0438\u044f\" title=\"gPRC \u043a\u043b\u0438\u0435\u043d\u0442-\u0441\u0435\u0440\u0432\u0438\u0441\u043d\u0430\u044f \u043a\u043e\u043c\u043c\u0443\u043d\u0438\u043a\u0430\u0446\u0438\u044f\" width=\"720\" height=\"420\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/6fd\/ef9\/d0f\/6fdef9d0fc621a393675f29a0d1effda.png\"\/><\/p>\n<div><figcaption>gPRC \u043a\u043b\u0438\u0435\u043d\u0442-\u0441\u0435\u0440\u0432\u0438\u0441\u043d\u0430\u044f \u043a\u043e\u043c\u043c\u0443\u043d\u0438\u043a\u0430\u0446\u0438\u044f<\/figcaption><\/div>\n<\/figure>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/cbc\/ca1\/5af\/cbcca15af298e128b807a8d4c38dd16b.png\" alt=\"\u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 (\u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043d\u043e\u0433\u043e, \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u043e\u0433\u043e \u0438 \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u043e\u0432)\" title=\"\u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 (\u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043d\u043e\u0433\u043e, \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u043e\u0433\u043e \u0438 \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u043e\u0432)\" width=\"720\" height=\"185\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/cbc\/ca1\/5af\/cbcca15af298e128b807a8d4c38dd16b.png\"\/><\/p>\n<div><figcaption>\u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 (\u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043d\u043e\u0433\u043e, \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u043e\u0433\u043e \u0438 \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u043e\u0432)<\/figcaption><\/div>\n<\/figure>\n<p>\u0421\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043e \u0432 <a href=\"https:\/\/www.vinsguru.com\/grpc-vs-rest-performance-comparison\/\"><u>\u044d\u0442\u043e\u043c \u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u0435<\/u><\/a>. \u041d\u0430 \u0432\u0438\u0434\u0435\u043e \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u044e \u0432 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0438:<\/p>\n<div class=\"tm-iframe_temp\" data-src=\"https:\/\/embedd.srv.habr.com\/iframe\/644271644d2bbedf9b4488ff\" data-style=\"\" id=\"644271644d2bbedf9b4488ff\" width=\"\"><\/div>\n<h3>\u0422\u0438\u043f\u044b \u0434\u0430\u043d\u043d\u044b\u0445<\/h3>\n<p>int32 (\u0434\u043b\u044f int) \u2014 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e: 0<br \/>int64 (\u0434\u043b\u044f long) \u2014 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e: 0<br \/>float \u2014 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e: 0<br \/>double \u2014 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e: 0<br \/>bool \u2014 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e: false<br \/>string \u2014 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e: \u043f\u0443\u0441\u0442\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430<br \/>\u0431\u0430\u0439\u0442 (\u0434\u043b\u044f <em>byte<\/em>[])<br \/>repeated (\u0434\u043b\u044f List\/Collection)<br \/>map (\u0434\u043b\u044f Map) \u2014 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e: <em>empty map<br \/><\/em>enum \u2014 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e: \u043f\u0435\u0440\u0432\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 \u0441\u043f\u0438\u0441\u043a\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439.<\/p>\n<p>\u0415\u0441\u0442\u044c \u0442\u0430\u043a\u0436\u0435 \u043a\u043b\u0430\u0441\u0441\u044b-\u043e\u0431\u0451\u0440\u0442\u043a\u0438 (\u043a\u0430\u043a Integer \u0432 Java), \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c, \u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0432 \u0438\u0445 \u0432 proto-\u0444\u0430\u0439\u043b.<\/p>\n<pre><code>import \u201cgoogle\/protobuf\/wrappers.proto\u201d;<\/code><\/pre>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435:<\/p>\n<pre><code>google.protobuf.UInt64Value id_number = 1;<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u043d\u0443\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u043e\u043b\u0435 \u043c\u0435\u0442\u043a\u0438 \u0432\u0440\u0435\u043c\u0435\u043d\u0438, \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0438\u043c\u043f\u043e\u0440\u0442, \u043a\u0430\u043a \u043e\u043f\u0438\u0441\u0430\u043d\u043e <a href=\"https:\/\/developers.google.com\/protocol-buffers\/docs\/reference\/csharp\/class\/google\/protobuf\/well-known-types\/timestamp\"><u>\u0437\u0434\u0435\u0441\u044c<\/u><\/a>:<\/p>\n<pre><code>import \"google\/protobuf\/timestamp.proto\";<\/code><\/pre>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435:<\/p>\n<pre><code>google.protobuf.Timestamp timestamp = 2;<\/code><\/pre>\n<h3>\u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u043f\u0440\u043e\u0435\u043a\u0442\u0430<\/h3>\n<p>\u0420\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u0435\u0442\u0441\u044f \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u043e\u0434\u0443\u043b\u044c \u0434\u043b\u044f proto-\u043c\u043e\u0434\u0435\u043b\u0438 \u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0439 \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432 \u0434\u043b\u044f \u043e\u0431\u0449\u0435\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f (\u043a\u0430\u043a \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044c).<\/p>\n<p>\u0412 \u0441\u043e\u0433\u043b\u0430\u0448\u0435\u043d\u0438\u0438 \u043e\u0431 \u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u0438 proto-\u0444\u0430\u0439\u043b\u043e\u0432 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u0435\u0442\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c &#171;<em>lower_snake_case.proto<\/em>&#171;. \u0420\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u043e \u043f\u043e \u0441\u0442\u0438\u043b\u044e \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c <a href=\"https:\/\/docs.buf.build\/best-practices\/style-guide\/\"><u>\u0437\u0434\u0435\u0441\u044c<\/u><\/a>.<\/p>\n<p>\u0418\u043c\u0435\u043d\u0430 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0442\u0430\u043a\u0436\u0435 \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u044b \u0432 \u043d\u0438\u0436\u043d\u0435\u043c \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0435 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0437\u043d\u0430\u043a\u0430 \u043f\u043e\u0434\u0447\u0435\u0440\u043a\u0438\u0432\u0430\u043d\u0438\u044f \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u0435\u043b\u044f \u043c\u0435\u0436\u0434\u0443 \u0441\u043b\u043e\u0432\u0430\u043c\u0438.<\/p>\n<p>\u042f \u0441\u043e\u0437\u0434\u0430\u043b proto-\u0444\u0430\u0439\u043b \u0441 \u0438\u043c\u0435\u043d\u0435\u043c &#171;city_score.proto&#187;:<\/p>\n<pre><code class=\"java\">syntax = \"proto3\";  import \"google\/protobuf\/timestamp.proto\";  package cityscore;  option java_package = \"com.nils.gprc.cityscore\"; option java_multiple_files = true;  message CityScoreRequest {   int32 city_code = 1; }  message CityScoreResponse {   int32 city_score = 1; }  enum CityScoreErrorCode {   INVALID_CITY_CODE_VALUE = 0;   CITY_CODE_CANNOT_BE_NULL = 1; }  message CityScoreExceptionResponse {   google.protobuf.Timestamp timestamp = 1;   CityScoreErrorCode error_code = 2; }  service CityScoreService {   \/\/ unary   rpc calculateCityScore(CityScoreRequest) returns (CityScoreResponse) {}; }<\/code><\/pre>\n<p>\u041f\u0440\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 \u0432 Maven \u044f \u043f\u043e\u043b\u0443\u0447\u0438\u043b \u043e\u0448\u0438\u0431\u043a\u0443:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/647\/142\/020\/647142020abf9c7d94b06d073479781d.png\" alt=\"\" title=\"\" width=\"720\" height=\"128\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/647\/142\/020\/647142020abf9c7d94b06d073479781d.png\"\/><\/figure>\n<p>\u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u043e\u0431\u0430\u0432\u0438\u043b \u044d\u0442\u0438 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430:<\/p>\n<pre><code>&lt;project.build.sourceEncoding>UTF-8&lt;\/project.build.sourceEncoding> &lt;maven.compiler.source>16&lt;\/maven.compiler.source> &lt;maven.compiler.target>16&lt;\/maven.compiler.target><\/code><\/pre>\n<p>\u0417\u0430\u0442\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u043b \u043e\u0448\u0438\u0431\u043a\u0443 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/85f\/48f\/385\/85f48f3859eaa539a1b4a34f2fd56dd4.png\" alt=\"\" title=\"\" width=\"720\" height=\"186\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/85f\/48f\/385\/85f48f3859eaa539a1b4a34f2fd56dd4.png\"\/><\/figure>\n<p>\u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u044f \u043e\u0431\u043d\u043e\u0432\u0438\u043b \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 Maven \u0434\u043e \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439 \u0432\u0435\u0440\u0441\u0438\u0438, \u043a\u0430\u043a \u0443\u043a\u0430\u0437\u0430\u043d\u043e <a href=\"https:\/\/github.com\/grpc\/grpc-java\"><u>\u0437\u0434\u0435\u0441\u044c<\/u><\/a>:<\/p>\n<pre><code>&lt;dependency>   &lt;groupId>io.grpc&lt;\/groupId>   &lt;artifactId>grpc-netty-shaded&lt;\/artifactId>   &lt;version>1.41.0&lt;\/version> &lt;\/dependency> &lt;dependency>   &lt;groupId>io.grpc&lt;\/groupId>   &lt;artifactId>grpc-protobuf&lt;\/artifactId>   &lt;version>1.41.0&lt;\/version> &lt;\/dependency> &lt;dependency>   &lt;groupId>io.grpc&lt;\/groupId>   &lt;artifactId>grpc-stub&lt;\/artifactId>   &lt;version>1.41.0&lt;\/version> &lt;\/dependency> &lt;dependency> &lt;!-- necessary for Java 9+ -->   &lt;groupId>org.apache.tomcat&lt;\/groupId>   &lt;artifactId>annotations-api&lt;\/artifactId>   &lt;version>6.0.53&lt;\/version>   &lt;scope>provided&lt;\/scope> &lt;\/dependency><\/code><\/pre>\n<p>\u0412\u0441\u0435 \u0448\u043b\u043e \u0445\u043e\u0440\u043e\u0448\u043e, \u043f\u043e\u043a\u0430 \u044f \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043b \u0432 \u043f\u0440\u043e\u0435\u043a\u0442\u0435 \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u043e\u0448\u0438\u0431\u043a\u0443 \u043d\u0435\u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u044f \u0432\u0435\u0440\u0441\u0438\u0439 proto-\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439 (\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u044f\u0432\u0438\u043b\u0438\u0441\u044c \u043f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u044f \u0434\u043e\u0431\u0430\u0432\u0438\u043b \u043e\u0431\u0449\u0438\u0439 proto-\u043c\u043e\u0434\u0443\u043b\u044c \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438) \u0438 &#171;grpc-server-spring-boot-starter&#187;, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u043d\u0435 \u043f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u043f\u043e\u043d\u0438\u0437\u0438\u0442\u044c \u0432\u0435\u0440\u0441\u0438\u0438 proto-\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439 \u0434\u043e 1.37. \u0418\u043c\u0435\u043d\u043d\u043e \u044d\u0442\u0443 \u0432\u0435\u0440\u0441\u0438\u044e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u044f\u044f \u0432\u0435\u0440\u0441\u0438\u044f grpc-server-spring-boot-starter \u2014 2.12.0.RELEASE:<\/p>\n<pre><code>&lt;dependencies>     &lt;dependency>         &lt;groupId>io.grpc&lt;\/groupId>         &lt;artifactId>grpc-netty-shaded&lt;\/artifactId>         &lt;version>1.37.0&lt;\/version>     &lt;\/dependency>     &lt;dependency>         &lt;groupId>io.grpc&lt;\/groupId>         &lt;artifactId>grpc-protobuf&lt;\/artifactId>         &lt;version>1.37.0&lt;\/version>     &lt;\/dependency>     &lt;dependency>         &lt;groupId>io.grpc&lt;\/groupId>         &lt;artifactId>grpc-stub&lt;\/artifactId>         &lt;version>1.37.0&lt;\/version>     &lt;\/dependency>     &lt;dependency> &lt;!-- necessary for Java 9+ -->         &lt;groupId>org.apache.tomcat&lt;\/groupId>         &lt;artifactId>annotations-api&lt;\/artifactId>         &lt;version>6.0.53&lt;\/version>         &lt;scope>provided&lt;\/scope>     &lt;\/dependency> &lt;\/dependencies><\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u0439 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 \u0432 Maven \u044f \u043f\u043e\u043b\u0443\u0447\u0438\u043b \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0438\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0438 \u0434\u043b\u044f proto-\u0444\u0430\u0439\u043b\u0430:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/496\/8e5\/4a5\/4968e54a5cc71ddf133990ab1ef55d7e.png\" alt=\"\" title=\"\" width=\"720\" height=\"1081\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/496\/8e5\/4a5\/4968e54a5cc71ddf133990ab1ef55d7e.png\"\/><\/figure>\n<h3>\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043c\u043e\u0435\u0433\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u0430<\/h3>\n<ul>\n<li>\n<p>City Score Service<\/p>\n<\/li>\n<li>\n<p>Score Segment Service<\/p>\n<\/li>\n<li>\n<p>Score Calculator Service (\u0441\u0435\u0440\u0432\u0438\u0441-\u0430\u0433\u0440\u0435\u0433\u0430\u0442\u043e\u0440, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u043a\u0430\u043a City Score, \u0442\u0430\u043a \u0438 Score Segment).<\/p>\n<\/li>\n<\/ul>\n<p>\u041a\u0430\u043a \u0432\u044b \u0443\u0432\u0438\u0434\u0438\u0442\u0435 \u0438\u0437 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u043a\u043e\u0434\u0430, \u044f \u0431\u0443\u0434\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0434\u0438\u043d\u043e\u0447\u043d\u044b\u0435 \u0432\u044b\u0437\u043e\u0432\u044b \u0432 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438.<\/p>\n<h3>\u0420\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0441\u0435\u0440\u0432\u0438\u0441\u0430<\/h3>\n<p>\u0412\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u0430 &#171;\u0441\u0435\u0440\u0432\u0435\u0440\u043d\u0430\u044f&#187; \u0432\u0435\u0440\u0441\u0438\u044f grpc spring-boot-starter \u0432 \u0444\u0430\u0439\u043b\u0435 pom.xml:<\/p>\n<pre><code>&lt;dependency>    &lt;groupId>net.devh&lt;\/groupId>    &lt;artifactId>grpc-server-spring-boot-starter&lt;\/artifactId>    &lt;version>2.12.0.RELEASE&lt;\/version> &lt;\/dependency>  &lt;dependency>    &lt;groupId>com.nils.gprc&lt;\/groupId>    &lt;artifactId>proto-common&lt;\/artifactId>    &lt;version>0.0.1-SNAPSHOT&lt;\/version> &lt;\/dependency><\/code><\/pre>\n<p>\u042d\u0442\u043e \u043c\u043e\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f City Score Service:<\/p>\n<pre><code class=\"java\">package com.nils.microservices.cityscore.service;  import com.nils.gprc.cityscore.CityScoreRequest; import com.nils.gprc.cityscore.CityScoreResponse; import com.nils.gprc.cityscore.CityScoreServiceGrpc; import io.grpc.stub.StreamObserver; import net.devh.boot.grpc.server.service.GrpcService; import org.springframework.beans.factory.annotation.Autowired;  @GrpcService public class CityScoreService extends CityScoreServiceGrpc.CityScoreServiceImplBase {      @Autowired     private ValidationService validationService;      @Override     public void calculateCityScore(CityScoreRequest request, StreamObserver&lt;CityScoreResponse> responseObserver) {         \/\/ System.out.println(\"Request received from client:\\n\" + request);          validationService.validateCityCode(request.getCityCode());          Integer cityScore = request.getCityCode() * 10;          CityScoreResponse response = CityScoreResponse.newBuilder()                 .setCityScore(cityScore)                 .build();          responseObserver.onNext(response);         responseObserver.onCompleted();     } }<\/code><\/pre>\n<h3>\u0412\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430<\/h3>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440\u044b \u0438 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u043e\u0448\u0438\u0431\u043e\u043a \u0432 gPRC \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c <a href=\"https:\/\/www.vinsguru.com\/grpc-error-handling\/\"><u>\u0437\u0434\u0435\u0441\u044c<\/u><\/a>.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435, \u0447\u0442\u043e\u0431\u044b \u0432\u043c\u0435\u0441\u0442\u043e \u0432\u044b\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u043d\u0438\u044f \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u043b\u0441\u044f \u043e\u0442\u0432\u0435\u0442 \u043e\u0431 \u043e\u0448\u0438\u0431\u043a\u0435, \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c &#171;oneof&#187; \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043e\u0442\u0432\u0435\u0442 success \u0434\u043b\u044f \u0443\u0441\u043f\u0435\u0448\u043d\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0438 \u043e\u0442\u0432\u0435\u0442 error \u0434\u043b\u044f \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0439:<\/p>\n<pre><code class=\"java\">oneof response {     SuccessResponse success_response = 1;     ErrorResponse error_response = 2;   } }<\/code><\/pre>\n<p>\u0423 \u043c\u0435\u043d\u044f \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u044f \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043b &#171;GrpcAdvice&#187; \u0438 &#171;GrpcExceptionHandler&#187;, \u0447\u0442\u043e\u0431\u044b \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0431\u044b\u043b\u043e \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u044b\u043c \u0441 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u043c \u043a\u043e\u0434\u043e\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f gPRC. \u0423\u0437\u043d\u0430\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u0435 \u043c\u043e\u0436\u043d\u043e \u043d\u0430 <a href=\"https:\/\/yidongnan.github.io\/grpc-spring-boot-starter\/en\/server\/exception-handling.html\"><u>\u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u043c \u0441\u0430\u0439\u0442\u0435<\/u><\/a> \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 Spring.<\/p>\n<p>\u0415\u0441\u0442\u044c \u0434\u0432\u0430 \u0441\u043f\u043e\u0441\u043e\u0431\u0430, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0432 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0438 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0443\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e. \u041e\u043d\u0438 \u043e\u043f\u0438\u0441\u0430\u043d\u044b <a href=\"https:\/\/stackoverflow.com\/questions\/48748745\/pattern-for-rich-error-handling-in-grpc\"><u>\u0437\u0434\u0435\u0441\u044c<\/u><\/a>.<\/p>\n<ul>\n<li>\n<p>\u041c\u0435\u0442\u0430\u0434\u0430\u043d\u043d\u044b\u0435<\/p>\n<\/li>\n<li>\n<p>Any.pack(yourCustomExceptionResponseObject)<\/p>\n<\/li>\n<\/ul>\n<p>CityScoreException \u2014 \u044d\u0442\u043e \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u043e\u0435 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 RuntimeException, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u044f \u0441\u043e\u0437\u0434\u0430\u043b \u0434\u043b\u044f \u043e\u0448\u0438\u0431\u043e\u043a \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0430. \u0427\u0442\u043e\u0431\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u043c\u043e\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 &#171;CityScoreExceptionResponse&#187;, \u0432\u0435\u0440\u043d\u0438\u0442\u0435\u0441\u044c \u043a \u043c\u043e\u0435\u043c\u0443 proto-\u0444\u0430\u0439\u043b\u0443. \u042d\u0442\u043e \u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 &#171;Grpc Exception Handler&#187;:<\/p>\n<pre><code class=\"java\">package com.nils.microservices.cityscore.exception;  import com.google.protobuf.Any; import com.google.protobuf.Timestamp; import com.google.rpc.Code; import com.google.rpc.Status; import com.nils.gprc.cityscore.CityScoreExceptionResponse; import io.grpc.StatusRuntimeException; import io.grpc.protobuf.StatusProto; import net.devh.boot.grpc.server.advice.GrpcAdvice; import net.devh.boot.grpc.server.advice.GrpcExceptionHandler;  import java.time.Instant;  @GrpcAdvice public class CityScoreExceptionHandler {      @GrpcExceptionHandler(CityScoreException.class)     public StatusRuntimeException handleValidationError(CityScoreException cause) {          Instant time = Instant.now();         Timestamp timestamp = Timestamp.newBuilder().setSeconds(time.getEpochSecond())                 .setNanos(time.getNano()).build();          CityScoreExceptionResponse exceptionResponse =                 CityScoreExceptionResponse.newBuilder()                         .setErrorCode(cause.getErrorCode())                         .setTimestamp(timestamp)                         .build();           Status status = Status.newBuilder()                         .setCode(Code.INVALID_ARGUMENT.getNumber())                         .setMessage(\"Invalid city code\")                         .addDetails(Any.pack(exceptionResponse))                         .build();          return StatusProto.toStatusRuntimeException(status);     } }<\/code><\/pre>\n<h3>\u041f\u043e\u0440\u0442\u044b \u0434\u043b\u044f \u0441\u0435\u0440\u0432\u0438\u0441\u0430<\/h3>\n<p>\u041f\u043e\u0440\u0442 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0434\u043b\u044f \u0441\u0435\u0440\u0432\u0435\u0440\u0430 gRPC \u2014 9090. \u0414\u0440\u0443\u0433\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043c\u043e\u0436\u043d\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 &#171;grpc.server.port&#187;:<\/p>\n<pre><code>grpc.server.port=8000<\/code><\/pre>\n<h3>\u0420\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u043e\u0439 \u0447\u0430\u0441\u0442\u0438<\/h3>\n<p>\u0412\u0430\u043c \u043d\u0443\u0436\u043d\u0430 &#171;\u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u0430\u044f&#187; \u0432\u0435\u0440\u0441\u0438\u044f grpc spring-boot-starter \u0432 \u0444\u0430\u0439\u043b\u0435 pom.xml:<\/p>\n<pre><code>&lt;dependency>    &lt;groupId>net.devh&lt;\/groupId>    &lt;artifactId>grpc-client-spring-boot-starter&lt;\/artifactId>    &lt;version>2.12.0.RELEASE&lt;\/version> &lt;\/dependency>  &lt;dependency>    &lt;groupId>com.nils.gprc&lt;\/groupId>    &lt;artifactId>proto-common&lt;\/artifactId>    &lt;version>0.0.1-SNAPSHOT&lt;\/version> &lt;\/dependency><\/code><\/pre>\n<p>\u042d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0441\u0435\u0440\u0432\u0438\u0441-\u0430\u0433\u0440\u0435\u0433\u0430\u0442\u043e\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0431\u0438\u0440\u0430\u0442\u044c \u043e\u0442\u0432\u0435\u0442\u044b \u043e\u0442 \u0432\u044b\u0437\u043e\u0432\u043e\u0432 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0441\u0435\u0440\u0432\u0438\u0441\u0430, \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u044f\u0442\u044c \u0438\u0445 \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u043a\u043e\u043d\u0435\u0447\u043d\u043e\u043c\u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e \u0447\u0435\u0440\u0435\u0437 RestController (\u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043e\u043d \u0442\u0430\u043a\u0436\u0435 \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c &#171;spring-boot-starter-web&#187;).<\/p>\n<p>\u0412\u043e\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f:<\/p>\n<pre><code class=\"java\">package com.nils.microservices.scorecalculator.service;  import com.google.protobuf.Any; import com.google.protobuf.InvalidProtocolBufferException; import com.google.protobuf.UInt64Value; import com.google.rpc.Status; import com.nils.gprc.cityscore.CityScoreExceptionResponse; import com.nils.gprc.cityscore.CityScoreRequest; import com.nils.gprc.cityscore.CityScoreResponse; import com.nils.gprc.cityscore.CityScoreServiceGrpc; import com.nils.gprc.scoresegment.ScoreSegmentExceptionResponse; import com.nils.gprc.scoresegment.ScoreSegmentRequest; import com.nils.gprc.scoresegment.ScoreSegmentResponse; import com.nils.gprc.scoresegment.ScoreSegmentServiceGrpc; import com.nils.microservices.scorecalculator.domain.IncomeBracketMultiplierInfo; import com.nils.microservices.scorecalculator.exception.ScoreCalculatorException; import com.nils.microservices.scorecalculator.model.ScoreCalculatorErrorCode; import com.nils.microservices.scorecalculator.model.ScoreCalculatorRequest; import io.grpc.StatusRuntimeException; import io.grpc.protobuf.StatusProto; import net.devh.boot.grpc.client.inject.GrpcClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;  import java.math.BigInteger; import java.util.Optional;  @Service public class ScoreCalculatorService {      @GrpcClient(\"city-score\")     private CityScoreServiceGrpc.CityScoreServiceBlockingStub cityScoreStub;      @GrpcClient(\"score-segment\")     private ScoreSegmentServiceGrpc.ScoreSegmentServiceBlockingStub scoreSegmentStub;      @Autowired     private IncomeBracketMultiplierInfoService incomeBracketMultiplierInfoService;       public BigInteger calculateScore(ScoreCalculatorRequest scoreCalculatorRequest) {         IncomeBracketMultiplierInfo selectedIncomeBracketMultiplerInfo = getIncomeBracketMultiplerInfo(scoreCalculatorRequest.getIncomeBracketMultiplierId());          BigInteger scoreSegment = getScoreSegment(scoreCalculatorRequest.getIdNumber());         Integer cityScore = getCityScore(scoreCalculatorRequest.getCityCode());         BigInteger score = BigInteger.valueOf(selectedIncomeBracketMultiplerInfo.getMultiplier().intValue())                                     .multiply(scoreSegment)                                     .add(BigInteger.valueOf(cityScore.intValue()));          return score;     }      private BigInteger getScoreSegment(BigInteger idNumber) {         ScoreSegmentRequest scoreSegmentRequest = ScoreSegmentRequest.newBuilder()                                                                     .setIdNumber(UInt64Value.newBuilder().setValue(idNumber.longValue()).build())                                                                     .build();         try {             ScoreSegmentResponse scoreSegmentResponse = scoreSegmentStub.calculateScoreSegment(scoreSegmentRequest);             return new BigInteger(scoreSegmentResponse.getScoreSegment().toString());         } catch (Exception e){             Status status = StatusProto.fromThrowable(e);             for (Any any : status.getDetailsList()) {                 if (!any.is(ScoreSegmentExceptionResponse.class)) {                     continue;                 }                 try {                     ScoreSegmentExceptionResponse exceptionResponse = any.unpack(ScoreSegmentExceptionResponse.class);                     System.out.println(\"timestamp: \" + exceptionResponse.getTimestamp() +                             \", errorCode : \" + exceptionResponse.getErrorCode());                 } catch (InvalidProtocolBufferException ex) {                     ex.printStackTrace();                 }             }             \/\/ System.out.println(status.getCode() + \" : \" + status.getDescription());         }          \/\/ return a default value         return BigInteger.ONE;     }      private Integer getCityScore(Integer cityCode) {         CityScoreRequest cityScoreRequest = CityScoreRequest.newBuilder()                                                             .setCityCode(cityCode)                                                             .build();         try {             CityScoreResponse cityScoreResponse = cityScoreStub.calculateCityScore(cityScoreRequest);             return cityScoreResponse.getCityScore();         } catch (StatusRuntimeException e){             Status status = StatusProto.fromThrowable(e);             for (Any any : status.getDetailsList()) {                 if (!any.is(CityScoreExceptionResponse.class)) {                     continue;                 }                 try {                     CityScoreExceptionResponse exceptionResponse = any.unpack(CityScoreExceptionResponse.class);                     System.out.println(\"timestamp: \" + exceptionResponse.getTimestamp() +                             \", errorCode : \" + exceptionResponse.getErrorCode());                 } catch (InvalidProtocolBufferException ex) {                     ex.printStackTrace();                 }             }             \/\/ System.out.println(status.getCode() + \" : \" + status.getDescription());         }          \/\/ return a default value         return Integer.valueOf(1);     }      private IncomeBracketMultiplierInfo getIncomeBracketMultiplerInfo(Long incomeBracketMultiplerInfoId) {         Optional&lt;IncomeBracketMultiplierInfo> multiplierInfo = incomeBracketMultiplierInfoService.findById(incomeBracketMultiplerInfoId);         if (!multiplierInfo.isPresent()) {             throw new ScoreCalculatorException(ScoreCalculatorErrorCode.INVALID_INCOME_BRACKET_MULTIPLIER_ID, incomeBracketMultiplerInfoId);         }         return multiplierInfo.get();     } }<\/code><\/pre>\n<p>\u041d\u0430\u043a\u043e\u043d\u0435\u0446, \u043d\u0435 \u0437\u0430\u0431\u0443\u0434\u044c\u0442\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0443\u0440\u043b\u044b \u0441\u0435\u0440\u0432\u0438\u0441\u0430 grpc \u0432 \u0444\u0430\u0439\u043b application.properties. \u0418\u043c\u0435\u043d\u0430 \u0441\u0432\u043e\u0439\u0441\u0442\u0432 \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u0442\u0430\u043a\u0438\u043c\u0438 \u0436\u0435, \u043a\u0430\u043a \u0430\u043d\u043d\u043e\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 <a class=\"mention\" href=\"\/users\/GrpcClient\">@GrpcClient<\/a><\/p>\n<pre><code>grpc.client.city-score.address=static:\/\/localhost:8000 grpc.client.city-score.negotiation-type=plaintext  grpc.client.score-segment.address=static:\/\/localhost:8100 grpc.client.score-segment.negotiation-type=plaintext<\/code><\/pre>\n<h3>\u0412\u0440\u0435\u043c\u044f \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c!<\/h3>\n<p>\u0414\u043b\u044f \u0432\u044b\u0437\u043e\u0432\u0430 \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <a href=\"https:\/\/github.com\/uw-labs\/bloomrpc\"><u>BloomRPC<\/u><\/a> (\u043a\u0430\u043a \u0432\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435 Postman \u0434\u043b\u044f \u0432\u044b\u0437\u043e\u0432\u043e\u0432 REST API).<\/p>\n<p>\u0414\u043b\u044f \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \u043d\u0430 Mac \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 HomeBrew:<\/p>\n<pre><code>brew install --cask bloomrpc<\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \u0432\u044b \u043d\u0430\u0439\u0434\u0435\u0442\u0435 \u0435\u0433\u043e \u0432 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445.<\/p>\n<p>\u0414\u0440\u0443\u0433\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u2014 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c <a href=\"https:\/\/github.com\/fullstorydev\/grpcurl\"><u>gRPCurl<\/u><\/a> \u0434\u043b\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 cURL \u0441 gPRC. \u0421\u043d\u043e\u0432\u0430 \u043c\u043e\u0436\u043d\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e HomeBrew:<\/p>\n<pre><code>brew install grpcurl<\/code><\/pre>\n<p>\u042f \u0431\u0443\u0434\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c BloomRPC \u0434\u043b\u044f \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u044d\u043d\u0434\u043f\u043e\u0438\u043d\u0442\u043e\u0432. \u041c\u044b \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043d\u0430\u0448\u0438 proto-\u0444\u0430\u0439\u043b\u044b \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043d\u043e\u043f\u043a\u0438 \u201d+\u201d:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/257\/7ce\/ca4\/2577ceca4679925f059077901490759b.png\" width=\"720\" height=\"395\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/257\/7ce\/ca4\/2577ceca4679925f059077901490759b.png\"\/><\/figure>\n<p>\u041d\u0430\u0436\u0438\u043c\u0430\u0435\u043c \u043d\u0430 \u043c\u0435\u0442\u043e\u0434, \u0437\u0434\u0435\u0441\u044c \u044d\u0442\u043e &#171;calculateCityScore&#187;:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/96a\/f03\/1bc\/96af031bcba41aa233d0431dec6ef549.png\" alt=\"\" title=\"\" width=\"720\" height=\"393\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/96a\/f03\/1bc\/96af031bcba41aa233d0431dec6ef549.png\"\/><\/figure>\n<p>\u041e\u043d \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u043e\u0431\u0440\u0430\u0437\u0435\u0446 \u0437\u0430\u043f\u0440\u043e\u0441\u0430. \u041c\u044b \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u043c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u043f\u043e\u0440\u0442\u0435 (grpc.server.port) \u0434\u043b\u044f \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u0438 \u043d\u0430\u0436\u0438\u043c\u0430\u0435\u043c \u043a\u043d\u043e\u043f\u043a\u0443 play:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/9a6\/d34\/b55\/9a6d34b55f983b0347ff331d34a0d4cd.png\" alt=\"\" title=\"\" width=\"720\" height=\"395\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/9a6\/d34\/b55\/9a6d34b55f983b0347ff331d34a0d4cd.png\"\/><\/figure>\n<p>\u0427\u0442\u043e\u0431\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u043a\u0435\u0439\u0441 \u0441 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435\u043c, \u044f \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043b \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u044f city_code \u0438 \u043d\u0430\u0436\u0430\u043b \u043a\u043d\u043e\u043f\u043a\u0443 play:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/ce6\/577\/a69\/ce6577a697b9503b346f54f10559b497.png\" alt=\"\" title=\"\" width=\"720\" height=\"394\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/ce6\/577\/a69\/ce6577a697b9503b346f54f10559b497.png\"\/><\/figure>\n<p>\u041d\u0430\u043a\u043e\u043d\u0435\u0446, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u043d\u0430\u0448 \u0441\u0435\u0440\u0432\u0438\u0441-\u0430\u0433\u0440\u0435\u0433\u0430\u0442\u043e\u0440 \u2014 Score Calculator, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f Postman:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/0d7\/4e6\/36a\/0d74e636a4abd5d60618af8f08d268d0.png\" alt=\"\" title=\"\" width=\"720\" height=\"378\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/0d7\/4e6\/36a\/0d74e636a4abd5d60618af8f08d268d0.png\"\/><\/figure>\n<p>\u0415\u0441\u043b\u0438 \u044f \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044e &#171;-35&#187; \u0434\u043b\u044f cityCode \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u044e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u043d\u0443\u044e \u0447\u0430\u0441\u0442\u044c \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f, \u0442\u043e \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u044c \u0431\u0443\u0434\u0443\u0442 \u0432\u044b\u0432\u0435\u0434\u0435\u043d\u044b \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0439, \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0445 \u0432 \u043e\u0442\u0432\u0435\u0442:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/57f\/1df\/0db\/57f1df0db09acc891c2d3a0485b3bc9f.png\" alt=\"\" title=\"\" width=\"720\" height=\"476\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/57f\/1df\/0db\/57f1df0db09acc891c2d3a0485b3bc9f.png\"\/><\/figure>\n<p>\u041f\u043e\u0441\u043b\u0435\u0434\u043d\u044f\u044f \u0432\u0435\u0440\u0441\u0438\u044f \u043c\u043e\u0435\u0433\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u043b\u0435\u0436\u0438\u0442 <a href=\"https:\/\/github.com\/senoritadeveloper01\/nils-spring-microservices-grpc\"><u>\u0437\u0434\u0435\u0441\u044c<\/u><\/a>.<\/p>\n<p><em>\u041f\u0435\u0440\u0435\u0432\u0435\u043b\u0430: <\/em><a href=\"https:\/\/habr.com\/ru\/users\/kmoseenk\/publications\/articles\/\"><em>\u041a\u0441\u0435\u043d\u0438\u044f \u041c\u043e\u0441\u0435\u0435\u043d\u043a\u043e\u0432\u0430<\/em><\/a><\/p>\n<hr\/>\n<p>\u0421\u043a\u043e\u0440\u043e \u0441\u043e\u0441\u0442\u043e\u0438\u0442\u0441\u044f <a href=\"https:\/\/otus.pw\/Qj5j\/\">\u043e\u0442\u043a\u0440\u044b\u0442\u043e\u0435 \u0437\u0430\u043d\u044f\u0442\u0438\u0435<\/a> \u00ab\u0421\u0432\u043e\u0439\u0441\u0442\u0432\u0430 Spring-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u00bb. \u041d\u0430 \u0432\u0441\u0442\u0440\u0435\u0447\u0435 \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c, \u043a\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043c\u043e\u0436\u043d\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043d\u0430 \u0447\u0438\u0441\u0442\u043e\u043c Spring, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0437\u0430\u0442\u0440\u043e\u043d\u0435\u043c \u0442\u0435\u043c\u0443 \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0430\u0446\u0438\u0438 \u0442\u0438\u043f\u043e\u0432. <\/p>\n<ul>\n<li>\n<p><a href=\"https:\/\/otus.pw\/Qj5j\/\">\u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u043e\u0442\u043a\u0440\u044b\u0442\u043e\u0435 \u0437\u0430\u043d\u044f\u0442\u0438\u0435<\/a><\/p>\n<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/730740\/\"> https:\/\/habr.com\/ru\/articles\/730740\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0425\u043e\u0440\u043e\u0448\u043e \u0431\u044b \u043f\u043e\u043d\u0438\u043c\u0430\u0442\u044c \u0440\u0430\u0437\u043b\u0438\u0447\u0438\u044f \u043c\u0435\u0436\u0434\u0443 HTTP\/1.1 \u0438 HTTP\/2, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 gRPC \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 HTTP\/2 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e.<\/p>\n<h3>HTTP\/1.1 vs HTTP\/2<\/h3>\n<p>\u0425\u0430\u0440\u0430\u043a\u0442\u0435\u0440\u0438\u0441\u0442\u0438\u043a\u0438 HTTP\/1.1:<\/p>\n<ul>\n<li>\n<p>\u0422\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0439 \u0444\u043e\u0440\u043c\u0430\u0442<\/p>\n<\/li>\n<li>\n<p>\u0417\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0438 \u0432 \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u043e\u043c \u0444\u043e\u0440\u043c\u0430\u0442\u0435<\/p>\n<\/li>\n<li>\n<p>TCP-\u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u00ab\u0442\u0440\u0435\u0445\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0433\u043e \u0440\u0443\u043a\u043e\u043f\u043e\u0436\u0430\u0442\u0438\u044f\u00bb (three-way handshake) \u2014 \u043e\u0434\u0438\u043d \u0437\u0430\u043f\u0440\u043e\u0441 \u0438 \u043e\u0442\u0432\u0435\u0442 \u0441 \u043e\u0434\u043d\u0438\u043c \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c TCP-\u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435\u043c.<\/p>\n<\/li>\n<\/ul>\n<p>\u0425\u0430\u0440\u0430\u043a\u0442\u0435\u0440\u0438\u0441\u0442\u0438\u043a\u0438 HTTP\/2:<\/p>\n<ul>\n<li>\n<p>\u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u0444\u043e\u0440\u043c\u0430\u0442<\/p>\n<\/li>\n<li>\n<p>\u0421\u0436\u0430\u0442\u0438\u0435 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u043e\u0432<\/p>\n<\/li>\n<li>\n<p>\u0423\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043f\u043e\u0442\u043e\u043a\u043e\u043c<\/p>\n<\/li>\n<li>\n<p>\u041c\u0443\u043b\u044c\u0442\u0438\u043f\u043b\u0435\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 (\u043e\u0434\u043d\u043e \u0438 \u0442\u043e \u0436\u0435 TCP-\u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u043e \u0434\u043b\u044f \u043c\u0443\u043b\u044c\u0442\u0438\u043f\u043b\u0435\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f. \u041f\u043e\u0442\u043e\u043a\u043e\u0432\u0430\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 \u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u2014 \u043f\u043e\u0442\u043e\u043a\u043e\u0432\u0430\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 \u043e\u0442 \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u2014 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u0430 \u0434\u0432\u0443\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u0430\u044f \u043f\u043e\u0442\u043e\u043a\u043e\u0432\u0430\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0430)<\/p>\n<\/li>\n<\/ul>\n<figure class=\"full-width\"><\/figure>\n<p>\u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c, \u043a\u0430\u043a \u0432\u0435\u0434\u0435\u0442 \u0441\u0435\u0431\u044f \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u043a\u0430\u0436\u0434\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u0434\u043b\u044f HTTP1 \u0438 HTTP2 (\u043c\u0443\u043b\u044c\u0442\u0438\u043f\u043b\u0435\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435) \u043c\u043e\u0436\u043d\u043e <a href=\"https:\/\/http2.akamai.com\/demo\"><u>\u043f\u043e \u044d\u0442\u043e\u0439 \u0441\u0441\u044b\u043b\u043a\u0435<\/u><\/a>.<\/p>\n<p>\u0412 \u044d\u0442\u043e\u043c \u0440\u0430\u0437\u0434\u0435\u043b\u0435 \u043f\u043e\u0441\u0442\u0430\u0440\u0430\u0435\u043c\u0441\u044f \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0432 \u043f\u0440\u0438\u0447\u0438\u043d\u0430\u0445, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u043f\u043e\u0432\u043b\u0438\u044f\u0442\u044c \u043d\u0430 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043e \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0435 \u0441 REST \u0438 JSON \u043a gRPC \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c Protocol Buffers.<\/p>\n<h3>JSON vs gRPC (\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 Protocol Buffers)<\/h3>\n<p>JSON<\/p>\n<ul>\n<li>\n<p>\u041d\u0435\u0442 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0441\u0445\u0435\u043c\u044b \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430.<\/p>\n<\/li>\n<li>\n<p>\u0422\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0439 \u0444\u043e\u0440\u043c\u0430\u0442 (\u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f\/\u0434\u0435\u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e \u0438 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u0437\u0430\u0442\u0440\u0430\u0442 \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432).<\/p>\n<\/li>\n<li>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0432 REST.<\/p>\n<\/li>\n<\/ul>\n<p>gPRC<\/p>\n<ul>\n<li>\n<p>\u0421\u0442\u0440\u043e\u0433\u043e\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0441\u0445\u0435\u043c\u044b \u0438 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u044c \u0442\u0438\u043f\u043e\u0432 (IDL: \u044f\u0437\u044b\u043a \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 \u0434\u043b\u044f API).<\/p>\n<\/li>\n<li>\n<p>\u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439, \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442 \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e\/\u0434\u0435\u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0431\u044b\u0441\u0442\u0440\u0435\u0435.<\/p>\n<\/li>\n<li>\n<p>\u0410\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430, \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u0430\u044f \u0434\u043b\u044f \u043c\u0435\u0436\u0441\u0435\u0440\u0432\u0438\u0441\u043d\u043e\u0433\u043e \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f.<\/p>\n<\/li>\n<li>\n<p>HTTP\/2 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043c\u0443\u043b\u044c\u0442\u0438\u043f\u043b\u0435\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435.<\/p>\n<\/li>\n<\/ul>\n<figure class=\"full-width\">\n<div><figcaption>gPRC \u043a\u043b\u0438\u0435\u043d\u0442-\u0441\u0435\u0440\u0432\u0438\u0441\u043d\u0430\u044f \u043a\u043e\u043c\u043c\u0443\u043d\u0438\u043a\u0430\u0446\u0438\u044f<\/figcaption><\/div>\n<\/figure>\n<figure class=\"full-width\">\n<div><figcaption>\u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 (\u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043d\u043e\u0433\u043e, \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u043e\u0433\u043e \u0438 \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u043e\u0432)<\/figcaption><\/div>\n<\/figure>\n<p>\u0421\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043e \u0432 <a href=\"https:\/\/www.vinsguru.com\/grpc-vs-rest-performance-comparison\/\"><u>\u044d\u0442\u043e\u043c \u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u0435<\/u><\/a>. \u041d\u0430 \u0432\u0438\u0434\u0435\u043e \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u044e \u0432 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0438:<\/p>\n<div class=\"tm-iframe_temp\" data-src=\"https:\/\/embedd.srv.habr.com\/iframe\/644271644d2bbedf9b4488ff\" data-style=\"\" id=\"644271644d2bbedf9b4488ff\" width=\"\"><\/div>\n<h3>\u0422\u0438\u043f\u044b \u0434\u0430\u043d\u043d\u044b\u0445<\/h3>\n<p>int32 (\u0434\u043b\u044f int) \u2014 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e: 0<br \/>int64 (\u0434\u043b\u044f long) \u2014 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e: 0<br \/>float \u2014 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e: 0<br \/>double \u2014 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e: 0<br \/>bool \u2014 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e: false<br \/>string \u2014 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e: \u043f\u0443\u0441\u0442\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430<br \/>\u0431\u0430\u0439\u0442 (\u0434\u043b\u044f <em>byte<\/em>[])<br \/>repeated (\u0434\u043b\u044f List\/Collection)<br \/>map (\u0434\u043b\u044f Map) \u2014 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e: <em>empty map<br \/><\/em>enum \u2014 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e: \u043f\u0435\u0440\u0432\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 \u0441\u043f\u0438\u0441\u043a\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439.<\/p>\n<p>\u0415\u0441\u0442\u044c \u0442\u0430\u043a\u0436\u0435 \u043a\u043b\u0430\u0441\u0441\u044b-\u043e\u0431\u0451\u0440\u0442\u043a\u0438 (\u043a\u0430\u043a Integer \u0432 Java), \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c, \u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0432 \u0438\u0445 \u0432 proto-\u0444\u0430\u0439\u043b.<\/p>\n<pre><code>import \u201cgoogle\/protobuf\/wrappers.proto\u201d;<\/code><\/pre>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435:<\/p>\n<pre><code>google.protobuf.UInt64Value id_number = 1;<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u043d\u0443\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u043e\u043b\u0435 \u043c\u0435\u0442\u043a\u0438 \u0432\u0440\u0435\u043c\u0435\u043d\u0438, \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0438\u043c\u043f\u043e\u0440\u0442, \u043a\u0430\u043a \u043e\u043f\u0438\u0441\u0430\u043d\u043e <a href=\"https:\/\/developers.google.com\/protocol-buffers\/docs\/reference\/csharp\/class\/google\/protobuf\/well-known-types\/timestamp\"><u>\u0437\u0434\u0435\u0441\u044c<\/u><\/a>:<\/p>\n<pre><code>import \"google\/protobuf\/timestamp.proto\";<\/code><\/pre>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435:<\/p>\n<pre><code>google.protobuf.Timestamp timestamp = 2;<\/code><\/pre>\n<h3>\u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u043f\u0440\u043e\u0435\u043a\u0442\u0430<\/h3>\n<p>\u0420\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u0435\u0442\u0441\u044f \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u043e\u0434\u0443\u043b\u044c \u0434\u043b\u044f proto-\u043c\u043e\u0434\u0435\u043b\u0438 \u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0439 \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432 \u0434\u043b\u044f \u043e\u0431\u0449\u0435\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f (\u043a\u0430\u043a \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044c).<\/p>\n<p>\u0412 \u0441\u043e\u0433\u043b\u0430\u0448\u0435\u043d\u0438\u0438 \u043e\u0431 \u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u0438 proto-\u0444\u0430\u0439\u043b\u043e\u0432 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u0435\u0442\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c &#171;<em>lower_snake_case.proto<\/em>&#171;. \u0420\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u043e \u043f\u043e \u0441\u0442\u0438\u043b\u044e \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c <a href=\"https:\/\/docs.buf.build\/best-practices\/style-guide\/\"><u>\u0437\u0434\u0435\u0441\u044c<\/u><\/a>.<\/p>\n<p>\u0418\u043c\u0435\u043d\u0430 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0442\u0430\u043a\u0436\u0435 \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u044b \u0432 \u043d\u0438\u0436\u043d\u0435\u043c \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0435 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0437\u043d\u0430\u043a\u0430 \u043f\u043e\u0434\u0447\u0435\u0440\u043a\u0438\u0432\u0430\u043d\u0438\u044f \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u0435\u043b\u044f \u043c\u0435\u0436\u0434\u0443 \u0441\u043b\u043e\u0432\u0430\u043c\u0438.<\/p>\n<p>\u042f \u0441\u043e\u0437\u0434\u0430\u043b proto-\u0444\u0430\u0439\u043b \u0441 \u0438\u043c\u0435\u043d\u0435\u043c &#171;city_score.proto&#187;:<\/p>\n<pre><code class=\"java\">syntax = \"proto3\";  import \"google\/protobuf\/timestamp.proto\";  package cityscore;  option java_package = \"com.nils.gprc.cityscore\"; option java_multiple_files = true;  message CityScoreRequest {   int32 city_code = 1; }  message CityScoreResponse {   int32 city_score = 1; }  enum CityScoreErrorCode {   INVALID_CITY_CODE_VALUE = 0;   CITY_CODE_CANNOT_BE_NULL = 1; }  message CityScoreExceptionResponse {   google.protobuf.Timestamp timestamp = 1;   CityScoreErrorCode error_code = 2; }  service CityScoreService {   \/\/ unary   rpc calculateCityScore(CityScoreRequest) returns (CityScoreResponse) {}; }<\/code><\/pre>\n<p>\u041f\u0440\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 \u0432 Maven \u044f \u043f\u043e\u043b\u0443\u0447\u0438\u043b \u043e\u0448\u0438\u0431\u043a\u0443:<\/p>\n<figure class=\"full-width\"><\/figure>\n<p>\u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u043e\u0431\u0430\u0432\u0438\u043b \u044d\u0442\u0438 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430:<\/p>\n<pre><code>&lt;project.build.sourceEncoding>UTF-8&lt;\/project.build.sourceEncoding> &lt;maven.compiler.source>16&lt;\/maven.compiler.source> &lt;maven.compiler.target>16&lt;\/maven.compiler.target><\/code><\/pre>\n<p>\u0417\u0430\u0442\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u043b \u043e\u0448\u0438\u0431\u043a\u0443 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438:<\/p>\n<figure class=\"full-width\"><\/figure>\n<p>\u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u044f \u043e\u0431\u043d\u043e\u0432\u0438\u043b \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 Maven \u0434\u043e \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439 \u0432\u0435\u0440\u0441\u0438\u0438, \u043a\u0430\u043a \u0443\u043a\u0430\u0437\u0430\u043d\u043e <a href=\"https:\/\/github.com\/grpc\/grpc-java\"><u>\u0437\u0434\u0435\u0441\u044c<\/u><\/a>:<\/p>\n<pre><code>&lt;dependency>   &lt;groupId>io.grpc&lt;\/groupId>   &lt;artifactId>grpc-netty-shaded&lt;\/artifactId>   &lt;version>1.41.0&lt;\/version> &lt;\/dependency> &lt;dependency>   &lt;groupId>io.grpc&lt;\/groupId>   &lt;artifactId>grpc-protobuf&lt;\/artifactId>   &lt;version>1.41.0&lt;\/version> &lt;\/dependency> &lt;dependency>   &lt;groupId>io.grpc&lt;\/groupId>   &lt;artifactId>grpc-stub&lt;\/artifactId>   &lt;version>1.41.0&lt;\/version> &lt;\/dependency> &lt;dependency> &lt;!-- necessary for Java 9+ -->   &lt;groupId>org.apache.tomcat&lt;\/groupId>   &lt;artifactId>annotations-api&lt;\/artifactId>   &lt;version>6.0.53&lt;\/version>   &lt;scope>provided&lt;\/scope> &lt;\/dependency><\/code><\/pre>\n<p>\u0412\u0441\u0435 \u0448\u043b\u043e \u0445\u043e\u0440\u043e\u0448\u043e, \u043f\u043e\u043a\u0430 \u044f \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043b \u0432 \u043f\u0440\u043e\u0435\u043a\u0442\u0435 \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u043e\u0448\u0438\u0431\u043a\u0443 \u043d\u0435\u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u044f \u0432\u0435\u0440\u0441\u0438\u0439 proto-\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439 (\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u044f\u0432\u0438\u043b\u0438\u0441\u044c \u043f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u044f \u0434\u043e\u0431\u0430\u0432\u0438\u043b \u043e\u0431\u0449\u0438\u0439 proto-\u043c\u043e\u0434\u0443\u043b\u044c \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438) \u0438 &#171;grpc-server-spring-boot-starter&#187;, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u043d\u0435 \u043f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u043f\u043e\u043d\u0438\u0437\u0438\u0442\u044c \u0432\u0435\u0440\u0441\u0438\u0438 proto-\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439 \u0434\u043e 1.37. \u0418\u043c\u0435\u043d\u043d\u043e \u044d\u0442\u0443 \u0432\u0435\u0440\u0441\u0438\u044e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u044f\u044f \u0432\u0435\u0440\u0441\u0438\u044f grpc-server-spring-boot-starter \u2014 2.12.0.RELEASE:<\/p>\n<pre><code>&lt;dependencies>     &lt;dependency>         &lt;groupId>io.grpc&lt;\/groupId>         &lt;artifactId>grpc-netty-shaded&lt;\/artifactId>         &lt;version>1.37.0&lt;\/version>     &lt;\/dependency>     &lt;dependency>         &lt;groupId>io.grpc&lt;\/groupId>         &lt;artifactId>grpc-protobuf&lt;\/artifactId>         &lt;version>1.37.0&lt;\/version>     &lt;\/dependency>     &lt;dependency>         &lt;groupId>io.grpc&lt;\/groupId>         &lt;artifactId>grpc-stub&lt;\/artifactId>         &lt;version>1.37.0&lt;\/version>     &lt;\/dependency>     &lt;dependency> &lt;!-- necessary for Java 9+ -->         &lt;groupId>org.apache.tomcat&lt;\/groupId>         &lt;artifactId>annotations-api&lt;\/artifactId>         &lt;version>6.0.53&lt;\/version>         &lt;scope>provided&lt;\/scope>     &lt;\/dependency> &lt;\/dependencies><\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u0439 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 \u0432 Maven \u044f \u043f\u043e\u043b\u0443\u0447\u0438\u043b \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0438\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0438 \u0434\u043b\u044f proto-\u0444\u0430\u0439\u043b\u0430:<\/p>\n<figure class=\"full-width\"><\/figure>\n<h3>\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043c\u043e\u0435\u0433\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u0430<\/h3>\n<ul>\n<li>\n<p>City Score Service<\/p>\n<\/li>\n<li>\n<p>Score Segment Service<\/p>\n<\/li>\n<li>\n<p>Score Calculator Service (\u0441\u0435\u0440\u0432\u0438\u0441-\u0430\u0433\u0440\u0435\u0433\u0430\u0442\u043e\u0440, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u043a\u0430\u043a City Score, \u0442\u0430\u043a \u0438 Score Segment).<\/p>\n<\/li>\n<\/ul>\n<p>\u041a\u0430\u043a \u0432\u044b \u0443\u0432\u0438\u0434\u0438\u0442\u0435 \u0438\u0437 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u043a\u043e\u0434\u0430, \u044f \u0431\u0443\u0434\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0434\u0438\u043d\u043e\u0447\u043d\u044b\u0435 \u0432\u044b\u0437\u043e\u0432\u044b \u0432 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438.<\/p>\n<h3>\u0420\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0441\u0435\u0440\u0432\u0438\u0441\u0430<\/h3>\n<p>\u0412\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u0430 &#171;\u0441\u0435\u0440\u0432\u0435\u0440\u043d\u0430\u044f&#187; \u0432\u0435\u0440\u0441\u0438\u044f grpc spring-boot-starter \u0432 \u0444\u0430\u0439\u043b\u0435 pom.xml:<\/p>\n<pre><code>&lt;dependency>    &lt;groupId>net.devh&lt;\/groupId>    &lt;artifactId>grpc-server-spring-boot-starter&lt;\/artifactId>    &lt;version>2.12.0.RELEASE&lt;\/version> &lt;\/dependency>  &lt;dependency>    &lt;groupId>com.nils.gprc&lt;\/groupId>    &lt;artifactId>proto-common&lt;\/artifactId>    &lt;version>0.0.1-SNAPSHOT&lt;\/version> &lt;\/dependency><\/code><\/pre>\n<p>\u042d\u0442\u043e \u043c\u043e\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f City Score Service:<\/p>\n<pre><code class=\"java\">package com.nils.microservices.cityscore.service;  import com.nils.gprc.cityscore.CityScoreRequest; import com.nils.gprc.cityscore.CityScoreResponse; import com.nils.gprc.cityscore.CityScoreServiceGrpc; import io.grpc.stub.StreamObserver; import net.devh.boot.grpc.server.service.GrpcService; import org.springframework.beans.factory.annotation.Autowired;  @GrpcService public class CityScoreService extends CityScoreServiceGrpc.CityScoreServiceImplBase {      @Autowired     private ValidationService validationService;      @Override     public void calculateCityScore(CityScoreRequest request, StreamObserver&lt;CityScoreResponse> responseObserver) {         \/\/ System.out.println(\"Request received from client:\\n\" + request);          validationService.validateCityCode(request.getCityCode());          Integer cityScore = request.getCityCode() * 10;          CityScoreResponse response = CityScoreResponse.newBuilder()                 .setCityScore(cityScore)                 .build();          responseObserver.onNext(response);         responseObserver.onCompleted();     } }<\/code><\/pre>\n<h3>\u0412\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430<\/h3>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440\u044b \u0438 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u043e\u0448\u0438\u0431\u043e\u043a \u0432 gPRC \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c <a href=\"https:\/\/www.vinsguru.com\/grpc-error-handling\/\"><u>\u0437\u0434\u0435\u0441\u044c<\/u><\/a>.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435, \u0447\u0442\u043e\u0431\u044b \u0432\u043c\u0435\u0441\u0442\u043e \u0432\u044b\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u043d\u0438\u044f \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u043b\u0441\u044f \u043e\u0442\u0432\u0435\u0442 \u043e\u0431 \u043e\u0448\u0438\u0431\u043a\u0435, \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c &#171;oneof&#187; \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043e\u0442\u0432\u0435\u0442 success \u0434\u043b\u044f \u0443\u0441\u043f\u0435\u0448\u043d\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0438 \u043e\u0442\u0432\u0435\u0442 error \u0434\u043b\u044f \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0439:<\/p>\n<pre><code class=\"java\">oneof response {     SuccessResponse success_response = 1;     ErrorResponse error_response = 2;   } }<\/code><\/pre>\n<p>\u0423 \u043c\u0435\u043d\u044f \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u044f \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043b &#171;GrpcAdvice&#187; \u0438 &#171;GrpcExceptionHandler&#187;, \u0447\u0442\u043e\u0431\u044b \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0431\u044b\u043b\u043e \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u044b\u043c \u0441 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u043c \u043a\u043e\u0434\u043e\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f gPRC. \u0423\u0437\u043d\u0430\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u0435 \u043c\u043e\u0436\u043d\u043e \u043d\u0430 <a href=\"https:\/\/yidongnan.github.io\/grpc-spring-boot-starter\/en\/server\/exception-handling.html\"><u>\u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u043c \u0441\u0430\u0439\u0442\u0435<\/u><\/a> \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 Spring.<\/p>\n<p>\u0415\u0441\u0442\u044c \u0434\u0432\u0430 \u0441\u043f\u043e\u0441\u043e\u0431\u0430, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0432 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0438 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0443\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e. \u041e\u043d\u0438 \u043e\u043f\u0438\u0441\u0430\u043d\u044b <a href=\"https:\/\/stackoverflow.com\/questions\/48748745\/pattern-for-rich-error-handling-in-grpc\"><u>\u0437\u0434\u0435\u0441\u044c<\/u><\/a>.<\/p>\n<ul>\n<li>\n<p>\u041c\u0435\u0442\u0430\u0434\u0430\u043d\u043d\u044b\u0435<\/p>\n<\/li>\n<li>\n<p>Any.pack(yourCustomExceptionResponseObject)<\/p>\n<\/li>\n<\/ul>\n<p>CityScoreException \u2014 \u044d\u0442\u043e \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u043e\u0435 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 RuntimeException, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u044f \u0441\u043e\u0437\u0434\u0430\u043b \u0434\u043b\u044f \u043e\u0448\u0438\u0431\u043e\u043a \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0430. \u0427\u0442\u043e\u0431\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u043c\u043e\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 &#171;CityScoreExceptionResponse&#187;, \u0432\u0435\u0440\u043d\u0438\u0442\u0435\u0441\u044c \u043a \u043c\u043e\u0435\u043c\u0443 proto-\u0444\u0430\u0439\u043b\u0443. \u042d\u0442\u043e \u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 &#171;Grpc Exception Handler&#187;:<\/p>\n<pre><code class=\"java\">package com.nils.microservices.cityscore.exception;  import com.google.protobuf.Any; import com.google.protobuf.Timestamp; import com.google.rpc.Code; import com.google.rpc.Status; import com.nils.gprc.cityscore.CityScoreExceptionResponse; import io.grpc.StatusRuntimeException; import io.grpc.protobuf.StatusProto; import net.devh.boot.grpc.server.advice.GrpcAdvice; import net.devh.boot.grpc.server.advice.GrpcExceptionHandler;  import java.time.Instant;  @GrpcAdvice public class CityScoreExceptionHandler {      @GrpcExceptionHandler(CityScoreException.class)     public StatusRuntimeException handleValidationError(CityScoreException cause) {          Instant time = Instant.now();         Timestamp timestamp = Timestamp.newBuilder().setSeconds(time.getEpochSecond())                 .setNanos(time.getNano()).build();          CityScoreExceptionResponse exceptionResponse =                 CityScoreExceptionResponse.newBuilder()                         .setErrorCode(cause.getErrorCode())                         .setTimestamp(timestamp)                         .build();           Status status = Status.newBuilder()                         .setCode(Code.INVALID_ARGUMENT.getNumber())                         .setMessage(\"Invalid city code\")                         .addDetails(Any.pack(exceptionResponse))                         .build();          return StatusProto.toStatusRuntimeException(status);     } }<\/code><\/pre>\n<h3>\u041f\u043e\u0440\u0442\u044b \u0434\u043b\u044f \u0441\u0435\u0440\u0432\u0438\u0441\u0430<\/h3>\n<p>\u041f\u043e\u0440\u0442 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0434\u043b\u044f \u0441\u0435\u0440\u0432\u0435\u0440\u0430 gRPC \u2014 9090. \u0414\u0440\u0443\u0433\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043c\u043e\u0436\u043d\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 &#171;grpc.server.port&#187;:<\/p>\n<pre><code>grpc.server.port=8000<\/code><\/pre>\n<h3>\u0420\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u043e\u0439 \u0447\u0430\u0441\u0442\u0438<\/h3>\n<p>\u0412\u0430\u043c \u043d\u0443\u0436\u043d\u0430<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-416798","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/416798","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=416798"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/416798\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=416798"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=416798"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=416798"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}