SpringBoot 工程集成 ProxyServer 作为内部代理服务器。
ProxyServer 支持在应用中打开另一个代理服务器,可以扩展应用的服务能力,支持服务代理第三方服务作为服务网关使用。
ProxyServer 支持代理:
- HTTP
- WebSocket
pom 导入依赖
xml
<dependency>
<groupId>com.yiidata.proxyserver</groupId>
<artifactId>proxyserver-core</artifactId>
<version>${proxyserver.version}</version>
<exclusions>
<exclusion>
<artifactId>fastjson</artifactId>
<groupId>com.alibaba</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.yiidata.proxyserver</groupId>
<artifactId>proxyserver-spring</artifactId>
<version>${proxyserver.version}</version>
<exclusions>
<exclusion>
<artifactId>fastjson</artifactId>
<groupId>com.alibaba</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring.boot.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
<!-- 移除 tomcat 容器,擦用 jetty servlet 容器 -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 添加 jetty 作为服务器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
<version>${spring.boot.version}</version>
</dependency>
开启 ProxyServer 服务
通过 @EnableProxyServer
注解即可开启 ProxyServer 的代理服务。
java
@Slf4j
@EnableProxyServer
@SpringBootApplication
public class ProxyServerApplication {
public static void main(String[] args) {
SpringApplication.run(ProxyServerApplication.class, args);
}
}
配置 ProxyServer
代理路由配置,可来自数据库,也可以来自配置文件,具体实现由 ProxyService
接口实现类决定。
java
@Slf4j
@Service
public class ProxyServiceConfigure implements ProxyService {
@Override
public String getProxyContextPath() {
String proxyPath = ...; // 代理的路径
return Optional.ofNullable(proxyPath).orElse("/proxy");
}
@Override
public List<ProxyLocation> listProxy() {
List<ProxyLocation> locations = new ArrayList<>();
// 代理路由
ProxyLocation location = new ProxyLocation();
location.setProxyType("http");
location.setEnableFlag("Y");
location.setLocation(path);
location.setProxyTarget(path);
location.setProxyPass(upStream);
locations.add(location);
log.info("add router {} to path: {}", path, upStream);
return locations;
}
开启多个路由,则从数据库或者配置文件中获取多个路由,locations 然后返回即可。
代理 websocket
开启 ProxyServer WebSocket 代理支持。
java
@EnableWebSocket
@Configuration
public class WebSocketConfig {
/**
* 注入ServerEndpointExporter,
* 这个bean会自动注册使用了@ServerEndpoint注解声明的Websocket endpoint
*/
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
@Bean
public WebSocketConfigurer webSocketConfigurer() {
final Supplier<HttpProxyServlet> supplier = () -> {
final ProxyContextHandler proxyContextHandler = SpringContextUtils.getBean(ProxyContextHandler.class);
return proxyContextHandler.getProxyServlet();
};
return (WebSocketHandlerRegistry registry) -> {
// 设置连接路径和处理
registry.addHandler(new SpringProxyWebSocketHandler(supplier), "/websockify/*")
.setAllowedOrigins("*");
};
}
}