SpringCloud eureka+config+gateway整合多模块开发

项目git链接: https://github.com/kfluo/demo-cloud

引言

此项目是基于 Spring Cloud Finchley 的一个分布式系统套件的整合。

技术选型

  • 分布式系统套件版本:Spring Cloud Finchley
  • 分布式统一配置中心:Spring Cloud Config
  • 网关路由代理调用:Spring Cloud Gateway
  • 声明式服务调用:Spring Cloud OpenFeign

子项目介绍

项目结构

eureka配置文件

server:
  port: 8970
  servlet:
    context-path: ~
  tomcat:
    uri-encoding: UTF-8


# 服务注册
eureka:
  # 实例设置
  instance:
    # 实例主机名称
    hostname: 127.0.0.1
    # 实例是否允许使用IP
    preferIpAddress: false

  # 服务端设置
  server:
    # 关闭自我保护,将出现故障的服务快速剔除
    enableSelfPreservation: false
    # 清理无效节点的时间间隔,缺省 (1000*60)ms
    evictionIntervalTimerInMs: 6000
    # 当获取不到对应实例时,需要等待的时间,缺省 (1000*60*5)ms
    waitTimeInMsWhenSyncEmpty: 6000
    
  # 客户端设置
  client:
    # 由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己(注册中心集群时开启)
    registerWithEureka: false
    # 由于注册中心的职责就是维护服务实例,它不需要去检索服务,所以设置为false
    fetchRegistry: false
    # 注册中心地址(集群时指定另外一个注册中心地址)
    serviceUrl.defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

spring:
  # 应用程序名称
  application:
    name: luokf-cloud-eureka
  profiles:
    active: default
  
  # 关闭横幅
#  main:
#    bannerMode: "off"
    
# 日志配置
logging:
  custom:
    filepath: /usr/local/logs/${spring.application.name}
  config: classpath:logback-spring.xml

config配置

server:
  port: 8971
  servlet:
    context-path: ~
  tomcat:
    uri-encoding: UTF-8

# 服务注册
eureka:

  # 实例设置
  instance:
    # 实例主机名称
    hostname: 127.0.0.1
    # 实例是否允许使用IP
    preferIpAddress: false
    # 实例状态监控参数
    statusPageUrlPath: ${server.servlet.context-path}/project/default
    healthCheckUrlPath: ${server.servlet.context-path}/actuator/health

  # 客户端设置
  client:
    # 注册中心地址(集群时指定另外一个注册中心地址)
    serviceUrl:
      defaultZone: http://127.0.0.1:8970/eureka/

spring:

  # 应用程序名称
  application:
    name: luokf-cloud-config

  # 当前环境名称
  profiles:
    active: native
  
  # 分布式配置中心
  cloud:
    config:
      server:
      
        # 使用本地配置文件
        native:
          searchLocations: classpath:cloud-config
          
        # 使用Git储存配置文件
#        git:
#          uri: https://github.com/kfluo/demo-cloud-config.git
#          username: ~
#          password: ~
    
    # Consul 服务发现
    consul:
      host: 127.0.0.1
      port: 8500
      discovery:
        hostname: 127.0.0.1
        preferIpAddress: false
        healthCheckPath: ${server.servlet.context-path}/actuator/health
        healthCheckInterval: 15s
          
  # 消息队列服务
#  rabbitmq:
#    host: 192.168.56.106
#    port: 5672
#    username: admin
#    password: admin123

  # 关闭横幅
  main:
    bannerMode: "off"
    
# 日志配置
logging:
  root: /usr/local/logs/${spring.application.name}
  config: classpath:logback-spring.xml

 

spring.cloud.config.server.native.search-locations指定本地配置文件

或者spring.cloud.config.server.git.uri指定远程配置文件

gateway配置

server:

  port: 8980
  servlet:
    context-path: ~
  tomcat:
    uri-encoding: UTF-8

# 服务注册
eureka:

  # 实例设置
  instance:
    # 实例主机名称
    hostname: 127.0.0.1
    # 实例是否允许使用IP
    preferIpAddress: false
    # 实例状态监控参数
    statusPageUrlPath: ${server.servlet.context-path}
    healthCheckUrlPath: ${server.servlet.context-path}/actuator/health

  # 客户端设置
  client:
    # 注册中心地址(集群时指定另外一个注册中心地址)
    serviceUrl.defaultZone: http://127.0.0.1:8970/eureka/

spring:
  cloud:
    # Consul 服务发现
    consul:
      host: 127.0.0.1
      port: 8500
      discovery:
        healthCheckPath: ${server.servlet.context-path}/actuator/health
        healthCheckInterval: 15s
      
    # 网关路由配置
    gateway:
      discovery:
        locator:
          enabled: false
          lowerCaseServiceId: true
      defaultFilters:
        - PreserveHostHeader
      routes:
        # 测试模块1
        - id: luokf-cloud-module-test1
          uri: lb://luokf-cloud-module-test1
          predicates:
            - Path=/demo/**
          filters:
            - StripPrefix=1
        # 测试模块2
        - id: luokf-cloud-module-test2
          uri: lb://luokf-cloud-module-test2
          predicates:
            - Path=/user/**
          filters:
            - StripPrefix=1

  
  # 应用程序名称
  application:
    name: luokf-cloud-gateway

  # 当前环境名称
  profiles:
    active: default
  
  # 打印横幅
  main:
    bannerMode: "off"

# 日志配置
logging:
  root: /usr/local/logs/${spring.application.name}
  config: classpath:logback-spring.xml

  • routes:路由规则列表
  • id:我们自定义的路由 ID,保持唯一
  • uri:目标服务地址
  • predicates:路由条件,Predicate 接受一个输入参数,返回一个布尔值结果。该接口包含多种默认方法来将 Predicate 组合成其他复杂的逻辑(比如:与,或,非)。
  • filters:过滤规则,

上面路由配置表示所有包含 /demo/ 的url都会被路由到luokf-cloud-module-test1服务,所有包含/user/的url都会被路由到luokf-cloud-module-test服务StripPrefix=1表示路由时会去除/demo/和/user/。

业务模块配置通过如下配置指定配置中心

  # 分布式配置中心
  cloud:
    config:
      discovery:
        enabled: true
        serviceId: luokf-cloud-config

运行

按顺序运行以下启动类的main方法:

EurekaApplication>ConfigApplication>GatewayApplication>CoreApplication>Test1Application>Test2Application

调用演示

  • 网关代理模块调用

http://localhost:8980/demo/test1/testData/getById?id=1

  • 模块之间调用

http://localhost:8980/user/test2/testData/getTestDataFromTest1?id=1

 

 

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值