With the popularity of microservice architecture, API gateway, as an indispensable part of microservice architecture, is increasingly valued by enterprises. The API gateway mainly acts as an external facade, provides an entry point, and is responsible for a series of functions such as protocol conversion, distributed load balancing, security authentication, and API management. In many application scenarios, API gateway not only solves many problems at the technical level, but also achieves benefits such as rapid iteration and flexible expansion at the business level.
This article will introduce how to use Go language to build a Kong-based API gateway, including using Kong to manage APIs and Plugins, and using Go language and Kong API to operate Kong configuration.
1. Introduction to Kong
Kong is an open source API gateway that provides a complete series of API management and security functions and can run in a variety of environments. It mainly provides management tools such as load balancing, service discovery, routing, authentication, authorization, current limiting, monitoring, and plug-ins. Kong uses Nginx as a proxy server and uses the plug-in mechanism to add various advanced functions to HTTP traffic. Kong's plug-ins can be enabled as needed, so that Kong can meet various specific business scenarios and needs, so it is also very popular.
2. Use Kong to manage APIs and Plugins
You can choose different environments for installation through the installation guide on the Kong official website. Here is Ubuntu as an example:
$ echo "deb https://kong.bintray.com/kong-deb `lsb_release -sc` main" | sudo tee -a /etc/apt/sources.list $ curl -o bintray.key https://bintray.com/user/downloadSubjectPublicKey?username=bintray $ sudo apt-key add bintray.key $ sudo apt-get update && sudo apt-get install -y kong
After successful installation, the Kong configuration file will be generated in the /etc/kong
directory.
$ kong start
Use Kong's Admin API to manage APIs.
$ curl -i -X POST --url http://localhost:8001/apis/ --data 'name=helloworld' --data 'uris=/hello' --data 'upstream_url=http://example.com/hello'
The above example creates an API named helloworld through the Admin API, the routing URI is /hello, and the upstream server address is http://example.com/hello. In this way, as long as you access http://localhost :8000/hello, you can access http://example.com/hello.
Using Kong can easily add Plugins, further enhancing the capabilities of the API.
$ curl -i -X POST --url http://localhost:8001/apis/helloworld/plugins/ --data 'name=key-auth'
The above example adds a plug-in named key-auth to the helloworld API through the Admin API. This plugin is used for authentication based on API Key.
3. Use Go language and Kong API to operate Kong configuration
In addition to using the Admin API to manage APIs and Plugins, you can also use the RESTful API provided by Kong for more fine-grained control. It is completely It conforms to Kong's internal data model and is more convenient than the Admin API.
Here we will use Go language and combine it with Kong Client SDK to operate Kong configuration.
$ go get github.com/Kong/go-kong/kong
conf := kong.Configuration{ KongAdminURL: "http://localhost:8001", // Kong Admin接口地址 KongURL: "http://localhost:8000", // Kong代理服务地址 }
api := &kong.Api{ Name: kong.String("helloworld"), RequestHost: kong.String("example.com"), UpstreamURL: kong.String("http://example.com/hello"), } route := &kong.Route{ Name: kong.String("hello-route"), Paths: kong.StringSlice([]string{"/hello"}), Methods: kong.StringSlice([]string{"GET"}), }
After creation, you can use the following code to associate them:
service, err := kong.CreateServiceWithDefaults(conf, api) // 用默认配置创建Service if err != nil { panic(err) } route, err = kong.CreateRouteWithDefaults(conf, route, service) if err != nil { panic(err) }
In this way, a world named helloworld is created, the routing URI is /hello, and the upstream server address is http:// example.com/hello, Route accessed using GET request.
Adding Plugin is also very simple. Here we add a Token-based Authentication plug-in for user authentication. The code is as follows:
plugin := &kong.Plugin{ Name: kong.String("jwt"), Config: kong.Configuration{ "anonymous": "true", }, } route.Plugins = []*kong.Plugin{plugin} _, err = kong.UpdateRouteWithDefaults(conf, route, service) if err != nil { panic(err) }
Here we use the Plugin name as jwt, so we can add JWT authentication. Here we use anonymous authentication.
Summary
This article introduces the process of using Go language and Kong Client SDK to build an API gateway. By using Kong, you can quickly build an API gateway, manage APIs and plug-ins, and implement functions such as authentication and current limiting. Kong can be further controlled and managed using the Go language and Kong Client SDK. These tools can improve the scalability and management of API gateways, allowing microservices to better serve the business.
The above is the detailed content of Build API gateway using Go language. For more information, please follow other related articles on the PHP Chinese website!