Spring Cloud整合Zuul实现简单的路由 出现zuul No route found for uri 问题:

错误代码示例:

启动类:(@EnableEurekaServe和 @EnableZuulProxy)

@EnableEurekaServe 注意需要加上 Zuul也是要注册到Eureka注册中心的

package com.wpresource.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@SpringBootApplication
@EnableEurekaServer
@EnableZuulProxy
public class ConsumerApplication {

	public static void main(String[] args) {
		SpringApplication.run(ConsumerApplication.class, args);
	}
}

properties.yml:

server:
    port: 8081
    application:
       name: consumer

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://sh-cdb-qgx7tksn.sql.tencentcdb.com:66666/soy
    username: root
    password: ssss
    type: com.alibaba.druid.pool.DruidDataSource


eureka:
    client:
        fetch-registry: true
        register-with-eureka: true
        service-url:
            defaultZone: http://xzz1:1111/eureka/,http://xzz1:2222/eureka/

zuul:
  routes:
    first:
      path: /zuul
      url: http://www.163.com/

上诉配置文件主要看 zuul配置 其他可以忽略;

运行代码不会得到想要的结果跳到163网址 信息如下:



如果遇到以上提示会错误,请参考下方解决办法:

调试时发现404错误:日志打印zuul No route found for uri:


跟踪代码发现/zuul的默认context-path是/zuul,它会裁剪请求的url

加上一个配置

zuul: 

     servlet-path=/

完整的zuul配置为:

zuul:
  servlet-path: /
  routes:
    first:
      path: /zuul
      url: http://www.163.com/

加上如上配置 问题得以解决!





Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐