简要说明
路径选择器和路径映射器在SSS服务器中用于管理服务器的访问,确保服务器能够正确加载网页和处理请求,路径选择器决定了服务器访问本地服务器还是云服务器,路径映射器则将访问地址或请求映射到具体的路径。
配置路径选择器
路径选择器负责根据请求类型选择访问本地服务器还是云服务器,Spring Boot允许使用JNDI(Java净依赖插件)的PathSelector类来配置路径选择器。
配置步骤:
-
导入配置类:
import org.springframework.boot.config.PathSelector; import org.springframework.boot.config.PathMapper;
-
配置路径选择器:
- 添加路径选择器到配置中。
- 使用默认路径选择器:
PathSelector pathSelector = new PathSelector(); pathSelector.setName("ssd-pathSelector"); pathSelector.setPath("http://localhost"); pathSelector.setDefaultPath("http://example.com"); pathSelector.setRequiresStatic(true);
-
配置路径选择器属性:
name:路径选择器的名称。path:本地服务器的路径。defaultPath:云服务器的路径。requireStatic:是否需要静态字符串。staticRequired:是否需要静态字符串的唯一性。
配置路径映射器
路径映射器将访问地址或请求映射到具体的路径。
配置步骤:
-
导入路径映射器类:
import org.springframework.boot.config.RoutingMapper;
-
配置路径映射器:
RoutingMapper routingMapper = new RoutingMapper(); routingMapper.addParameter("request", "request"); routingMapper.addParameter("requestBody", "requestBody"); routingMapper.addParameter("server", "http://localhost"); routingMapper.addParameter("path", "/my-page"); routingMapper.addParameter("domain", "example.com"); routingMapper.addParameter("user", "user@example.com"); routingMapper.addParameter("password", "password"); routingMapper.setAutoRoute(true); routingMapper.setStaticName("http://localhost/my-page"); -
定义路由映射逻辑:
根据访问地址或请求生成路径。
组合路径选择器和路径映射器
路径选择器负责选择访问服务器,路径映射器将访问地址或请求映射到路径。
组合步骤:
-
组合配置:
PathSelector pathSelector = new PathSelector(); pathSelector.setName("ssd-pathSelector"); pathSelector.setPath("http://localhost"); pathSelector.setDefaultPath("http://example.com"); pathSelector.setRequiresStatic(true); pathSelector.setStaticRequired(true); RoutingMapper routingMapper = new RoutingMapper(); routingMapper.addParameter("request", "request"); routingMapper.addParameter("requestBody", "requestBody"); routingMapper.addParameter("server", "http://localhost"); routingMapper.addParameter("path", "/my-page"); routingMapper.addParameter("domain", "example.com"); routingMapper.addParameter("user", "user@example.com"); routingMapper.addParameter("password", "password"); routingMapper.setAutoRoute(true); routingMapper.setStaticName("http://localhost/my-page"); Configuration configuration = new Configuration(pathSelector, routingMapper);
配置SSS服务器
SSS服务器会使用配置文件生成正确的访问路径,配置文件通常名为ssd.config或类似名称。
配置步骤:
-
定义访问日志和请求:
Configuration configuration = new Configuration(); configuration.setAccessControl("ssd"); configuration.setAccessControlType("read-write"); configuration.setAccessControlGroups(new Group("ssd-group")); configuration.setPathSelector(pathSelector); configuration.setRoutingMapper(routingMapper); configuration.writeTo(StandardOutput.class); -
生成访问路径:
Configuration configuration = new Configuration(); configuration.setAccessControl("ssd"); configuration.setAccessControlType("read-write"); configuration.setAccessControlGroups(new Group("ssd-group")); configuration.setPathSelector(pathSelector); configuration.setRoutingMapper(routingMapper); configuration.writeTo(StandardOutput.class);
测试配置
-
运行配置文件:
spring-boot.config ssd.config
-
访问服务器:
http://localhost:88
-
查看访问日志:
curl -X GET http://localhost:88/223-1-1/12345
注意事项
- 路径选择器和路径映射器的作用是确保服务器正确加载网页和处理请求,因此它们需要与SSS服务器的配置相匹配。
- 路径格式化(如去掉前后空格)可以提高访问性能。
- 编码处理(如URL编码后的路径)确保访问服务器时能够正确解析请求。
通过以上步骤,可以确保在SSS服务器上正确选择和映射路径,从而优化服务器性能和响应速度。


