Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的。SpringCloud将它集成在其子项目spring-cloud-netflix中,以实现SpringCloud的服务发现功能。
Eureka包含两个组件:Eureka Server和Eureka Client。
简单使用
server
@SpringBootApplication
@EnableEurekaServer
public class SpringcloudServer8080Application {
public static void main(String[] args) {
SpringApplication.run(SpringcloudServer8080Application.class, args);
}
}
server:
port: 8080
spring:
application:
name: goodtype-server
eureka:
server:
enable-self-preservation: false # 关闭自我保护模式
eviction-interval-timer-in-ms: 1000 # 扫描失效服务的间隔时间(缺省为 60*1000ms)
instance:
hostname: localhost #eureka服务端的实例名称
instance-id: goodtype-server-8080 #对应status的name
prefer-ip-address: true
client:
register-with-eureka: false # 是否向注册中心注册自己
fetch-registry: false #为 false 表示自己是注册中心
service-url: #注册地址
#单机版本
#defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
#集群
defaultZone: http://com.luo1:7001/eureka/,http://com.luo2:7002/eureka/
provider
@SpringBootApplication
//@MapperScan("com.luo.springcloudprovider8001.mapper")
@EnableEurekaClient
//@EnableDiscoveryClient //服务发现
public class SpringcloudProvider8001Application {
public static void main(String[] args) {
SpringApplication.run(SpringcloudProvider8001Application.class, args);
}
}
server:
port: 8001
mybatis:
mapper-locations: classpath:mapper/*.xml
spring:
application:
name: goodstype-provider
datasource:
username: root
password: root
url: jdbc:mysql://127.0.0.1/xsj?serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
eureka:
instance:
lease-renewal-interval-in-seconds: 5 # 心跳,服务续约(renew)的间隔,默认为 30 秒
lease-expiration-duration-in-seconds: 10 # 服务失效时间,默认值 90 秒
prefer-ip-address: true # 当调用 getHostname 获取实例的 hostname 时,返回ip 而不是 host 名称
client:
fetch-registry: true # 是否拉取服务注册信息
register-with-eureka: true # 向服务中心注册自己
registry-fetch-interval-seconds: 30 #client间隔多久去拉取服务器注册信息,默认为30秒
service-url: # 注册地址
defaultZone: http://127.0.0.1:8080/eureka/,http://com.luo1:7001/eureka/,http://com.luo2:7002/eureka/
info:
app.name: ljk.cloud
company.name: kk
搭建集群
server1
server:
port: 8080
spring:
application:
name: goodtype-server
eureka:
server:
enable-self-preservation: false # 关闭自我保护模式
eviction-interval-timer-in-ms: 1000 # 扫描失效服务的间隔时间(缺省为 60*1000ms)
instance:
hostname: localhost #eureka服务端的实例名称
instance-id: goodtype-server-8080 #对应status的name
prefer-ip-address: true
client:
register-with-eureka: false # 是否向注册中心注册自己
fetch-registry: false #为 false 表示自己是注册中心
service-url: #注册地址
#集群
defaultZone: http://com.luo1:7001/eureka/,http://com.luo2:7002/eureka/
server2
server:
port: 7001
spring:
application:
name: goodtype-server-7001
eureka:
instance:
hostname: com.luo1 #eureka服务端的实例名称
instance-id: goodtype-server-7001 #对应status的name
client:
register-with-eureka: false # 是否向注册中心注册自己
fetch-registry: false #为 false 表示自己是注册中心
service-url: #注册地址
defaultZone: http://127.0.0.1:8080/eureka/,http://com.luo2:7002/eureka/
server3
server:
port: 7002
spring:
application:
name: goodtype-server-7002
eureka:
instance:
hostname: com.luo1 #eureka服务端的实例名称
instance-id: goodtype-server-7002 #对应status的name
client:
register-with-eureka: false # 是否向注册中心注册自己
fetch-registry: false #为 false 表示自己是注册中心
service-url: #注册地址
defaultZone: http://127.0.0.1:8080/eureka/,http://com.luo2:7001/eureka/