https://github.com/taofen8/kong-client
fast integrate spring projects to kong api gateway
https://github.com/taofen8/kong-client
gateway java kong kong-client microservices sdk-java
Last synced: 16 days ago
JSON representation
fast integrate spring projects to kong api gateway
- Host: GitHub
- URL: https://github.com/taofen8/kong-client
- Owner: taofen8
- License: apache-2.0
- Created: 2020-04-22T07:59:59.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2022-12-16T02:28:24.000Z (over 3 years ago)
- Last Synced: 2025-07-13T22:47:03.143Z (10 months ago)
- Topics: gateway, java, kong, kong-client, microservices, sdk-java
- Language: Java
- Homepage:
- Size: 116 KB
- Stars: 53
- Watchers: 15
- Forks: 13
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-java - Kong Client
README
EN | 中文
# Kong-client - A light weight java sdk for fast integrating with kong

kong-client is a java sdk for kong ,which is a high performance and extensible microservice
API gateway
# Prerequsites
- Spring 3.2.5+
- JDK 1.7+
- Kong 2.0.1+
# Features
- Fast Integration with springMVC:Suitable for all spring web app and work with less config
- Mutiple strategies for service proxy
- Register services automatically
- Service Call API:Generic API for firing request
- Load balancer
- Built-in Monitor: sdk version, server status and metrics etc.
# Terminology
### Proxy strategy
- #### kongmapping
By this strategy, kong-client will scan all services those annotationed by `KongServiceMapping`,and register them to kong server.
Note that, these services must be designed to single paramter for request and response,and the parameter must be a java type instance which can be formatted to JSON. An possible service would be like this:
```
@KongServiceMapping(path = "/sayHello")
HelloResponse sayHello(HelloRequest request){
}
```
- #### springmapping
This strategy is very suitable for existing springMVC projects which are wanted to be integrated to kong,and use spring's `RequestMapping` annotation on every service.
The strategy will take advantage of all spring features,like intercepters,view resolvers,and complex request handlers etc. kong-client will only be acted as a request proxy and do nothing invasive in request process.
# Usage
### Config
#### server-side
```
# kong admin url, required
# example: http://192.168.0.1:8001
kong.config.server.admin.url
# unique id for app, required
kong.config.server.app.identifier
# hosts can be accessed for services in this app, optional
kong.config.server.route.hosts
# proxy strategy, required
# value: kongmapping / springmapping
kong.config.server.proxy.strategy
# healthcheck config, required
kong.config.server.healthcheck.config
# auto resolve ip from net interfaces ,default :on
kong.config.server.address.resolve
```
These config items would be specified if current app want to provide services to kong.
Note that,if a dispatcher were customered in spring context,
the config `kong.config.server.proxy.strategy` will be ignored.
#### client-side
```
# kong nodes for load balancer, required
# format : single node :192.168.0.1:8000 , multiple nodes: 192.168.0.1:8000 w:200,192.168.0.2:8000 w:100
kong.config.caller.balancer.nodes
# switch of key-auth plugin
# value: on /off , defualt: off
kong.config.caller.keyauth.enable
# keyname of key-auth plugin if the plugin enabled,
# default: apikey
kong.config.caller.keyauth.keyname
```
These config items would be specifed if current app will call kong's services.
Note that, if the app is only be a consumer of services but not a provider when `kong.config.caller.keyauth.enable` is enabled, `kong.config.server.admin.url` and `kong.config.server.app.identifier
` also must be specified in order to register consumers automatically
### Development
* import maven dependency
```
com.taofen8.mid
kong-client
0.2.2-RELEASE
```
* setup servlet
- Springboot App
Customize a class to extends `com.taofen8.mid.kong.KongDispatcherServlet`
```$xslt
@WebServlet(urlPatterns = "/*", loadOnStartup = 1)
public class KongProxyDispatcherServlet extends KongDispatcherServlet {
}
```
and,add annotation `@ServletComponentScan` to app entrypoint class:
```
@ServletComponentScan
@SpringBootApplication
public class KongTestApplication {
public static void main(String[] args) {
SpringApplication.run(KongTestApplication.class, args);
}
}
```
- SpringMVC App
Modify `web.xml`, change `org.springframework.web.servlet.DispatcherServlet` to `com.taofen8.mid.kong.KongDispatcherServlet`:
```$xslt
kong
com.taofen8.mid.kong.KongDispatcherServlet
1
kong
/
```