SpringCloudGateway Nacos配置中心与注册中心,动态路由

SpringCloudGateway+Nacos 集成应用

1. 应用集成(配置中心+应用中心)

参见(SpringCloud+Nacos 配置中心与注册中心应用)

2. 启动服务

SpringCloudGateway Nacos配置中心与注册中心,动态路由_第1张图片

3. API文档访问
4. 动态路由

只需要修改对应的配置文件即可,nacos会发起刷新

附录
  • 网关配置
TIMEOUT.IN.MILLISECONDS: 60000
hystrix:
  command:
    fallbackcmd:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: ${
     TIMEOUT.IN.MILLISECONDS}
          strategy: SEMAPHORE
          semaphore:
            maxConcurrentRequests: 1000
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: ${
     TIMEOUT.IN.MILLISECONDS}
          strategy: SEMAPHORE
          semaphore:
            maxConcurrentRequests: 1000
spring:
  profiles: dev
# 链路追踪
  zipkin:
    # 可使用服务名称
    base-url: http://localhost:9411/
    # 没有注册到注册中心,不需要开启
    discovery-client-enabled: false
    service:
      name: ${
     spring.application.name}
    sender:
      type: web
  sleuth:
    sampler:
      probability: 1.0
  redis:
    password: ****
    sentinel:
      master: mymaster
      nodes: ip:26379,ip:26380
    lettuce:
      pool:
        max-active: 8
        max-idle: 8
        max-wait: -1ms
        min-idle: 0
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
          lower-case-service-id: true
      routes:
        - id: nacos-demo-test
          uri: lb://nacos-demo-test
          predicates:
          - Path= /api/t/**
          filters:
          - StripPrefix= 2
        - id: nacos-demo-local
          uri: lb://nacos-demo-local
          predicates:
          - Path= /api/local/**
          filters:
          - StripPrefix= 2
        - id: nacos-demo-non
          uri: lb://nacos-demo-non
          predicates:
          - Path= /api/non/**
          filters:
          - StripPrefix= 2


sonic:
  platform:
    mybatis:
      mapper-locations: classpath:/mappers/*Mapper.xml
    datasource:
      jdbc-url: jdbc:mysql://****:3306/zipkin?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false&allowMultiQueries=true
      username: ****
      password: ****
      driver-class: com.mysql.cj.jdbc.Driver
      min-idle: 5
      max-active: 10
      initial-size: 5
    gateway:
      services:
        nacos-demo-local: Nacos_Local服务
        nacos-demo-test: Nacos_Test服务
        nacos-demo-non: Nacos_不存在的服务
  • 应用中配置
# 此处为自定义,配置中心和注册中心共同属性,下文引入即可
nacos:
  group: demo
  # 配置中心地址
  server-addr: http://127.0.0.1:9948
  # 命名空间
  namespace: dev
service.port: 9999

spring:
  profiles:
    active: dev
  application:
    name: nacos-demo-gateway
  cloud:
    nacos:
      server-addr: ${
     nacos.server-addr}
      config:
      # nacos 配置中心服务地址 nginx 代理的地址
      # 默认  ${spring.cloud.nacos.server-addr}
      #  server-addr:
        file-extension: yaml
        prefix: ${
     spring.application.name}
      # dataID  ${prefix}-${spring.profiles.active}.${file-extension}
      # 命名空间 默认 public
        namespace: ${
     nacos.namespace}
      # 配置组  默认 DEFAULT_GROUP
        group: ${
     nacos.group}
      # 共享配置(公共配置)
        shared-configs:
          - data-id: nacos-demo.yaml
            # 可自定义
            group: ${
     nacos.group}
            refresh: true


你可能感兴趣的:(SpringCloud,gateway,springcloud,spring,boot,nacos,java)