微服务通过zuul网关访问controller报404,直接URL可以访问
网关配置如下:
eureka:
client:
registry-fetch-interval-seconds: 5
service-url:
defaultZone: http://localhost:8090/eureka
server:
port: 8092 #网关端口号
spring:
application:
name: zuul-service #网关服务名称
zuul:
prefix: /api
routes: #路径列表
zuulservicehello1:
path: /zuulservicehello1/**
service-id: HELLO1
strip-prefix: false
retryable: true
add-proxy-headers: false
sensitive-headers:
add-host-header: true
解析后的yml:
{ eureka:
{ client:
{ 'registry-fetch-interval-seconds': 5,
'service-url': { defaultZone: 'http://localhost:8090/eureka' } } },
server: { port: 8092 },
spring: { application: { name: 'zuul-service' } },
zuul:
{ prefix: '/api',
routes:
{ zuulservicehello1:
{ path: '/zuulservicehello1/**',
'service-id': 'HELLO1',
'strip-prefix': false } },
retryable: true,
'add-proxy-headers': false,
'sensitive-headers': null,
'add-host-header': true } }
服务配置:
eureka:
client:
registry-fetch-interval-seconds: 5
service-url:
defaultZone: http://localhost:8090/eureka
server:
port: 8095 #后台服务端口号
spring:
application:
name: HELLO1
解析后的yml
{ eureka:
{ client:
{ 'registry-fetch-interval-seconds': 5,
'service-url': { defaultZone: 'http://localhost:8090/eureka' } } },
server: { port: 8095 },
spring: { application: { name: 'HELLO1' } } }
服务程序代码:
package com.example.springboot.controller.Demo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController()
public class HelloWorld {
@RequestMapping("/hello")
public String Hello(){
return ("HelloSpringCloud");
}
}
注册中心配置
eureka:
client:
service-url:
defaultZone: http://localhost:8090/eureka/
register-with-eureka: false
fetch-registry: false
service:
enable-self-preservation: false
server:
port: 8090 #eureka注册中心端口号
通过URL直接访问服务程序可以访问成功,通过Zuul网关访问失败,SpringCloud初学者,求解惑!!!
补充说明路由信息是有的
原因出在strip-prefix的配置,改为true问题解决,用法参照:https://blog.csdn.net/u010953880/article/details/102977884