Springcloud 公用discovery和config

来到新公司后一直使用的框架是springboot,和其相关springcloud,分布式服务用的是eureka.但是一直是在一台服务器上使用,在多个服务器上就需要启动多个discovery和config服务,这样会浪费很多内存.

Discovery

服务发现和注册,用于发现服务并将发现的服务注册进discovery中.

配置(yml文件):

application.yml

server:

  port: 8761

 

eureka:

  instance:

    hostname: 172.16.16.52 

  client:

    register-with-eureka: false

    fetch-registry: false

 

Server.port  : discovery服务的端口号,自定义

Eureka.instance.hostname : discovery服务的主机ip,可以填写localhost

Config

服务的配置,用于统一管理其他服务的一些配置,如数据库的连接信息

Application.yml

spring:

  cloud:

    config:

      server:

        native:

          search-locations: classpath:/config #1

 

server:

  port: 8888

search-locations : 查询配置文件的位置,classpath表示在此项目的根目录下

 

Bootstrap.yml

spring:

  application:

    name: config #1

  profiles:

    active: native #2

    

eureka:

  instance:

    non-secure-port: ${server.port:8888} #3

    metadata-map:

      instanceId: ${spring.application.name}:${random.value} #4

  client:

    service-url:

      defaultZone: http://172.16.16.52:8761/eureka/ #5

 

Spring.pplication.name : 当前应用的名称,其他服务需要从此项目中读取配置时需填写,经过discovery应用之后,注册名会转成大写字母

client.service-url.defaultZone: 客户端服务的默认域,填写的是discovery的ip地址和端口号以及组件名 (如:http://172.16.16.66:8761/eureka/)

 

注:若dicovery在本地的话,可以填写

http://${eureka.host:localhost}:${eureka.port:8761}/eureka/

194051_0Zei_2459446.png 

194055_11dl_2459446.png 

:若同时注册两个config服务,discovery会优先读取同一台服务器上的(物理就近原则)

其他服务的配置

Bootstrap.yml

spring:

  application:

    name: activity

  cloud:

    config:

      enabled: true

      discovery:

        enabled: true

        service-id: CONFIG

eureka:

  instance:

    non-secure-port: ${server.port:8096}

  client:

    service-url:

      defaultZone: http://172.16.16.66:8761/eureka/

 

cloud.config.enabled:表示是否需要从cloud中读取config,true表示需要,false表示不需要

cloud.config.discovery.service-id: 配置文件所在的服务的id,此id是服务注册的名称

转载于:https://my.oschina.net/wxdl/blog/802569

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值