This is the 21st day of my participation in Gwen Challenge

If ❤️ my article is helpful, welcome to like, follow. This is the biggest encouragement for me to continue my technical creation. Coderdao.github. IO /

Thrift builds the Golang request

Introduction to the

The thrift 0.11.0 version of the TCP proxy cannot be installed. The result is a bunch of code errors. The next best thing is to talk about thrift building golang requests

The environment

window 10

Install thrift

The thrift website window has a direct exe file; Linux/MAC requires compilation and installation

Thrift also provides historical versions if required, but you must make sure that installing thrift is consistent with the version of the thrift SDK that your program uses. Otherwise, the code generated by the thrift IDL generation command will fail because of an error

  • Thrift. Exe historical version
  • Thrift source code version

After the installation is successful, run the following command to check whether the installation is successful:

$/ d/Dev/env/thrift014 thrift014. Exe - version Thrift version 0.14.2Copy the code

Download thrift source code

git clone [email protected]:apache/thrift.git

# If github connection speed is slow, you can also use domestic image
git clone [email protected]:apache/thrift.git
Copy the code

If we open up the folder we can find various language versions of the code, the main one here is Golang, so we chooseGo folder

Unfortunately, I haven’t found a way to install the Go Module offline yet. So still using online installation go get git.apache.org/thrift.git install thrift

Environment set up

In the directory thrift/tutorial, you can find the model example as follows

1. In the $GOROOT/ SRC directory, create folder thrift_server_client and enable the go mod

$  mkdir $GOROOT/src/thrift_server_client
$  cd $GOROOT/src/thrift_server_client
$  go mod init thrift_server_client
Copy the code

2. Install thrift SDK

$  go get git.apache.org/thrift.git
Copy the code

3, copy,Thrift sourcedirectorythrift/tutorialThe fileshared.thrift,tutorial.thrift$GOROOT/src/thrift_server_clientUnder the file

4, run IDL generate command, generate the corresponding language server/client structure ‘thrift –gen [language] [code configuration file] ‘

$  /d/Dev/env/thrift014/thrift014.exe --gen go shared.thrift
$  /d/Dev/env/thrift014/thrift014.exe --gen go tutorial.thrift
Copy the code

You get the following code

5. If you read the generated code at this point, you’ll seetutorial.go.tutorial-consts.goThe existence ofsharedIdle can’t read the source code

Actually, Shared it points to is that we have just/d/Dev/env/thrift014 thrift014. Exe – gen go Shared. Thrift generated code. Replace the shared reference with the relative path thrift_server_client/gen-go/shared

Build the server and client

Let’s copy thrift/tutorial/go/ SRC to $GOROOT/ SRC /thrift_server_client

Just look at the main.go file

	// Whether the server is enabled
	server := flag.Bool("server".false."Run server")
	// Specify the request protocol
	protocol := flag.String("P"."binary"."Specify the protocol (binary, compact, json, simplejson)")
	// bind the listener address
	addr := flag.String("addr"."localhost:9090"."Address to listen to")
	// Whether to enable HTTPS
	secure := flag.Bool("secure".false."Use tls secure transport")
Copy the code
  • Server := flag.Bool(“server”,true, “Run Server “) open the server and Run it
$ go run main.go server.go client.go handler.go
*thrift.TServerSocket
Starting the simple server... on  localhost:9090
Copy the code
  • Bool(“server”,false, “Run server”), Run
$ go run main.go server.go client.go handler.go
ping()
1+1=2
Invalid operation: InvalidOperation({WhatOp:4 Why:Cannot divide by 0})
15-10=5
Check log: 5
Copy the code

This is what the server side will also accept to the client side, printing the content:

Starting the simple server... on  localhost:9090
ping()
add(1,1)
calculate(1, {DIVIDE,1,0})
calculate(1, {SUBTRACT,15,10})
getStruct(1)
Copy the code

How does the thrift example request end up here, and then things like long links and WSS can be modified based on that example