{"id":24710,"date":"2021-11-28T16:36:11","date_gmt":"2021-11-28T08:36:11","guid":{"rendered":"https:\/\/aqzt.com\/24710.html"},"modified":"2021-11-28T16:36:11","modified_gmt":"2021-11-28T08:36:11","slug":"go-kit%e5%ae%9e%e8%b7%b5%e4%b9%8b1%ef%bc%9ago-kit-%e4%b8%8e-grpc-%e7%bb%93%e5%90%88%e5%bc%80%e5%8f%91%e5%be%ae%e6%9c%8d%e5%8a%a1%ef%bc%88go-kit-endpoint%e7%9a%84%e4%bd%bf%e7%94%a8%ef%bc%89-2","status":"publish","type":"post","link":"https:\/\/aqzt.com\/24710.html","title":{"rendered":"go-kit\u5b9e\u8df5\u4e4b1\uff1ago-kit \u4e0e grpc \u7ed3\u5408\u5f00\u53d1\u5fae\u670d\u52a1\uff08go-kit endpoint\u7684\u4f7f\u7528\uff09"},"content":{"rendered":"<p><strong>go-kit<\/strong>\u00a0\u662f\u4e00\u4e2a\u5fae\u670d\u52a1\u7684\u5f00\u53d1\u5de5\u5177\u96c6\uff0c\u5fae\u670d\u52a1\u7cfb\u7edf\u4e2d\u7684\u5927\u591a\u6570\u5e38\u89c1\u95ee\u9898\uff0c\u56e0\u6b64\uff0c\u4f7f\u7528\u8005\u53ef\u4ee5\u5c06\u7cbe\u529b\u96c6\u4e2d\u5728\u4e1a\u52a1\u903b\u8f91\u4e0a\u3002<\/p>\n<\/p>\n<p>grpc\u7f3a\u4e4f\u670d\u52a1\u6cbb\u7406\u7684\u529f\u80fd\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7go-kit\u7ed3\u5408grpc\u6765\u5b9e\u73b0\u6211\u4eec\u7684\u5b8c\u6574\u9700\u6c42\u3002go-kit\u62bd\u8c61\u7684endpoint\u8bbe\u8ba1\u8ba9\u6211\u4eec\u53ef\u4ee5\u5f88\u5bb9\u6613\u5305\u88c5\u5176\u5b83\u5fae\u670d\u52a1\u6846\u67b6\u4f7f\u7528\u7684\u534f\u8bae\u3002<br \/>\n<strong>endpoint\u4e3aServers\u548cClients\u63d0\u4f9b\u4e86\u57fa\u4e8eRPC\u65b9\u6cd5\u7684\u6784\u5efa\u6a21\u5757\u3002<\/strong><\/p>\n<p>\u5148\u6765\u770b\u770bendpoint\u6e90\u7801\uff1a<\/p>\n<pre>\n\n<div class=\"codecolorer-container text solarized-dark language-go\" style=\"overflow:auto;white-space:nowrap;width:800px;height:300px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<br \/>23<br \/>24<br \/>25<br \/>26<br \/>27<br \/>28<br \/>29<br \/>30<br \/>31<br \/>32<br \/>33<br \/>34<br \/>35<br \/>36<br \/>37<br \/>38<br \/>39<br \/>40<br \/>41<br \/>42<br \/><\/div><\/td><td><div class=\"text codecolorer\">1package endpoint<br \/>\n2<br \/>\n3import (<br \/>\n4 &nbsp; &amp;quot;context&amp;quot;<br \/>\n5)<br \/>\n6<br \/>\n7\/\/ Endpoint is the fundamental building block of servers and clients.<br \/>\n8\/\/ It represents a single RPC method.<br \/>\n9type Endpoint func(ctx context.Context, request interface{}) (response interface{}, err error)<br \/>\n10<br \/>\n11\/\/ Nop is an endpoint that does nothing and returns a nil error.<br \/>\n12\/\/ Useful for tests.<br \/>\n13func Nop(context.Context, interface{}) (interface{}, error) { return struct{}{}, nil }<br \/>\n14<br \/>\n15\/\/ Middleware is a chainable behavior modifier for endpoints.<br \/>\n16type Middleware func(Endpoint) Endpoint<br \/>\n17<br \/>\n18\/\/ Chain is a helper function for composing middlewares. Requests will<br \/>\n19\/\/ traverse them in the order they&amp;#x27;re declared. That is, the first middleware<br \/>\n20\/\/ is treated as the outermost middleware.<br \/>\n21func Chain(outer Middleware, others ...Middleware) Middleware {<br \/>\n22&nbsp; return func(next Endpoint) Endpoint {<br \/>\n23&nbsp; &nbsp; &nbsp; for i := len(others) - 1; i &amp;gt;= 0; i-- { \/\/ reverse<br \/>\n24&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; next = others[i](next)<br \/>\n25&nbsp; &nbsp; &nbsp; }<br \/>\n26&nbsp; &nbsp; &nbsp; return outer(next)<br \/>\n27&nbsp; }<br \/>\n28}<br \/>\n29<br \/>\n30\/\/ Failer may be implemented by Go kit response types that contain business<br \/>\n31\/\/ logic error details. If Failed returns a non-nil error, the Go kit transport<br \/>\n32\/\/ layer may interpret this as a business logic error, and may encode it<br \/>\n33\/\/ differently than a regular, successful response.<br \/>\n34\/\/<br \/>\n35\/\/ It&amp;#x27;s not necessary for your response types to implement Failer, but it may<br \/>\n36\/\/ help for more sophisticated use cases. The addsvc example shows how Failer<br \/>\n37\/\/ should be used by a complete application.<br \/>\n38type Failer interface {<br \/>\n39&nbsp; Failed() error<br \/>\n40}<br \/>\n41<br \/>\n42<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<p><strong>go-kit\u63d0\u4f9b\u4ee5\u4e0b\u529f\u80fd\uff1a<\/strong><\/p>\n<p>1\u3001Circuit breaker\uff08\u7194\u65ad\u5668\uff09<\/p>\n<p>2\u3001Rate limiter\uff08\u9650\u6d41\u5668\uff09<\/p>\n<p>3\u3001Logging\uff08\u65e5\u5fd7\uff09<\/p>\n<p>4\u3001Metrics\uff08Prometheus\u7edf\u8ba1\uff09<\/p>\n<p>5\u3001Request tracing\uff08\u8bf7\u6c42\u8ddf\u8e2a\uff09<\/p>\n<p>6\u3001Service discovery and load balancing\uff08\u670d\u52a1\u53d1\u73b0\u548c\u8d1f\u8f7d\u5747\u8861\uff09<\/p>\n<h3>1\u3001\u5b89\u88c5go-kit\u5305<\/h3>\n<pre>\n\n<div class=\"codecolorer-container text solarized-dark language-bash\" style=\"overflow:auto;white-space:nowrap;width:800px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/><\/div><\/td><td><div class=\"text codecolorer\">1git clone https:\/\/github.com\/go-kit\/kit.git<br \/>\n2<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<p>\u653e\u5165\u5230\u5bf9\u5e94\u7684gopath\u76ee\u5f55\u4e0b\u3002<\/p>\n<h3>2\u3001protobuf\u6587\u4ef6<\/h3>\n<pre>\n\n<div class=\"codecolorer-container text solarized-dark language-cpp\" style=\"overflow:auto;white-space:nowrap;width:800px;height:300px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<br \/>23<br \/>24<br \/>25<br \/>26<br \/>27<br \/>28<br \/>29<br \/>30<br \/>31<br \/><\/div><\/td><td><div class=\"text codecolorer\">1syntax = &amp;quot;proto3&amp;quot;;<br \/>\n2<br \/>\n3\/\/ \u8bf7\u6c42\u4e66\u8be6\u60c5\u7684\u53c2\u6570\u7ed3\u6784 &nbsp;book_id 32\u4f4d\u6574\u5f62<br \/>\n4message BookInfoParams {<br \/>\n5 &nbsp; &nbsp;int32 book_id = 1;<br \/>\n6}<br \/>\n7<br \/>\n8<br \/>\n9\/\/ \u4e66\u8be6\u60c5\u4fe1\u606f\u7684\u7ed3\u6784 &nbsp; book_name\u5b57\u7b26\u4e32\u7c7b\u578b<br \/>\n10message BookInfo {<br \/>\n11 &nbsp; &nbsp;int32 book_id = 1;<br \/>\n12 &nbsp; &nbsp;string &nbsp;book_name = 2;<br \/>\n13}<br \/>\n14<br \/>\n15\/\/ \u8bf7\u6c42\u4e66\u5217\u8868\u7684\u53c2\u6570\u7ed3\u6784 &nbsp;page\u3001limit &nbsp; 32\u4f4d\u6574\u5f62<br \/>\n16message BookListParams {<br \/>\n17 &nbsp; &nbsp;int32 page = 1;<br \/>\n18 &nbsp; &nbsp;int32 limit = 2;<br \/>\n19}<br \/>\n20<br \/>\n21<br \/>\n22\/\/ \u4e66\u5217\u8868\u7684\u7ed3\u6784 &nbsp; &nbsp;BookInfo\u7ed3\u6784\u6570\u7ec4<br \/>\n23message BookList {<br \/>\n24 &nbsp; &nbsp;repeated BookInfo book_list = 1;<br \/>\n25}<br \/>\n26\/\/ \u5b9a\u4e49 \u83b7\u53d6\u4e66\u8be6\u60c5 &nbsp;\u548c \u4e66\u5217\u8868\u670d\u52a1 &nbsp; \u5165\u53c2\u51fa\u53c2\u5206\u522b\u4e3a\u4e0a\u9762\u6240\u5b9a\u4e49\u7684\u7ed3\u6784<br \/>\n27service BookService {<br \/>\n28 &nbsp; &nbsp;rpc GetBookInfo (BookInfoParams) returns (BookInfo) {}<br \/>\n29 &nbsp; &nbsp;rpc GetBookList (BookListParams) returns (BookList) {}<br \/>\n30}<br \/>\n31<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<p>\u751f\u6210\u5bf9\u5e94\u7684go\u8bed\u8a00\u4ee3\u7801\u6587\u4ef6\uff1aprotoc &#8211;go_out=plugins=grpc:. book.proto\u00a0 \uff08\u5176\u4e2d\uff1aprotobuf\u6587\u4ef6\u540d\u4e3a\uff1abook.proto\uff09,\u751f\u6210\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>\n\n<div class=\"codecolorer-container text solarized-dark language-go\" style=\"overflow:auto;white-space:nowrap;width:800px;height:300px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<br \/>23<br \/>24<br \/>25<br \/>26<br \/>27<br \/>28<br \/>29<br \/>30<br \/>31<br \/>32<br \/>33<br \/>34<br \/>35<br \/>36<br \/>37<br \/>38<br \/>39<br \/>40<br \/>41<br \/>42<br \/>43<br \/>44<br \/>45<br \/>46<br \/>47<br \/>48<br \/>49<br \/>50<br \/>51<br \/>52<br \/>53<br \/>54<br \/>55<br \/>56<br \/>57<br \/>58<br \/>59<br \/>60<br \/>61<br \/>62<br \/>63<br \/>64<br \/>65<br \/>66<br \/>67<br \/>68<br \/>69<br \/>70<br \/>71<br \/>72<br \/>73<br \/>74<br \/>75<br \/>76<br \/>77<br \/>78<br \/>79<br \/>80<br \/>81<br \/>82<br \/>83<br \/>84<br \/>85<br \/>86<br \/>87<br \/>88<br \/>89<br \/>90<br \/>91<br \/>92<br \/>93<br \/>94<br \/>95<br \/>96<br \/>97<br \/>98<br \/>99<br \/>100<br \/>101<br \/>102<br \/>103<br \/>104<br \/>105<br \/>106<br \/>107<br \/>108<br \/>109<br \/>110<br \/>111<br \/>112<br \/>113<br \/>114<br \/>115<br \/>116<br \/>117<br \/>118<br \/>119<br \/>120<br \/>121<br \/>122<br \/>123<br \/>124<br \/>125<br \/>126<br \/>127<br \/>128<br \/>129<br \/>130<br \/>131<br \/>132<br \/>133<br \/>134<br \/>135<br \/>136<br \/>137<br \/>138<br \/>139<br \/>140<br \/>141<br \/>142<br \/>143<br \/>144<br \/>145<br \/>146<br \/>147<br \/>148<br \/>149<br \/>150<br \/>151<br \/>152<br \/>153<br \/>154<br \/>155<br \/>156<br \/>157<br \/>158<br \/>159<br \/>160<br \/>161<br \/>162<br \/>163<br \/>164<br \/>165<br \/>166<br \/>167<br \/>168<br \/>169<br \/>170<br \/>171<br \/>172<br \/>173<br \/>174<br \/>175<br \/>176<br \/>177<br \/>178<br \/>179<br \/>180<br \/>181<br \/>182<br \/>183<br \/>184<br \/>185<br \/>186<br \/>187<br \/>188<br \/>189<br \/>190<br \/>191<br \/>192<br \/>193<br \/>194<br \/>195<br \/>196<br \/>197<br \/>198<br \/>199<br \/>200<br \/>201<br \/>202<br \/>203<br \/>204<br \/>205<br \/>206<br \/>207<br \/>208<br \/>209<br \/>210<br \/>211<br \/>212<br \/>213<br \/>214<br \/>215<br \/>216<br \/>217<br \/>218<br \/>219<br \/>220<br \/>221<br \/>222<br \/>223<br \/>224<br \/>225<br \/>226<br \/>227<br \/>228<br \/>229<br \/>230<br \/>231<br \/>232<br \/>233<br \/>234<br \/>235<br \/>236<br \/>237<br \/>238<br \/>239<br \/>240<br \/>241<br \/>242<br \/>243<br \/>244<br \/>245<br \/>246<br \/>247<br \/>248<br \/>249<br \/>250<br \/>251<br \/>252<br \/>253<br \/>254<br \/>255<br \/>256<br \/>257<br \/>258<br \/>259<br \/>260<br \/>261<br \/>262<br \/>263<br \/>264<br \/>265<br \/>266<br \/>267<br \/>268<br \/>269<br \/>270<br \/>271<br \/>272<br \/>273<br \/>274<br \/>275<br \/>276<br \/>277<br \/>278<br \/>279<br \/>280<br \/>281<br \/>282<br \/>283<br \/>284<br \/>285<br \/>286<br \/>287<br \/>288<br \/>289<br \/>290<br \/>291<br \/>292<br \/>293<br \/>294<br \/>295<br \/>296<br \/>297<br \/>298<br \/>299<br \/>300<br \/>301<br \/>302<br \/>303<br \/>304<br \/>305<br \/>306<br \/>307<br \/>308<br \/>309<br \/>310<br \/>311<br \/>312<br \/>313<br \/>314<br \/>315<br \/>316<br \/>317<br \/>318<br \/>319<br \/>320<br \/>321<br \/>322<br \/>323<br \/>324<br \/>325<br \/>326<br \/>327<br \/>328<br \/>329<br \/>330<br \/>331<br \/>332<br \/>333<br \/>334<br \/>335<br \/>336<br \/>337<br \/>338<br \/>339<br \/>340<br \/>341<br \/>342<br \/>343<br \/>344<br \/>345<br \/>346<br \/><\/div><\/td><td><div class=\"text codecolorer\">1\/\/ Code generated by protoc-gen-go. DO NOT EDIT.<br \/>\n2\/\/ source: book.proto<br \/>\n3<br \/>\n4package book<br \/>\n5<br \/>\n6import (<br \/>\n7 &nbsp; context &amp;quot;context&amp;quot;<br \/>\n8 &nbsp; fmt &amp;quot;fmt&amp;quot;<br \/>\n9 &nbsp; proto &amp;quot;github.com\/golang\/protobuf\/proto&amp;quot;<br \/>\n10&nbsp; grpc &amp;quot;google.golang.org\/grpc&amp;quot;<br \/>\n11&nbsp; codes &amp;quot;google.golang.org\/grpc\/codes&amp;quot;<br \/>\n12&nbsp; status &amp;quot;google.golang.org\/grpc\/status&amp;quot;<br \/>\n13&nbsp; math &amp;quot;math&amp;quot;<br \/>\n14)<br \/>\n15<br \/>\n16\/\/ Reference imports to suppress errors if they are not otherwise used.<br \/>\n17var _ = proto.Marshal<br \/>\n18var _ = fmt.Errorf<br \/>\n19var _ = math.Inf<br \/>\n20<br \/>\n21\/\/ This is a compile-time assertion to ensure that this generated file<br \/>\n22\/\/ is compatible with the proto package it is being compiled against.<br \/>\n23\/\/ A compilation error at this line likely means your copy of the<br \/>\n24\/\/ proto package needs to be updated.<br \/>\n25const _ = proto.ProtoPackageIsVersion3 \/\/ please upgrade the proto package<br \/>\n26<br \/>\n27\/\/ \u8bf7\u6c42\u4e66\u8be6\u60c5\u7684\u53c2\u6570\u7ed3\u6784 &nbsp;book_id 32\u4f4d\u6574\u5f62<br \/>\n28type BookInfoParams struct {<br \/>\n29&nbsp; BookId &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int32 &nbsp; &nbsp;`protobuf:&amp;quot;varint,1,opt,name=book_id,json=bookId,proto3&amp;quot; json:&amp;quot;book_id,omitempty&amp;quot;`<br \/>\n30&nbsp; XXX_NoUnkeyedLiteral struct{} `json:&amp;quot;-&amp;quot;`<br \/>\n31&nbsp; XXX_unrecognized &nbsp; &nbsp; []byte &nbsp; `json:&amp;quot;-&amp;quot;`<br \/>\n32&nbsp; XXX_sizecache &nbsp; &nbsp; &nbsp; &nbsp;int32 &nbsp; &nbsp;`json:&amp;quot;-&amp;quot;`<br \/>\n33}<br \/>\n34<br \/>\n35func (m *BookInfoParams) Reset() &nbsp; &nbsp; &nbsp; &nbsp; { *m = BookInfoParams{} }<br \/>\n36func (m *BookInfoParams) String() string { return proto.CompactTextString(m) }<br \/>\n37func (*BookInfoParams) ProtoMessage() &nbsp; &nbsp;{}<br \/>\n38func (*BookInfoParams) Descriptor() ([]byte, []int) {<br \/>\n39&nbsp; return fileDescriptor_1e89d0eaa98dc5d8, []int{0}<br \/>\n40}<br \/>\n41<br \/>\n42func (m *BookInfoParams) XXX_Unmarshal(b []byte) error {<br \/>\n43&nbsp; return xxx_messageInfo_BookInfoParams.Unmarshal(m, b)<br \/>\n44}<br \/>\n45func (m *BookInfoParams) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {<br \/>\n46&nbsp; return xxx_messageInfo_BookInfoParams.Marshal(b, m, deterministic)<br \/>\n47}<br \/>\n48func (m *BookInfoParams) XXX_Merge(src proto.Message) {<br \/>\n49&nbsp; xxx_messageInfo_BookInfoParams.Merge(m, src)<br \/>\n50}<br \/>\n51func (m *BookInfoParams) XXX_Size() int {<br \/>\n52&nbsp; return xxx_messageInfo_BookInfoParams.Size(m)<br \/>\n53}<br \/>\n54func (m *BookInfoParams) XXX_DiscardUnknown() {<br \/>\n55&nbsp; xxx_messageInfo_BookInfoParams.DiscardUnknown(m)<br \/>\n56}<br \/>\n57<br \/>\n58var xxx_messageInfo_BookInfoParams proto.InternalMessageInfo<br \/>\n59<br \/>\n60func (m *BookInfoParams) GetBookId() int32 {<br \/>\n61&nbsp; if m != nil {<br \/>\n62&nbsp; &nbsp; &nbsp; return m.BookId<br \/>\n63&nbsp; }<br \/>\n64&nbsp; return 0<br \/>\n65}<br \/>\n66<br \/>\n67\/\/ \u4e66\u8be6\u60c5\u4fe1\u606f\u7684\u7ed3\u6784 &nbsp; book_name\u5b57\u7b26\u4e32\u7c7b\u578b<br \/>\n68type BookInfo struct {<br \/>\n69&nbsp; BookId &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int32 &nbsp; &nbsp;`protobuf:&amp;quot;varint,1,opt,name=book_id,json=bookId,proto3&amp;quot; json:&amp;quot;book_id,omitempty&amp;quot;`<br \/>\n70&nbsp; BookName &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string &nbsp; `protobuf:&amp;quot;bytes,2,opt,name=book_name,json=bookName,proto3&amp;quot; json:&amp;quot;book_name,omitempty&amp;quot;`<br \/>\n71&nbsp; XXX_NoUnkeyedLiteral struct{} `json:&amp;quot;-&amp;quot;`<br \/>\n72&nbsp; XXX_unrecognized &nbsp; &nbsp; []byte &nbsp; `json:&amp;quot;-&amp;quot;`<br \/>\n73&nbsp; XXX_sizecache &nbsp; &nbsp; &nbsp; &nbsp;int32 &nbsp; &nbsp;`json:&amp;quot;-&amp;quot;`<br \/>\n74}<br \/>\n75<br \/>\n76func (m *BookInfo) Reset() &nbsp; &nbsp; &nbsp; &nbsp; { *m = BookInfo{} }<br \/>\n77func (m *BookInfo) String() string { return proto.CompactTextString(m) }<br \/>\n78func (*BookInfo) ProtoMessage() &nbsp; &nbsp;{}<br \/>\n79func (*BookInfo) Descriptor() ([]byte, []int) {<br \/>\n80&nbsp; return fileDescriptor_1e89d0eaa98dc5d8, []int{1}<br \/>\n81}<br \/>\n82<br \/>\n83func (m *BookInfo) XXX_Unmarshal(b []byte) error {<br \/>\n84&nbsp; return xxx_messageInfo_BookInfo.Unmarshal(m, b)<br \/>\n85}<br \/>\n86func (m *BookInfo) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {<br \/>\n87&nbsp; return xxx_messageInfo_BookInfo.Marshal(b, m, deterministic)<br \/>\n88}<br \/>\n89func (m *BookInfo) XXX_Merge(src proto.Message) {<br \/>\n90&nbsp; xxx_messageInfo_BookInfo.Merge(m, src)<br \/>\n91}<br \/>\n92func (m *BookInfo) XXX_Size() int {<br \/>\n93&nbsp; return xxx_messageInfo_BookInfo.Size(m)<br \/>\n94}<br \/>\n95func (m *BookInfo) XXX_DiscardUnknown() {<br \/>\n96&nbsp; xxx_messageInfo_BookInfo.DiscardUnknown(m)<br \/>\n97}<br \/>\n98<br \/>\n99var xxx_messageInfo_BookInfo proto.InternalMessageInfo<br \/>\n100<br \/>\n101func (m *BookInfo) GetBookId() int32 {<br \/>\n102 if m != nil {<br \/>\n103 &nbsp; &nbsp; return m.BookId<br \/>\n104 }<br \/>\n105 return 0<br \/>\n106}<br \/>\n107<br \/>\n108func (m *BookInfo) GetBookName() string {<br \/>\n109 if m != nil {<br \/>\n110 &nbsp; &nbsp; return m.BookName<br \/>\n111 }<br \/>\n112 return &amp;quot;&amp;quot;<br \/>\n113}<br \/>\n114<br \/>\n115\/\/ \u8bf7\u6c42\u4e66\u5217\u8868\u7684\u53c2\u6570\u7ed3\u6784 &nbsp;page\u3001limit &nbsp; 32\u4f4d\u6574\u5f62<br \/>\n116type BookListParams struct {<br \/>\n117 Page &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int32 &nbsp; &nbsp;`protobuf:&amp;quot;varint,1,opt,name=page,proto3&amp;quot; json:&amp;quot;page,omitempty&amp;quot;`<br \/>\n118 Limit &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int32 &nbsp; &nbsp;`protobuf:&amp;quot;varint,2,opt,name=limit,proto3&amp;quot; json:&amp;quot;limit,omitempty&amp;quot;`<br \/>\n119 XXX_NoUnkeyedLiteral struct{} `json:&amp;quot;-&amp;quot;`<br \/>\n120 XXX_unrecognized &nbsp; &nbsp; []byte &nbsp; `json:&amp;quot;-&amp;quot;`<br \/>\n121 XXX_sizecache &nbsp; &nbsp; &nbsp; &nbsp;int32 &nbsp; &nbsp;`json:&amp;quot;-&amp;quot;`<br \/>\n122}<br \/>\n123<br \/>\n124func (m *BookListParams) Reset() &nbsp; &nbsp; &nbsp; &nbsp; { *m = BookListParams{} }<br \/>\n125func (m *BookListParams) String() string { return proto.CompactTextString(m) }<br \/>\n126func (*BookListParams) ProtoMessage() &nbsp; &nbsp;{}<br \/>\n127func (*BookListParams) Descriptor() ([]byte, []int) {<br \/>\n128 return fileDescriptor_1e89d0eaa98dc5d8, []int{2}<br \/>\n129}<br \/>\n130<br \/>\n131func (m *BookListParams) XXX_Unmarshal(b []byte) error {<br \/>\n132 return xxx_messageInfo_BookListParams.Unmarshal(m, b)<br \/>\n133}<br \/>\n134func (m *BookListParams) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {<br \/>\n135 return xxx_messageInfo_BookListParams.Marshal(b, m, deterministic)<br \/>\n136}<br \/>\n137func (m *BookListParams) XXX_Merge(src proto.Message) {<br \/>\n138 xxx_messageInfo_BookListParams.Merge(m, src)<br \/>\n139}<br \/>\n140func (m *BookListParams) XXX_Size() int {<br \/>\n141 return xxx_messageInfo_BookListParams.Size(m)<br \/>\n142}<br \/>\n143func (m *BookListParams) XXX_DiscardUnknown() {<br \/>\n144 xxx_messageInfo_BookListParams.DiscardUnknown(m)<br \/>\n145}<br \/>\n146<br \/>\n147var xxx_messageInfo_BookListParams proto.InternalMessageInfo<br \/>\n148<br \/>\n149func (m *BookListParams) GetPage() int32 {<br \/>\n150 if m != nil {<br \/>\n151 &nbsp; &nbsp; return m.Page<br \/>\n152 }<br \/>\n153 return 0<br \/>\n154}<br \/>\n155<br \/>\n156func (m *BookListParams) GetLimit() int32 {<br \/>\n157 if m != nil {<br \/>\n158 &nbsp; &nbsp; return m.Limit<br \/>\n159 }<br \/>\n160 return 0<br \/>\n161}<br \/>\n162<br \/>\n163\/\/ \u4e66\u5217\u8868\u7684\u7ed3\u6784 &nbsp; &nbsp;BookInfo\u7ed3\u6784\u6570\u7ec4<br \/>\n164type BookList struct {<br \/>\n165 BookList &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; []*BookInfo `protobuf:&amp;quot;bytes,1,rep,name=book_list,json=bookList,proto3&amp;quot; json:&amp;quot;book_list,omitempty&amp;quot;`<br \/>\n166 XXX_NoUnkeyedLiteral struct{} &nbsp; &nbsp;`json:&amp;quot;-&amp;quot;`<br \/>\n167 XXX_unrecognized &nbsp; &nbsp; []byte &nbsp; &nbsp; &nbsp;`json:&amp;quot;-&amp;quot;`<br \/>\n168 XXX_sizecache &nbsp; &nbsp; &nbsp; &nbsp;int32 &nbsp; &nbsp; &nbsp; `json:&amp;quot;-&amp;quot;`<br \/>\n169}<br \/>\n170<br \/>\n171func (m *BookList) Reset() &nbsp; &nbsp; &nbsp; &nbsp; { *m = BookList{} }<br \/>\n172func (m *BookList) String() string { return proto.CompactTextString(m) }<br \/>\n173func (*BookList) ProtoMessage() &nbsp; &nbsp;{}<br \/>\n174func (*BookList) Descriptor() ([]byte, []int) {<br \/>\n175 return fileDescriptor_1e89d0eaa98dc5d8, []int{3}<br \/>\n176}<br \/>\n177<br \/>\n178func (m *BookList) XXX_Unmarshal(b []byte) error {<br \/>\n179 return xxx_messageInfo_BookList.Unmarshal(m, b)<br \/>\n180}<br \/>\n181func (m *BookList) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {<br \/>\n182 return xxx_messageInfo_BookList.Marshal(b, m, deterministic)<br \/>\n183}<br \/>\n184func (m *BookList) XXX_Merge(src proto.Message) {<br \/>\n185 xxx_messageInfo_BookList.Merge(m, src)<br \/>\n186}<br \/>\n187func (m *BookList) XXX_Size() int {<br \/>\n188 return xxx_messageInfo_BookList.Size(m)<br \/>\n189}<br \/>\n190func (m *BookList) XXX_DiscardUnknown() {<br \/>\n191 xxx_messageInfo_BookList.DiscardUnknown(m)<br \/>\n192}<br \/>\n193<br \/>\n194var xxx_messageInfo_BookList proto.InternalMessageInfo<br \/>\n195<br \/>\n196func (m *BookList) GetBookList() []*BookInfo {<br \/>\n197 if m != nil {<br \/>\n198 &nbsp; &nbsp; return m.BookList<br \/>\n199 }<br \/>\n200 return nil<br \/>\n201}<br \/>\n202<br \/>\n203func init() {<br \/>\n204 proto.RegisterType((*BookInfoParams)(nil), &amp;quot;BookInfoParams&amp;quot;)<br \/>\n205 proto.RegisterType((*BookInfo)(nil), &amp;quot;BookInfo&amp;quot;)<br \/>\n206 proto.RegisterType((*BookListParams)(nil), &amp;quot;BookListParams&amp;quot;)<br \/>\n207 proto.RegisterType((*BookList)(nil), &amp;quot;BookList&amp;quot;)<br \/>\n208}<br \/>\n209<br \/>\n210func init() { proto.RegisterFile(&amp;quot;book.proto&amp;quot;, fileDescriptor_1e89d0eaa98dc5d8) }<br \/>\n211<br \/>\n212var fileDescriptor_1e89d0eaa98dc5d8 = []byte{<br \/>\n213 \/\/ 224 bytes of a gzipped FileDescriptorProto<br \/>\n214 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0x4a, 0xca, 0xcf, 0xcf,<br \/>\n215 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x57, 0xd2, 0xe4, 0xe2, 0x73, 0xca, 0xcf, 0xcf, 0xf6, 0xcc,<br \/>\n216 0x4b, 0xcb, 0x0f, 0x48, 0x2c, 0x4a, 0xcc, 0x2d, 0x16, 0x12, 0xe7, 0x62, 0x07, 0xc9, 0xc7, 0x67,<br \/>\n217 0xa6, 0x48, 0x30, 0x2a, 0x30, 0x6a, 0xb0, 0x06, 0xb1, 0x81, 0xb8, 0x9e, 0x29, 0x4a, 0x0e, 0x5c,<br \/>\n218 0x1c, 0x30, 0xa5, 0x38, 0x15, 0x09, 0x49, 0x73, 0x71, 0x82, 0x25, 0xf2, 0x12, 0x73, 0x53, 0x25,<br \/>\n219 0x98, 0x14, 0x18, 0x35, 0x38, 0x83, 0x38, 0x40, 0x02, 0x7e, 0x89, 0xb9, 0xa9, 0x4a, 0x56, 0x10,<br \/>\n220 0xcb, 0x7c, 0x32, 0x8b, 0x4b, 0xa0, 0x96, 0x09, 0x71, 0xb1, 0x14, 0x24, 0xa6, 0xa7, 0x42, 0x0d,<br \/>\n221 0x01, 0xb3, 0x85, 0x44, 0xb8, 0x58, 0x73, 0x32, 0x73, 0x33, 0x4b, 0xc0, 0xda, 0x59, 0x83, 0x20,<br \/>\n222 0x1c, 0x25, 0x23, 0x88, 0xed, 0x20, 0xbd, 0x42, 0x6a, 0x50, 0x4b, 0x72, 0x32, 0x8b, 0x4b, 0x24,<br \/>\n223 0x18, 0x15, 0x98, 0x35, 0xb8, 0x8d, 0x38, 0xf5, 0x60, 0x6e, 0x83, 0xd8, 0x07, 0x52, 0x67, 0x94,<br \/>\n224 0xce, 0xc5, 0x0d, 0x12, 0x0d, 0x4e, 0x2d, 0x2a, 0xcb, 0x4c, 0x4e, 0x15, 0xd2, 0xe6, 0xe2, 0x76,<br \/>\n225 0x4f, 0x2d, 0x81, 0xfb, 0x81, 0x5f, 0x0f, 0xd5, 0xe7, 0x52, 0x08, 0x33, 0x94, 0x18, 0x90, 0x14,<br \/>\n226 0x83, 0xad, 0x84, 0x28, 0x46, 0xb8, 0x1c, 0xaa, 0x18, 0x24, 0xa0, 0xc4, 0x90, 0xc4, 0x06, 0x0e,<br \/>\n227 0x4c, 0x63, 0x40, 0x00, 0x00, 0x00, 0xff, 0xff, 0x3e, 0x22, 0x2f, 0xab, 0x5a, 0x01, 0x00, 0x00,<br \/>\n228}<br \/>\n229<br \/>\n230\/\/ Reference imports to suppress errors if they are not otherwise used.<br \/>\n231var _ context.Context<br \/>\n232var _ grpc.ClientConn<br \/>\n233<br \/>\n234\/\/ This is a compile-time assertion to ensure that this generated file<br \/>\n235\/\/ is compatible with the grpc package it is being compiled against.<br \/>\n236const _ = grpc.SupportPackageIsVersion4<br \/>\n237<br \/>\n238\/\/ BookServiceClient is the client API for BookService service.<br \/>\n239\/\/<br \/>\n240\/\/ For semantics around ctx use and closing\/ending streaming RPCs, please refer to https:\/\/godoc.org\/google.golang.org\/grpc#ClientConn.NewStream.<br \/>\n241type BookServiceClient interface {<br \/>\n242 GetBookInfo(ctx context.Context, in *BookInfoParams, opts ...grpc.CallOption) (*BookInfo, error)<br \/>\n243 GetBookList(ctx context.Context, in *BookListParams, opts ...grpc.CallOption) (*BookList, error)<br \/>\n244}<br \/>\n245<br \/>\n246type bookServiceClient struct {<br \/>\n247 cc *grpc.ClientConn<br \/>\n248}<br \/>\n249<br \/>\n250func NewBookServiceClient(cc *grpc.ClientConn) BookServiceClient {<br \/>\n251 return &amp;amp;bookServiceClient{cc}<br \/>\n252}<br \/>\n253<br \/>\n254func (c *bookServiceClient) GetBookInfo(ctx context.Context, in *BookInfoParams, opts ...grpc.CallOption) (*BookInfo, error) {<br \/>\n255 out := new(BookInfo)<br \/>\n256 err := c.cc.Invoke(ctx, &amp;quot;\/BookService\/GetBookInfo&amp;quot;, in, out, opts...)<br \/>\n257 if err != nil {<br \/>\n258 &nbsp; &nbsp; return nil, err<br \/>\n259 }<br \/>\n260 return out, nil<br \/>\n261}<br \/>\n262<br \/>\n263func (c *bookServiceClient) GetBookList(ctx context.Context, in *BookListParams, opts ...grpc.CallOption) (*BookList, error) {<br \/>\n264 out := new(BookList)<br \/>\n265 err := c.cc.Invoke(ctx, &amp;quot;\/BookService\/GetBookList&amp;quot;, in, out, opts...)<br \/>\n266 if err != nil {<br \/>\n267 &nbsp; &nbsp; return nil, err<br \/>\n268 }<br \/>\n269 return out, nil<br \/>\n270}<br \/>\n271<br \/>\n272\/\/ BookServiceServer is the server API for BookService service.<br \/>\n273type BookServiceServer interface {<br \/>\n274 GetBookInfo(context.Context, *BookInfoParams) (*BookInfo, error)<br \/>\n275 GetBookList(context.Context, *BookListParams) (*BookList, error)<br \/>\n276}<br \/>\n277<br \/>\n278\/\/ UnimplementedBookServiceServer can be embedded to have forward compatible implementations.<br \/>\n279type UnimplementedBookServiceServer struct {<br \/>\n280}<br \/>\n281<br \/>\n282func (*UnimplementedBookServiceServer) GetBookInfo(ctx context.Context, req *BookInfoParams) (*BookInfo, error) {<br \/>\n283 return nil, status.Errorf(codes.Unimplemented, &amp;quot;method GetBookInfo not implemented&amp;quot;)<br \/>\n284}<br \/>\n285func (*UnimplementedBookServiceServer) GetBookList(ctx context.Context, req *BookListParams) (*BookList, error) {<br \/>\n286 return nil, status.Errorf(codes.Unimplemented, &amp;quot;method GetBookList not implemented&amp;quot;)<br \/>\n287}<br \/>\n288<br \/>\n289func RegisterBookServiceServer(s *grpc.Server, srv BookServiceServer) {<br \/>\n290 s.RegisterService(&amp;amp;_BookService_serviceDesc, srv)<br \/>\n291}<br \/>\n292<br \/>\n293func _BookService_GetBookInfo_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {<br \/>\n294 in := new(BookInfoParams)<br \/>\n295 if err := dec(in); err != nil {<br \/>\n296 &nbsp; &nbsp; return nil, err<br \/>\n297 }<br \/>\n298 if interceptor == nil {<br \/>\n299 &nbsp; &nbsp; return srv.(BookServiceServer).GetBookInfo(ctx, in)<br \/>\n300 }<br \/>\n301 info := &amp;amp;grpc.UnaryServerInfo{<br \/>\n302 &nbsp; &nbsp; Server: &nbsp; &nbsp; srv,<br \/>\n303 &nbsp; &nbsp; FullMethod: &amp;quot;\/BookService\/GetBookInfo&amp;quot;,<br \/>\n304 }<br \/>\n305 handler := func(ctx context.Context, req interface{}) (interface{}, error) {<br \/>\n306 &nbsp; &nbsp; return srv.(BookServiceServer).GetBookInfo(ctx, req.(*BookInfoParams))<br \/>\n307 }<br \/>\n308 return interceptor(ctx, in, info, handler)<br \/>\n309}<br \/>\n310<br \/>\n311func _BookService_GetBookList_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {<br \/>\n312 in := new(BookListParams)<br \/>\n313 if err := dec(in); err != nil {<br \/>\n314 &nbsp; &nbsp; return nil, err<br \/>\n315 }<br \/>\n316 if interceptor == nil {<br \/>\n317 &nbsp; &nbsp; return srv.(BookServiceServer).GetBookList(ctx, in)<br \/>\n318 }<br \/>\n319 info := &amp;amp;grpc.UnaryServerInfo{<br \/>\n320 &nbsp; &nbsp; Server: &nbsp; &nbsp; srv,<br \/>\n321 &nbsp; &nbsp; FullMethod: &amp;quot;\/BookService\/GetBookList&amp;quot;,<br \/>\n322 }<br \/>\n323 handler := func(ctx context.Context, req interface{}) (interface{}, error) {<br \/>\n324 &nbsp; &nbsp; return srv.(BookServiceServer).GetBookList(ctx, req.(*BookListParams))<br \/>\n325 }<br \/>\n326 return interceptor(ctx, in, info, handler)<br \/>\n327}<br \/>\n328<br \/>\n329var _BookService_serviceDesc = grpc.ServiceDesc{<br \/>\n330 ServiceName: &amp;quot;BookService&amp;quot;,<br \/>\n331 HandlerType: (*BookServiceServer)(nil),<br \/>\n332 Methods: []grpc.MethodDesc{<br \/>\n333 &nbsp; &nbsp; {<br \/>\n334 &nbsp; &nbsp; &nbsp; &nbsp; MethodName: &amp;quot;GetBookInfo&amp;quot;,<br \/>\n335 &nbsp; &nbsp; &nbsp; &nbsp; Handler: &nbsp; &nbsp;_BookService_GetBookInfo_Handler,<br \/>\n336 &nbsp; &nbsp; },<br \/>\n337 &nbsp; &nbsp; {<br \/>\n338 &nbsp; &nbsp; &nbsp; &nbsp; MethodName: &amp;quot;GetBookList&amp;quot;,<br \/>\n339 &nbsp; &nbsp; &nbsp; &nbsp; Handler: &nbsp; &nbsp;_BookService_GetBookList_Handler,<br \/>\n340 &nbsp; &nbsp; },<br \/>\n341 },<br \/>\n342 Streams: &nbsp;[]grpc.StreamDesc{},<br \/>\n343 Metadata: &amp;quot;book.proto&amp;quot;,<br \/>\n344}<br \/>\n345<br \/>\n346<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<h3>3\u3001\u670d\u52a1\u5668\u7aef\u4ee3\u7801<\/h3>\n<pre>\n\n<div class=\"codecolorer-container text solarized-dark language-go\" style=\"overflow:auto;white-space:nowrap;width:800px;height:300px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<br \/>23<br \/>24<br \/>25<br \/>26<br \/>27<br \/>28<br \/>29<br \/>30<br \/>31<br \/>32<br \/>33<br \/>34<br \/>35<br \/>36<br \/>37<br \/>38<br \/>39<br \/>40<br \/>41<br \/>42<br \/>43<br \/>44<br \/>45<br \/>46<br \/>47<br \/>48<br \/>49<br \/>50<br \/>51<br \/>52<br \/>53<br \/>54<br \/>55<br \/>56<br \/>57<br \/>58<br \/>59<br \/>60<br \/>61<br \/>62<br \/>63<br \/>64<br \/>65<br \/>66<br \/>67<br \/>68<br \/>69<br \/>70<br \/>71<br \/>72<br \/>73<br \/>74<br \/>75<br \/>76<br \/>77<br \/>78<br \/>79<br \/>80<br \/>81<br \/>82<br \/>83<br \/>84<br \/>85<br \/>86<br \/>87<br \/>88<br \/>89<br \/>90<br \/>91<br \/>92<br \/>93<br \/>94<br \/>95<br \/>96<br \/>97<br \/><\/div><\/td><td><div class=\"text codecolorer\">1package main<br \/>\n2<br \/>\n3import (<br \/>\n4 &nbsp; &amp;quot;MyKit&amp;quot; \/\/import book.pb.go(protoc \u751f\u6210\u7684\u6587\u4ef6)<br \/>\n5 &nbsp; &amp;quot;context&amp;quot;<br \/>\n6 &nbsp; &amp;quot;github.com\/go-kit\/kit\/endpoint&amp;quot;<br \/>\n7 &nbsp; grpc_transport &amp;quot;github.com\/go-kit\/kit\/transport\/grpc&amp;quot;<br \/>\n8 &nbsp; &amp;quot;google.golang.org\/grpc&amp;quot;<br \/>\n9 &nbsp; &amp;quot;net&amp;quot;<br \/>\n10)<br \/>\n11<br \/>\n12type BookServer struct {<br \/>\n13&nbsp; bookListHandler grpc_transport.Handler<br \/>\n14&nbsp; bookInfoHandler grpc_transport.Handler<br \/>\n15}<br \/>\n16<br \/>\n17\/\/\u901a\u8fc7grpc\u8c03\u7528GetBookInfo\u65f6,GetBookInfo\u53ea\u505a\u6570\u636e\u900f\u4f20, \u8c03\u7528BookServer\u4e2d\u5bf9\u5e94Handler.ServeGRPC\u8f6c\u4ea4\u7ed9go-kit\u5904\u7406<br \/>\n18func (s *BookServer) GetBookInfo(ctx context.Context, in *book.BookInfoParams) (*book.BookInfo, error) {<br \/>\n19&nbsp; _, rsp, err := s.bookInfoHandler.ServeGRPC(ctx, in)<br \/>\n20&nbsp; if err != nil {<br \/>\n21&nbsp; &nbsp; &nbsp; return nil, err<br \/>\n22&nbsp; }<br \/>\n23&nbsp; return rsp.(*book.BookInfo), err<br \/>\n24}<br \/>\n25<br \/>\n26\/\/\u901a\u8fc7grpc\u8c03\u7528GetBookList\u65f6,GetBookList\u53ea\u505a\u6570\u636e\u900f\u4f20, \u8c03\u7528BookServer\u4e2d\u5bf9\u5e94Handler.ServeGRPC\u8f6c\u4ea4\u7ed9go-kit\u5904\u7406<br \/>\n27func (s *BookServer) GetBookList(ctx context.Context, in *book.BookListParams) (*book.BookList, error) {<br \/>\n28&nbsp; _, rsp, err := s.bookListHandler.ServeGRPC(ctx, in)<br \/>\n29&nbsp; if err != nil {<br \/>\n30&nbsp; &nbsp; &nbsp; return nil, err<br \/>\n31&nbsp; }<br \/>\n32&nbsp; return rsp.(*book.BookList), err<br \/>\n33}<br \/>\n34<br \/>\n35\/\/\u521b\u5efabookList\u7684EndPoint<br \/>\n36func makeGetBookListEndpoint() endpoint.Endpoint {<br \/>\n37&nbsp; return func(ctx context.Context, request interface{}) (interface{}, error) {<br \/>\n38&nbsp; &nbsp; &nbsp; \/\/\u8bf7\u6c42\u5217\u8868\u65f6\u8fd4\u56de \u4e66\u7c4d\u5217\u8868<br \/>\n39&nbsp; &nbsp; &nbsp; bl := new(book.BookList)<br \/>\n40&nbsp; &nbsp; &nbsp; bl.BookList = append(bl.BookList, &amp;amp;book.BookInfo{BookId: 1, BookName: &amp;quot;Go\u5165\u95e8\u5230\u7cbe\u901a&amp;quot;})<br \/>\n41&nbsp; &nbsp; &nbsp; bl.BookList = append(bl.BookList, &amp;amp;book.BookInfo{BookId: 2, BookName: &amp;quot;go-kit\u5165\u95e8\u5230\u7cbe\u901a&amp;quot;})<br \/>\n42&nbsp; &nbsp; &nbsp; bl.BookList = append(bl.BookList, &amp;amp;book.BookInfo{BookId: 2, BookName: &amp;quot;go-micro\u5165\u95e8\u5230\u7cbe\u901a&amp;quot;})<br \/>\n43&nbsp; &nbsp; &nbsp; return bl, nil<br \/>\n44&nbsp; }<br \/>\n45}<br \/>\n46<br \/>\n47\/\/\u521b\u5efabookInfo\u7684EndPoint<br \/>\n48func makeGetBookInfoEndpoint() endpoint.Endpoint {<br \/>\n49&nbsp; return func(ctx context.Context, request interface{}) (interface{}, error) {<br \/>\n50&nbsp; &nbsp; &nbsp; \/\/\u8bf7\u6c42\u8be6\u60c5\u65f6\u8fd4\u56de \u4e66\u7c4d\u4fe1\u606f<br \/>\n51&nbsp; &nbsp; &nbsp; req := request.(*book.BookInfoParams)<br \/>\n52&nbsp; &nbsp; &nbsp; b := new(book.BookInfo)<br \/>\n53&nbsp; &nbsp; &nbsp; b.BookId = req.BookId<br \/>\n54&nbsp; &nbsp; &nbsp; b.BookName = &amp;quot;Go\u4e0e\u5fae\u670d\u52a1&amp;quot;<br \/>\n55&nbsp; &nbsp; &nbsp; return b, nil<br \/>\n56&nbsp; }<br \/>\n57}<br \/>\n58<br \/>\n59func decodeRequest(_ context.Context, req interface{}) (interface{}, error) {<br \/>\n60&nbsp; return req, nil<br \/>\n61}<br \/>\n62<br \/>\n63func encodeResponse(_ context.Context, req interface{}) (interface{}, error) {<br \/>\n64&nbsp; return req, nil<br \/>\n65}<br \/>\n66<br \/>\n67func main() {<br \/>\n68&nbsp; \/\/\u5305\u88c5BookServer<br \/>\n69<br \/>\n70&nbsp; bookServer := new(BookServer)<br \/>\n71&nbsp; \/\/\u521b\u5efabookList\u7684Handler<br \/>\n72&nbsp; bookListHandler := grpc_transport.NewServer(<br \/>\n73&nbsp; &nbsp; &nbsp; makeGetBookListEndpoint(),<br \/>\n74&nbsp; &nbsp; &nbsp; decodeRequest,<br \/>\n75&nbsp; &nbsp; &nbsp; encodeResponse,<br \/>\n76&nbsp; )<br \/>\n77&nbsp; \/\/bookServer \u589e\u52a0 go-kit\u6d41\u7a0b\u7684 bookList\u5904\u7406\u903b\u8f91<br \/>\n78&nbsp; bookServer.bookListHandler = bookListHandler<br \/>\n79<br \/>\n80&nbsp; \/\/\u521b\u5efabookInfo\u7684Handler<br \/>\n81&nbsp; bookInfoHandler := grpc_transport.NewServer(<br \/>\n82&nbsp; &nbsp; &nbsp; makeGetBookInfoEndpoint(),<br \/>\n83&nbsp; &nbsp; &nbsp; decodeRequest,<br \/>\n84&nbsp; &nbsp; &nbsp; encodeResponse,<br \/>\n85&nbsp; )<br \/>\n86&nbsp; \/\/bookServer \u589e\u52a0 go-kit\u6d41\u7a0b\u7684 bookInfo\u5904\u7406\u903b\u8f91<br \/>\n87&nbsp; bookServer.bookInfoHandler = bookInfoHandler<br \/>\n88<br \/>\n89&nbsp; \/\/\u542f\u52a8grpc\u670d\u52a1<br \/>\n90&nbsp; serviceAddress := &amp;quot;:50052&amp;quot;<br \/>\n91&nbsp; ls, _ := net.Listen(&amp;quot;tcp&amp;quot;, serviceAddress)<br \/>\n92&nbsp; gs := grpc.NewServer()<br \/>\n93&nbsp; book.RegisterBookServiceServer(gs, bookServer)<br \/>\n94&nbsp; gs.Serve(ls)<br \/>\n95}<br \/>\n96\/\/Todo go-kit\u53c2\u8003\uff1ahttps:\/\/blog.csdn.net\/lihao19910921\/article\/details\/80166399<br \/>\n97<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<h3>4\u3001\u5ba2\u6237\u7aef\u4ee3\u7801<\/h3>\n<pre>\n\n<div class=\"codecolorer-container text solarized-dark language-go\" style=\"overflow:auto;white-space:nowrap;width:800px;height:300px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<br \/>23<br \/>24<br \/>25<br \/>26<br \/>27<br \/>28<br \/>29<br \/><\/div><\/td><td><div class=\"text codecolorer\">1package main<br \/>\n2<br \/>\n3import (<br \/>\n4 &nbsp; &amp;quot;MyKit&amp;quot;<br \/>\n5 &nbsp; &amp;quot;context&amp;quot;<br \/>\n6 &nbsp; &amp;quot;fmt&amp;quot;<br \/>\n7 &nbsp; &amp;quot;google.golang.org\/grpc&amp;quot;<br \/>\n8)<br \/>\n9<br \/>\n10func main() {<br \/>\n11&nbsp; serviceAddress := &amp;quot;127.0.0.1:50052&amp;quot;<br \/>\n12&nbsp; conn, err := grpc.Dial(serviceAddress, grpc.WithInsecure())<br \/>\n13&nbsp; if err != nil {<br \/>\n14&nbsp; &nbsp; &nbsp; panic(&amp;quot;connect error&amp;quot;)<br \/>\n15&nbsp; }<br \/>\n16&nbsp; defer conn.Close()<br \/>\n17&nbsp; bookClient := book.NewBookServiceClient(conn)<br \/>\n18&nbsp; bi, _ := bookClient.GetBookInfo(context.Background(), &amp;amp;book.BookInfoParams{BookId: 1})<br \/>\n19&nbsp; fmt.Print(&amp;quot;\u83b7\u53d6\u4e66\u7c4d\u4fe1\u606f:\\t&amp;quot;)<br \/>\n20&nbsp; fmt.Println(&amp;quot;bookInfo:&amp;quot;, bi.BookName)<br \/>\n21<br \/>\n22&nbsp; bl, _ := bookClient.GetBookList(context.Background(), &amp;amp;book.BookListParams{Page: 1, Limit: 10})<br \/>\n23&nbsp; fmt.Println(&amp;quot;\u83b7\u53d6\u4e66\u7c4d\u5217\u8868:\\t&amp;quot;)<br \/>\n24&nbsp; for _, b := range bl.BookList {<br \/>\n25&nbsp; &nbsp; &nbsp; fmt.Println(&amp;quot;bookId:&amp;quot;, b.BookId, &amp;quot; =&amp;gt; &amp;quot;, &amp;quot;bookName:&amp;quot;, b.BookName)<br \/>\n26&nbsp; }<br \/>\n27}<br \/>\n28<br \/>\n29<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<h3>5\u3001\u8fd0\u884c<\/h3>\n<p>\u9996\u5148\u542f\u52a8Server\u7aef\uff0c\u5904\u4e8e\u76d1\u542c\u963b\u585e\u72b6\u6001\u3002\u518d\u8fd0\u884cclient\u7aef\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/aqzt.com\/wp-content\/uploads\/20220222102146-23.png\" alt=\"\" \/><\/p>\n<h3>6\u3001\u8bf4\u660e<\/h3>\n<p>go-kit\u62bd\u8c61\u7684endpoint\uff1a<\/p>\n<p>\u4e00\u4e2aTransport\u7684Server \u5fc5\u987b\u8981\u62e5\u6709endPoint\u3001decodeRequestFunc\u3001encodeResponseFunc<\/p>\n<p>1\u3001 endPoint\u4e00\u4e2a\u7aef\u70b9\u4ee3\u8868\u4e00\u4e2aRPC\uff0c\u4e5f\u5c31\u662f\u6211\u4eec\u670d\u52a1\u63a5\u53e3\u4e2d\u7684\u4e00\u4e2a\u51fd\u6570<\/p>\n<p>2\u3001 decodeRequestFunc \u8bf7\u6c42\u53c2\u6570\u89e3\u7801\u51fd\u6570<\/p>\n<p>3\u3001 encodeResponseFunc \u8fd4\u56de\u53c2\u6570\u7f16\u7801\u51fd\u6570<\/p>\n<p><strong>\u8bf7\u6c42\u6d41\u7a0b\uff1a<\/strong>\u00a0<\/p>\n<p>\u8bf7\u6c42-&gt;encodeResponseFunc -&gt; endPoint -&gt; decodeRequestFunc -&gt; \u8fd4\u56de\u8f93\u51fa<\/p>\n<p><strong>\u6ce8\uff1a\u5b89\u88c5go-kit\u73af\u5883\u4f9d\u8d56\u5185\u5bb9\u6bd4\u8f83\u591a\uff0c\u6709\u7684\u65f6\u5019\u56e0\u4e3a\u7f51\u7edc\u95ee\u9898\uff08\u4e00\u822c\u8981\u8bbf\u95ee\u5916\u7f51\uff09\uff0c\u56e0\u6b64\u9700\u8981\u5229\u7528GitHub\u4e0b\u8f7d\u4e4b\u540e\u79fb\u52a8\u5230\u5bf9\u5e94\u7684gopath\u4e2d\uff08\u5177\u4f53\u95ee\u9898\u53ea\u80fd\u7ed3\u5408\u81ea\u5df1\u7684\u7ecf\u9a8c\u4e0e\u767e\u5ea6\u4e86 O(\u2229_\u2229)O\uff09\u3002<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>go-kit\u00a0\u662f\u4e00\u4e2a\u5fae\u670d\u52a1\u7684\u5f00\u53d1\u5de5\u5177\u96c6\uff0c\u5fae\u670d\u52a1\u7cfb\u7edf\u4e2d\u7684\u5927\u591a\u6570\u5e38\u89c1\u95ee\u9898\uff0c\u56e0\u6b64\uff0c\u4f7f\u7528\u8005\u53ef\u4ee5\u5c06\u7cbe\u529b\u96c6\u4e2d\u5728\u4e1a\u52a1\u903b\u8f91\u4e0a\u3002 grpc\u7f3a\u4e4f\u670d\u52a1\u6cbb\u7406\u7684\u529f\u80fd\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7go-kit\u7ed3\u5408grpc\u6765\u5b9e\u73b0\u6211\u4eec\u7684\u5b8c\u6574\u9700\u6c42\u3002go-kit\u62bd\u8c61\u7684endpoint\u8bbe\u8ba1\u8ba9\u6211\u4eec\u53ef\u4ee5\u5f88\u5bb9\u6613\u5305\u88c5\u5176\u5b83\u5fae\u670d\u52a1\u6846\u67b6\u4f7f\u7528\u7684\u534f\u8bae\u3002 endpoint\u4e3aServers\u548cClients\u63d0\u4f9b\u4e86\u57fa\u4e8eRPC\u65b9\u6cd5\u7684\u6784\u5efa\u6a21\u5757\u3002 \u5148\u6765\u770b\u770bendpoint\u6e90\u7801\uff1a 1234567891011121314151617181920212223242526272829303132333435<\/p>\n","protected":false},"author":1,"featured_media":24708,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"footnotes":""},"categories":[2],"tags":[292,348,390,26,25,21,10,500,190],"collection":[],"_links":{"self":[{"href":"https:\/\/aqzt.com\/wp-json\/wp\/v2\/posts\/24710"}],"collection":[{"href":"https:\/\/aqzt.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aqzt.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aqzt.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/aqzt.com\/wp-json\/wp\/v2\/comments?post=24710"}],"version-history":[{"count":0,"href":"https:\/\/aqzt.com\/wp-json\/wp\/v2\/posts\/24710\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/aqzt.com\/wp-json\/wp\/v2\/media\/24708"}],"wp:attachment":[{"href":"https:\/\/aqzt.com\/wp-json\/wp\/v2\/media?parent=24710"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aqzt.com\/wp-json\/wp\/v2\/categories?post=24710"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aqzt.com\/wp-json\/wp\/v2\/tags?post=24710"},{"taxonomy":"collection","embeddable":true,"href":"https:\/\/aqzt.com\/wp-json\/wp\/v2\/collection?post=24710"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}