来到新公司后一直使用的框架是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/
注:若同时注册两个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是服务注册的名称