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

微服务网关访问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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog