我正在开发一个简单的spring引导应用程序,并尝试在集群中部署它。
控制器如下所示:
@Controller
@RequestMapping("/")
@RequiredArgsConstructor
public class UiController {
private final ScenarioRepository scenarioRepository;
@GetMapping
public String index() {
return "redirect:/scenarios";
}
@GetMapping("/debug")
public String debug(HttpServletRequest request) {
log.info("RequestURI: {}", request.getRequestURI());
log.info("Context path: {}", request.getContextPath());
log.info("Servlet path: {}", request.getSession());
log.info("Path translated: {}", request.getPathTranslated());
log.info("Path info: {}", request.getPathInfo());
return "redirect:/scenarios";
}
@GetMapping("/scenarios")
public String scenarios() {
List<Scenario> scenarios = scenarioRepository.findAll();
model.addAttribute("scenarios", scenarios);
return "scenarios";
}
}
入口配置:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: myservice
namespace: dev
labels:
app.kubernetes.io/name: myservice
app.kubernetes.io/instance: myservice
app.kubernetes.io/version: "1.0"
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
rules:
- host: dev.cluster
http:
paths:
- path: /apps/myservice/(.*)
backend:
serviceName: myservice
servicePort: http
没有额外的web/spring配置。
在这种配置下,我期望以下行为:
本地:请求 http://localhost/
重定向到 http://localhost/scenarios
群集:请求 http://dev.cluster/apps/myservice/
重定向到 http://dev.cluster/apps/myservice/scenarios
在我的ide中,它可以正常工作。但当我将它部署到k8s索引请求时,它会将我重定向到 http://dev.cluster/scenarios
缺少入口路径 /apps/myservice/
.
访问 /debug
在集群中生成以下输出:
12:43:21.338 Servlet path: org.apache.catalina.session.StandardSessionFacade@12da0eae
12:43:21.338 Path translated: null
12:43:21.338 Path info: null
12:43:21.236 RequestURI: /debug
12:43:21.236 Context path:
响应的位置标头: http://dev.cluster/scenarios
. 很明显,spring对入口重写一无所知,并生成非法的重定向url。
所以,问题是:如何解决这个配置问题?
暂无答案!
目前还没有任何答案,快来回答吧!