本地demo案例通过swagger访问api调用无返回结果

demo案例application.yml:

spring:
  application:
    name: test-service
  datasource:
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: root
server:
  port: 9999
mybatis:
  mapperLocations: classpath*:/mapper/*.xml
  configuration:
    mapUnderscoreToCamelCase: true
management:
  port: 8081
  security:
    enabled: true
feign:
  hystrix:
    enabled: true
security:
  basic:
    enabled: false
  ignored: /v2/api-docs
eureka:
    instance:
      preferIpAddress: true
      leaseRenewalIntervalInSeconds: 1
      leaseExpirationDurationInSeconds: 3
    client:
      serviceUrl:
        defaultZone: ${EUREKA_DEFAULT_ZONE:http://localhost:8000/eureka/}

docker-compose.yml配置:

version: "3"
services:
  zookeeper-0:
    container_name: zookeeper-0
    image: registry.saas.hand-china.com/tools/zookeeper:3.4.10
    hostname: zookeeper-0
    environment:
    - ZK_REPLICAS=1
    - ZK_HEAP_SIZE=2G
    - ZK_TICK_TIME=2000
    - ZK_INIT_LIMIT=10
    - ZK_SYNC_LIMIT=5
    - ZK_MAX_CLIENT_CNXNS=60
    - ZK_SNAP_RETAIN_COUNT=3
    - ZK_PURGE_INTERVAL=1
    - ZK_LOG_LEVEL=INFO
    - ZK_CLIENT_PORT=2181
    - ZK_SERVER_PORT=2888
    - ZK_ELECTION_PORT=3888
    ports:
    - "2181:2181"
    - "2888:2888"
    - "3888:3888"
    command:
    - sh
    - -c
    - zkGenConfig.sh && exec zkServer.sh start-foreground
    volumes:
    - "/home/guoqingdeng/Desktop/docker-share/kafka/zk:/var/lib/zookeeper"
  kafka-0:
    container_name: kafka-0
    image: registry.saas.hand-china.com/tools/kafka:1.0.0
    hostname: kafka-0
    depends_on:
    - zookeeper-0
    links:
    - zookeeper-0
    ports:
    - "9092:9092"
    command:
    - sh
    - -c
    - "/opt/kafka/bin/kafka-server-start.sh config/server.properties \
           --override zookeeper.connect=zookeeper-0:2181 \
           --override log.dirs=/opt/kafka/data/logs \
           --override broker.id=0 "
    volumes:
    - "/home/guoqingdeng/Desktop/docker-share/kafka/kafka:/opt/kafka/data"
  redis:
    container_name: redis
    image: hub.c.163.com/library/redis:latest
    ports:
      - "6379:6379"
  mysql:
    container_name: mysql
    image: registry.cn-hangzhou.aliyuncs.com/choerodon-tools/mysql:5.7.17
    hostname: mysql
    ports:
    - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: root
    volumes:
      - /home/guoqingdeng/Desktop/docker-share/mysql/mysql_data:/var/lib/mysql
      - /home/guoqingdeng/Desktop/docker-share/mysql/mysql_db.cnf:/etc/mysql/conf.d/mysql_db.cnf
    expose:
    - "3306"
  phpadmin:
    container_name: phpadmin
    image: registry.saas.hand-china.com/tools/phpmyadmin
    ports:
      - "8888:80" # 80端口方便浏览器直接访问
    environment:
      PMA_ARBITRARY: 1 # 用于开启phpadmin关于可否输入host的设置
  eureka-server:
    container_name: eureka-server
    hostname: eureka-server
    image: registry.cn-shanghai.aliyuncs.com/choerodon/eureka-server:0.5.0
    ports:
    - "8000:8000"
    links:
    - kafka-0
    environment:
    - spring.kafka.bootstrap-servers=kafka-0:9092
    - eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8000/eureka/
    - eureka.client.register-with-eureka=false
    - eureka.client.fetch-registry=false
    - hystrix.stream.queue.enabled=false
    - spring.cloud.bus.enabled=false
    - spring.sleuth.stream.enabled=false
    expose:
    - "8000"
  api-gateway:
    container_name: api-gateway
    image: registry.cn-shanghai.aliyuncs.com/choerodon/api-gateway:0.5.0
    links: 
    - eureka-server
    depends_on:
    - eureka-server
    ports:
    - "8080:8080"
    environment:
    - eureka.client.serviceUrl.defaultZone=http://eureka-server:8000/eureka/
    - hystrix.stream.queue.enabled=false
    - spring.cloud.bus.enabled=false
    - spring.sleuth.stream.enabled=false
    expose:
    - "8080"
  gateway-helper:
    container_name: gateway-helper
    image: registry.cn-shanghai.aliyuncs.com/choerodon/gateway-helper:0.5.0
    depends_on:
    - eureka-server
    - mysql
    links: 
    - eureka-server
    - mysql
    ports:
    - "9180:9180"
    environment:
    - eureka.client.serviceUrl.defaultZone=http://eureka-server:8000/eureka/
    - spring.datasource.url=jdbc:mysql://mysql/iam_service?useUnicode=true&characterEncoding=utf-8&useSSL=false
    - spring.datasource.username=root
    - spring.datasource.password=root
    - hystrix.stream.queue.enabled=false
    - spring.cloud.bus.enabled=false
    - spring.sleuth.stream.enabled=false
  iam-service:
    container_name: iam-service
    image: registry.cn-shanghai.aliyuncs.com/choerodon/iam-service:0.5.0
    depends_on:
    - eureka-server
    - mysql
    - kafka-0
    links: 
    - eureka-server
    - mysql
    - kafka-0
    ports:
    - "8030:8030"
    environment:
    - eureka.client.serviceUrl.defaultZone=http://eureka-server:8000/eureka/
    - spring.kafka.bootstrap-servers=kafka-0:9092
    - spring.datasource.url=jdbc:mysql://mysql/iam_service?useUnicode=true&characterEncoding=utf-8&useSSL=false
    - spring.datasource.username=root
    - spring.datasource.password=root
    - hystrix.stream.queue.enabled=false
    - spring.cloud.bus.enabled=false
    - spring.sleuth.stream.enabled=false
  manager-service:
    container_name: manager-service
    image: registry.cn-shanghai.aliyuncs.com/choerodon/manager-service:0.5.0
    depends_on:
    - eureka-server
    - mysql
    - kafka-0
    links: 
    - eureka-server
    - mysql
    - kafka-0
    ports:
    - "8963:8963"
    environment:
    - spring.kafka.bootstrap-servers=kafka-0:9092
    - eureka.client.serviceUrl.defaultZone=http://eureka-server:8000/eureka/
    - spring.datasource.url=jdbc:mysql://mysql/manager_service?useUnicode=true&characterEncoding=utf-8&useSSL=false
    - spring.datasource.username=root
    - spring.datasource.password=root
    - hystrix.stream.queue.enabled=false
    - spring.cloud.bus.enabled=false
    - spring.sleuth.stream.enabled=false
  oauth-server:
    container_name: oauth-server
    image: registry.cn-shanghai.aliyuncs.com/choerodon/oauth-server:0.5.0
    depends_on:
    - eureka-server
    - mysql
    links: 
    - eureka-server
    - mysql
    ports:
    - "8020:8020"
    environment:
    - eureka.client.serviceUrl.defaultZone=http://eureka-server:8000/eureka/
    - spring.datasource.username=root
    - spring.datasource.url=jdbc:mysql://mysql/iam_service?useUnicode=true&characterEncoding=utf-8&useSSL=false
    - spring.datasource.password=root
    - hystrix.stream.queue.enabled=false
    - spring.cloud.bus.enabled=false
    - spring.sleuth.stream.enabled=false

本地环境系统为Linux,首先docker-compose启动相关组件容器,此处正常,然后idea中启动demo案例代码,此处基本正常,访问http://localhost:8963/swagger-ui.html通过swagger方式查看demo相关服务及api,如下:


如上图,此处应该也是正常的

相关操作步骤:

  • swagger中调用查询所有用户的api接口,已认证过,结果如下图,无数据返回

    这个api接口我经过其他方式测试是可以拿到数据的,而且我的数据库表也是有数据的。

上述的问题点应该在于网关api-gateway没正确调用到demo案例的服务资源接口,麻烦相关研发人员给瞅瞅,看看我这里是哪有问题,如果是配置的问题,请正确贴出来哈,谢谢!

看起来好像并没有什么问题,postman 直接测接口有没有返回?

可以看下docker logs看下api-gateway的日志,应该是api-gateway没有配置这个demo服务的路由造成的,可以在docker-compose修改api-gateway添加环境变量设置一下你的demo服务的路由,添加规则可以参照下面:

zuul:
  routes:
    test:
      path: /test/**
      serviceId: test-service

postman测试的话,采用basic auth认证,提示jwt token的问题。。。

postman测试需要添加Authorization头部,token 的值可以在swagger调接口时拿到

因此api-gateway添加环境变量zuul.routes.test.path=xxx和zuul.routes.test.serviceId=xxx,试下

ok,好的,我试试哈

docker-compose.yml添加了配置,如下:

试了下,结果还是不行。。。

跟demo案例中的这个有没有关系?

这个路由本地不会生效,只有线上通过config-server时会生效。去除运行主类的@EnableChoerodonResourceServer,直接调用test服务接口能通不?能通的话docker logs看api-gateway和gateway-helper日志

去掉后,还是通过swagger调,api-gateway无相关日志,gateway-helper有,如下:

gateway-helper | 2018-06-14 14:32:03.706 INFO [gateway-helper,4de96db462f2cce6,4de96db462f2cce6,false] 1 --- [ XNIO-3 task-5] i.c.g.h.p.RequestPermissionFilter : error.permissionVerifier.permission, can't find request service route, request uri /test/v1/user, zuulRoutes {event=ZuulProperties.ZuulRoute(id=null, path=/event/**, serviceId=event-store-service, url=null, stripPrefix=true, retryable=null, sensitiveHeaders=[], customSensitiveHeaders=false), devops=ZuulProperties.ZuulRoute(id=null, path=/devops/**, serviceId=devops-service, url=null, stripPrefix=true, retryable=null, sensitiveHeaders=[], customSensitiveHeaders=false), iam=ZuulProperties.ZuulRoute(id=null, path=/iam/**, serviceId=iam-service, url=null, stripPrefix=true, retryable=null, sensitiveHeaders=[], customSensitiveHeaders=false), oauth=ZuulProperties.ZuulRoute(id=null, path=/oauth/**, serviceId=oauth-server, url=null, stripPrefix=false, retryable=null, sensitiveHeaders=[], customSensitiveHeaders=true), notify=ZuulProperties.ZuulRoute(id=null, path=/notify/**, serviceId=notification-service, url=null, stripPrefix=true, retryable=null, sensitiveHeaders=[], customSensitiveHeaders=false), manager=ZuulProperties.ZuulRoute(id=null, path=/manager/**, serviceId=manager-service, url=null, stripPrefix=true, retryable=null, sensitiveHeaders=[], customSensitiveHeaders=false), file=ZuulProperties.ZuulRoute(id=null, path=/file/**, serviceId=file-service, url=null, stripPrefix=true, retryable=null, sensitiveHeaders=[], customSensitiveHeaders=false), org=ZuulProperties.ZuulRoute(id=null, path=/org/**, serviceId=organization-service, url=null, stripPrefix=true, retryable=null, sensitiveHeaders=[], customSensitiveHeaders=false)}

通过postman,直接访问,是可以的

那个swagger的问题咋个解决哈。。。

不过如果采用直接访问的话,那请求就不会经过api-gateway和gateway-helper这边了,这样的话。。。

所以说swagger这个问题还是得解决哈,不然集成这玩意有啥用呀。。。

找到原因了,文档的demo案例代码中确实一个注解,如下图:

你们这个文档,编写的人员要用点心呀,整出来的东西,缺胳膊少腿的,简直是坑死人不偿命是吧:joy::joy::joy:

不好意思,微服务开发后端文档最好参照开发手册-后端开发手册-开发demo程序的例子,快速入门代码文档不全,文档会尽快更新,谢谢🙏