kksanguo 2020-02-05 12:55 采纳率: 0%
浏览 2968
已结题

微服务网关访问Controller报404异常

微服务通过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

  • 写回答

1条回答

  • threenewbee 2020-02-05 14:17
    关注
    评论

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集