当前位置: 首页 > 知识库问答 >
问题:

Spring云网关+领事配置

卜季萌
2023-03-14
buildscript {
    ext {
        springBootVersion = '2.1.2.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.demo'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
    maven { url 'https://repo.spring.io/milestone' }
}

ext {
    set('springCloudVersion', 'Greenwich.RELEASE')
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.cloud:spring-cloud-starter-consul-config'
    implementation 'org.springframework.cloud:spring-cloud-starter-consul-discovery'
    implementation 'org.springframework.cloud:spring-cloud-starter-gateway'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    // https://mvnrepository.com/artifact/io.netty/netty-tcnative-boringssl-static
    compile group: 'io.netty', name: 'netty-tcnative-boringssl-static', version: '2.0.20.Final'
    runtimeOnly 'org.springframework.boot:spring-boot-devtools'
    compileOnly 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}
server:
  port: 10000
  http:
    port: 9000
  # enable HTTP2
  http2:
    enabled: true
  # enable compression
  compression:
    enabled: true
    mime-types: text/html,text/xml,text/plain,text/css,text/javascript,application/javascript,application/json
  ssl:
    enabled: true
    key-store: /var/.conf/self-signed.p12
    key-store-type: PKCS12
    key-store-password: "something"
    key-alias: athenasowl
    trust-store: /var/.conf/self-signe.p12
    trust-store-password: "something"
spring:
  application:
    name: api-gateway
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
          predicates:
          - Path="'/api/' + serviceId + '/**'"
          filters:
          - RewritePath="'/api/' + serviceId + '/(?<remaining>.*)'", "serviceId + '/${remaining}'"
management:
  security:
    enabled: false
  server:
    port: 10001
    ssl:
      enabled: false
  endpoint:
    gateway:
      enabled: true
  endpoints:
    web:
      exposure:
        include: "*"
    health:
      sensitive: false
logging:
  level:
    root: DEBUG
    org:
      springframework:
        web: INFO
  pattern:
    console: "%-5level %d{dd-MM-yyyy HH:mm:ss,SSS} [%F:%L] VTC : %msg%n"
    file: "%-5level %d{dd-MM-yyyy HH:mm:ss,SSS} [%F:%L] VTC : %msg%n"
  file: /tmp/log_files/apigateway.log
security:
  basic:
    enabled: false
    null

编辑1:在@SpencerGibb的帮助下,我们用HTTPS设置了spring云网关。但我们还面临一些额外的问题

  • 如果在API网关和服务都启用了HTTPS,我们收到以下错误

javax.net.ssl.sslexception:在io.netty.handler.ssl.sslhandler.handshake(...)处握手超时(未知源)~[netyhandler-4.1.31.final.jar:4.1.31.

>

  • 如果HTTPS仅在API网关上启用,我们收到以下错误

    无法连接

    路径http://localhost:8080/hello-service/hello/message

    请找到示例应用程序的链接

      null
      predicates:
        - name: Path
          args:
            pattern: "'/api/'+serviceId.toLowerCase()+'/**'"
      filters:
        - name: RewritePath
          args:
            regexp: "'/api/' + serviceId.toLowerCase() + '/(?<remaining>.*)'"
            replacement: "'/${remaining}'"
    
  • 共有1个答案

    陆昂然
    2023-03-14

    需要一些东西

    1. 禁用HTTP2
    2. 禁用HttpClient的ssl配置
    3. 更新定位器谓词和筛选器以使用详细配置。

    下面是application.yml的结果部分

    server:
      port: 8443
      http:
        port: 8080
      servlet:
      # enable HTTP2
    #  http2:
    #    enabled: true
      # enable compression
    
    # ... removed for brevity
    
    spring:
      application:
        name: api-gateway
      cloud:
        consul:
          enabled: true
        gateway:
    #      httpclient:
    #        ssl:
    #          handshake-timeout-millis: 10000
    #          close-notify-flush-timeout-millis: 3000
    #          close-notify-read-timeout-millis: 0
    #      routes:
          # - id: ui_path_route
          #   predicates:
          #   - Path="'/**'"
          #   filters:
          #   - RewritePath="'/**'", "/ui"
          discovery:
            instanceId: ${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}}
            locator:
              enabled: true
              predicates:
              - name: Path
                args:
                  pattern: "'/api/' + serviceId + '/**'"
              filters:
              - name: RewritePath
                args:
                  regexp: "'/api/' + serviceId + '/(?<remaining>.*)'"
                  replacement: "'/${remaining}'"
    #... removed for brevity
    
     类似资料:
    • 我尝试将spring cloud gateway配置为转发到'/Consul/ui'或仅'/Consul'下的HashiCorp Consult的标准ui。然而,我不知道如何正确地做到这一点。Consul正在标准配置中运行。我在10.2.0.2上有一个外部ip地址和一个本地网络接口。我试过: 但是curl-l 127.0.0.1/consul/ui给了我一个500,因为根据日志,网关不匹配/con

    • 我不知道spring-cloud-gateway是否支持从领事注册中心读取路由,就像Zuul一样。 另外,我用--debug启动了这一行:

    • 我有一个带有一些endpoint的anexo API,比如: 如何将Spring Cloud Gateway与这些endpoint一起使用?

    • 我在单独的服务器上运行Spring Cloud gateway,配置如下: 但每次我从React应用程序中得到: 访问位于'http://11.1.1.1:8080/api/support/tickets/create'从原点'http://localhost:3000“”已被CORS策略阻止:对飞行前请求的响应未通过访问控制检查:请求的资源上不存在“access control Allow Or

    • 我为所有传入请求创建了全局路由,并使用AbstracTerrorWebExceptionHandler。 我的application.yml 运行应用程序时的日志: 谢谢你的帮助。

    • 我使用的是堆栈下 SpringCloudGatewayHoxton发行版,Java1.8,Linux操作系统 我看到下面的异常在重启后消失,但在一段时间后再次出现。我没有为直接内存做任何显式设置。下面是使用的JVM参数- 有人能告诉我错误的原因吗?