本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 java
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
复制代码
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
复制代码
@EnableEurekaServer:启用eureka server相关配置git
spring:
application:
name: eureka-server
server:
port: 8701
#无参数启动
eureka:
instance:
hostname: localhost
prefer-ip-address: true
client:
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
register-with-eureka: false #默认为true,设置为false,仅做为服务中心,不做为服务客户端
fetch-registry: false #默认为true,设置为false, 不从服务中心检索注册的服务
server:
eviction-interval-timer-in-ms: 5000 #清理间隔(单位毫秒, 默认是60*1000)
enable-self-preservation: true #默认为true,设置为false,关闭自我保护
#eureka server: 在运行期间会去统计心跳失败比例在15分钟以内是否低于85%
renewal-percent-threshold: 0.49 #默认0.85
复制代码
单机模式下: register-with-eureka和fetch-registry应为false,不然启动会报错:Cannot execute request on any known server。缘由,在默认设置下,eureka服务注册中心会将本身做为客户端来尝试注册本身。github
打开浏览器输入:http://localhost:8701, 显示以下:web
红框内容表明尚未实例注册spring
至此,一个简单的单机服务注册中心就搭建完成了。浏览器
为了保证服务的高可用,咱们须要把单机应用改为集群应用,接下来,咱们建立一个简单的eureka server集群.app
增长application-server1.yml和application-server2.yml文件,同时修改原来的application.yml文件,文件内容以下:分布式
spring:
application:
name: eureka-server
server:
port: 8701
#无参数启动
eureka:
instance:
hostname: eureka1.server.com
prefer-ip-address: true
client:
service-url:
defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/
register-with-eureka: false
fetch-registry: true
server:
eviction-interval-timer-in-ms: 5000
enable-self-preservation: true
renewal-percent-threshold: 0.49
复制代码
spring:
application:
name: eureka-server
server:
port: 8702
#无参数启动
eureka:
instance:
hostname: eureka2.server.com
client:
service-url:
defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/
register-with-eureka: false
fetch-registry: true
server:
eviction-interval-timer-in-ms: 5000
enable-self-preservation: true
renewal-percent-threshold: 0.49
复制代码
spring:
application:
name: eureka-server
server:
port: 8703
#无参数启动
eureka:
instance:
hostname: eureka3.server.com
client:
service-url:
defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/
register-with-eureka: false
fetch-registry: true
server:
eviction-interval-timer-in-ms: 5000
enable-self-preservation: true
renewal-percent-threshold: 0.49 #默认0.85
复制代码
设置启动服务,按照截图中1234顺序依次添加ide
启动服务后分别访问 eureka1.server.com:8701,eureka1.server.com:8702,eureka1.server.com:8703 如图显示:spring-boot
三个页面如上图显示就表明服务所有启动成功。至此,一个简单的服务中心集群就搭建完成。
能够将application-server1.yml和application-server2.yml的配置信息都放到原application.yml配置中,经过‘---’ 三横杠模加spring.profiles模式来启动,同时增长启动参数: --spring.profiles.active=config-server1。本文采用过个application.yml的方式,方便之后的维护。
本文只是简单的搭建了服务注册中心的单机和集群应用,对eureka作为服务注册中心有一个简单对认识,后续会更新eureka的其余内容。
转载请注明出处,