Zuul访问不到product服务

来源:8-2 Zuul:路由转发,排除和自定义

刘大大112233

2019-04-07

我按照视频配置Zuul后可以通过Zuul路由访问config服务和order服务。但是通过http://localhost:8089/product/product/list访问不到product服务,返回405状态码,且Api-gateway控制台不打印任何信息。
通过http://localhost:8090/product/list直接访问product服务正常。
浏览器访问product服务结果截图:
通过zuul路由访问结果
直接访问product服务结果
访问config服务结果

Api-gateway启动类:

package top.liuliyong.apigateway;

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

@SpringBootApplication
@EnableZuulProxy
public class ApiGatewayApplication {

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

}

Api-gateway的bootstrap.yml:

spring:
  application:
    name: api-gateway
  cloud:
    config:
      discovery:
        enabled: true
        service-id: CONFIG
      profile: dev
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
# 下面的配置是我自己加的,如果不加访问/config/order-test.yml时会报错
# com.netflix.zuul.exception.ZuulException: Forwarding error
# Caused by: com.netflix.hystrix.exception.HystrixRuntimeException: config timed-out and no fallback available.
# Caused by: java.util.concurrent.TimeoutException: null
ribbon:
  ReadTimeout: 60000
  ConnectTimeout: 60000
  eureka:
  enabled: true
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 60000

Api-gateway的pom文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.M3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>top.liuliyong</groupId>
    <artifactId>api-gateway</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>api-gateway</name>
    <description>Demo for Zuul</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.M2</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

</project>

Eureka注册服务信息:
Eureka

写回答

2回答

廖师兄

2019-04-11

代码没问题啊,是不是你的端口搞混了?//img.mukewang.com/szimg/5caebdbc00013c7d18841202.jpg

//img.mukewang.com/szimg/5caebd9e0001af9117781172.jpg

//img.mukewang.com/szimg/5caebe9e00012f0728660952.jpg

另外,以后你给代码,最好在项目里写好端口,方便我马上可以运行,我理清你服务使用的端口花了不少时间。。。

server:
  port: 8089
0
3
刘大大112233
回复
廖师兄
好的,应该是我的环境问题,我这边把docker idea啥的全给卸了重装又能跑了,谢谢您
2019-04-15
共3条回复

廖师兄

2019-04-07

把所有代码都push到git,发我链接,我帮你看看。程序里把端口配置好

1
3
刘大大112233
回复
廖师兄
师兄我搞好了,新地址是https://gitee.com/liuliyong/projects,springCloudMicroService开头的是我上传的代码
2019-04-09
共3条回复

SpringCloud Finchley(M2+RELEASE+SR2)微服务实战

SpringCloud组件实现微服务,【已升级Finchley.Release】

5668 学习 · 2489 问题

查看课程