AndServer
v2.1.12
andServer是HTTP和反向代理服务器。
Android平台的Web服务器和Web框架。它提供了诸如SpringMVC之类的注释,如果您熟悉SpringMVC,则可以很快掌握它。
部署Web服务器:
Server server = AndServer . webServer ( context )
. port ( 8080 )
. timeout ( 10 , TimeUnit . SECONDS )
. build ();
// startup the server.
server . startup ();
...
// shutdown the server.
server . shutdown ();它还具有某些功能,例如inetAddress(InetAddress) , serverSocketFactory(ServerSocketFactory)和sslContext(SSLContext) ,具体取决于您想要实现的目标。
@ RestController
@ RequestMapping ( path = "/user" )
public class UserController {
@ PostMapping ( "/login" )
public String login ( @ RequestParam ( "account" ) String account ,
@ RequestParam ( "password" ) String password ) {
...
return "Successful." ;
}
@ GetMapping ( path = "/{userId}" )
public User info ( @ PathVariable ( "userId" ) String userId ,
@ QueryParam ( "fields" ) String fields ) {
User user = findUserById ( userId , fields );
...
return user ;
}
@ PutMapping ( path = "/{userId}" )
public void modify ( @ PathVariable ( "userId" ) String userId
@ RequestParam ( "age" ) int age ) {
...
}
}以上代码将生成以下两个HTTP API:
POST http://.../user/login
GET http://.../user/uid_001?fields=id,name,age
PUT http://.../user/uid_001
与客户获取连接信息:
@ GetMapping ( path = "/connection" )
void getConnection ( HttpRequest request , ...) {
request . getLocalAddr (); // HostAddress
request . getLocalName (); // HostName
request . getLocalPort (); // server's port
request . getRemoteAddr (); // HostAddress
request . getRemoteHost (); // Especially HostName, second HostAddress
request . getRemotePort (); // client's port
...
}有关文档和其他信息,请参见网站。
部署反向代理服务器:
Server server = AndServer . proxyServer ()
. addProxy ( "www.example1.com" , "http://192.167.1.11:8080" )
. addProxy ( "example2.com" , "https://192.167.1.12:9090" )
. addProxy ( "55.66.11.11" , "http://www.google.com" )
. addProxy ( "192.168.1.11" , "https://github.com:6666" )
. port ( 80 )
. timeout ( 10 , TimeUnit . SECONDS )
. build ();
// startup the server.
server . startup ();
...
// shutdown the server.
server . shutdown ();注意:这只是一个反向代理,没有能力照顾负载平衡。
将插件添加到您的项目构建脚本:
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath ' com.yanzhenjie.andserver:plugin:2.1.12 '
.. .
}
}
allprojects {
repositories {
google()
mavenCentral()
}
}
.. .然后将AndServer依赖性添加到您的模块:
apply plugin : ' com.yanzhenjie.andserver '
.. .
dependencies {
implementation ' com.yanzhenjie.andserver:api:2.1.12 '
annotationProcessor ' com.yanzhenjie.andserver:processor:2.1.12 '
.. .
}如果您使用的是Kotlin,请用kapt替换annotationProcessor 。
在提交拉动请求之前,贡献者必须遵守协议。
Copyright Zhenjie Yan
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.