consul 整合 spring cloud gateway 无法访问其他服务

问题描述

使用consul 作为注册服务中心
spring cloud gateway 作为网关

clipboard.png
图中 gateway 和 user服务都已注册上了

单独调用user是可以访问
图片描述

通过spring cloud gateway 来访问报404

clipboard.png

问题出现的环境背景及自己尝试过哪些方法

相关代码

网关配置

spring:
  profiles:
    active: dev
  application:
    name: angelcloud-gateway
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
#      filter:
#        remove-non-proxy-headers:
#          headers:
#          - dummy
      routes:
      - id: baidu
        # 重点!/info必须使用http进行转发,lb代表从注册中心获取服务
        uri: http://www.baidu.com/
         # 重点!转发该路径!,/userapi/**,
        predicates:
        - Path=/baidu/**
      # - id: apiuser
        # 重点!/info必须使用http进行转发,lb代表从注册中心获取服务
        # uri: lb://ANGELCLOUD-PROVIDER-USER
         # 重点!转发该路径!,/userapi/**,
        # predicates:
        # - Path=/ANGELCLOUD-PROVIDER-USER/**
    consul:
      host: localhost
      port: 8500
      healthCheckInterval: 15s

server:
  port: 8080

# eureka:
#  client:
#    service-url:
#      defaultZone: http://localhost:8761/eureka/
用户服务配置
server:
  port: 8030

spring:
  profiles:
    active: dev
  application:
    # name: @pom.artifactId@
    name: angelcloud-provider-user
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://127.0.0.1:3306/angelcloud_master?characterEncoding=utf-8&useSSL=false
  cloud:
    consul:
      host: localhost
      port: 8500
      healthCheckInterval: 15s
mybatis-plus:
  typeAliasesPackage: com.angel.provider.model.domain
  mapper-locations: classpath:mapper/*.xml,classpath*:sdk/mapper/*.xml
  configuration:
    # 来开启驼峰功能
    map-underscore-to-camel-case: true
mapper:
  mappers: com.angel.provider.mapper.*
  not-empty: false
  identity: MYSQL

# pagehelper:
#  helperDialect: mysql
#  reasonable: true
#  support-methods-arguments: true
#  params: count=countSql


# eureka:
#  client:
#    service-url:
#      defaultZone: http://localhost:8761/eureka/
用户接口
package com.angel.provider.web.frontend;


import com.angel.provider.model.domain.SysUser;
import com.angel.provider.service.ISysUserService;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 系统用户表(后台管理) 前端控制器
 * </p>
 *
 * @author aa
 * @since 2018-07-30
 */
@RestController
@RequestMapping("/sysUser")
public class SysUserController {

    @Resource
    private ISysUserService iSysUserService;

    @GetMapping(value = "selectall")
    public String selectAll () {
        Page<SysUser> page =new Page<SysUser>(0, 10);
        Page<SysUser> sysUserPage = iSysUserService.selectPage(page);
        List<SysUser> records = sysUserPage.getRecords();
        return records.toString();
    }

}

你期待的结果是什么?实际看到的错误信息又是什么?

求解 通过gateway访问报404

阅读 15.2k
4 个回答

解决了 另附上apigatway配置

spring:
  profiles:
    active: dev
  application:
    name: angelcloud-gateway
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
      routes:
        # 系统路由
      - id: angelcloud-provider-user
        # 重点!/info必须使用http进行转发,lb代表从注册中心获取服务
        uri: lb://angelcloud-provider-user
         # 重点!转发该路径!,/userapi/**,
        predicates:
        - Path= /userapi/**
        filters:
        - StripPrefix= 1
        # - RewritePath=/userapi/(?.*), /${segment}
      # - id: apiuser
        # 重点!/info必须使用http进行转发,lb代表从注册中心获取服务
        # uri: lb://ANGELCLOUD-PROVIDER-USER
         # 重点!转发该路径!,/userapi/**,
        # predicates:
        # - Path=/ANGELCLOUD-PROVIDER-USER/**

        # 博客路由
      - id: angelcloud-provider-blog
        uri: lb://angelcloud-provider-blog
        predicates:
        - Path= /blogapi/**
        filters:
        - StripPrefix= 1
    consul:
      host: localhost
      port: 8500
      healthCheckInterval: 15s

server:
  port: 8082
  
  

uri 后面的lb://${注册服务的名字}
predicates:

 - Path 转发路径 
    比如定义/testapi/**  则访问为 http://localhost:8082/testapi/服务下的所有接口 (端口为gateway的端口)     
   

filters:过滤规则

加上StripPrefix=1 去掉testapi,只保留**部分

新手上路,请多包涵

我这边已经可以访问了,原因是假如你有这个依赖spring-boot-starter-actuator 你的consul的配置healthCheckPath不能配置成healthCheckPath: ${management.context-path}/health,应该用healthCheckPath: /actuator/health,从consul ui页面看,个人猜测应该是你加入了spring-boot-starter-actuator,配置了healthCheckPath然后consul就会调用地址查询服务是否健康,但是这个检测健康的地址有问题,所以判定该服务不能被gateway获取。(只是个人看法,有错误请指出)

同问,我这是这么写的还是报404

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题