Layotto go-sdk Reference
Start from HelloWorld
go-sdk simple usage
# 1. Execute the following commands sequentially in the root directory of the project
# build a layotto binary program, and add it to the environment variables for easy subsequent operations
cd cmd/layotto
go build -o layotto
export PATH=$PATH:$(pwd)/layotto
# 2.Run layotto according to the configuration file
layotto start -c ../../configs/config_hello.json
# 3.Start another terminal and launch the go sdk client program
cd demo/hello/common
go build -o client
./client -s helloworld
## output:
runtime client initializing for: 127.0.0.1:34904
greeting
The go-sdk code
Briefly observe the usage process of client
, and the source code is placed in demo/hello/common/client.go
var storeName string
func init() {
flag.StringVar(&storeName, "s", "", "set `storeName`")
}
func main() {
flag.Parse()
if storeName == "" {
panic("storeName is empty.")
}
// Create a connection using the default IP port 127.0.0.1:34904, which is also the default startup port for local Layotto
cli, err := client.NewClient()
if err != nil {
log.Fatal(err)
}
// Close grpc connection
defer cli.Close()
// call the interface, send request
res, err := cli.SayHello(context.Background(), &client.SayHelloRequest{
ServiceName: storeName,
})
if err != nil {
log.Fatal(err)
}
fmt.Println(res.Hello)
}
Configuration files
Start Layotto and test the HelloWorld program above by using the simplest configuration file, the content is as follows:
For a more detailed introduction to configuration files, please refer to configuration.
{
"servers": [
{
"default_log_path": "stdout",
"default_log_level": "DEBUG",
"listeners": [
{
"name": "grpc",
"address": "127.0.0.1:34904",
"bind_port": true,
"filter_chains": [
{
"filters": [
{
"type": "grpc",
"config": {
"server_name": "runtime",
"grpc_config": {
"hellos": {
"helloworld": {
"type": "helloworld",
"hello": "greeting"
}
}
}
}
}
]
}
]
}
]
}
]
}
In the above process, Layotto registers components in the grpc_config
attribute in the configuration file.
The interface type of the component implementation is hellos
, and multiple instance instances can be supported to implement this interface.
The instance attribute in the configuration file is used to distinguish the instance names of different implementations of this interface.
In the above configuration file, only one instance helloworld
is involved.
When using go sdk to call hello related services, such as SayHello
in the program, the name of the instance needs to be specified.
The above code uses the ServiceName
parameter to pass the instance name, indicating that the specified instance is used to operate this request
There are two sub attributes in the instance attribute type
and hello
:
type
, this attribute is unique to every instance, indicating which type is used to implement the interface. Here, it is thehellowrold
type which is already been registered as a component type in Layottohello
, this attribute is used to initialize components, and different types of interfaces have different attribute fields. Different instance also have different attribute field to initialize. Use code to view the usage of relevant configuration.