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

微服务网关访问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
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式