spring cloud 2.x版本 Eureka Server服务注册中心教程

本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 java

1.建立服务注册中心

1.1 新建Spring boot工程:eureka-server

1.2 pom.xml所需依赖jar包

<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>
复制代码

1.3 EurekaServerApplication添加注解@EnableEurekaServer

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}
复制代码

@EnableEurekaServer:启用eureka server相关配置git

1.4 添加配置文件内容:application.yml

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

1.5 启动服务

打开浏览器输入:http://localhost:8701, 显示以下:web

红框内容表明尚未实例注册spring

结语

至此,一个简单的单机服务注册中心就搭建完成了。浏览器

搭建服务注册中心集群

为了保证服务的高可用,咱们须要把单机应用改为集群应用,接下来,咱们建立一个简单的eureka server集群.app

1.1 修改本地host

  • 127.0.0.1 eureka1.server.com
  • 127.0.0.1 eureka2.server.com
  • 127.0.0.1 eureka3.server.com

1.2 增长application.yml配置文件

增长application-server1.yml和application-server2.yml文件,同时修改原来的application.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 
复制代码
  • application-server1.yml
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
复制代码
  • application-server2.yml
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
复制代码

1.3 在idea中添加启动服务

设置启动服务,按照截图中1234顺序依次添加ide

分别建立eureka-server2和eureka-server3.(注:eureka-server1用原来的启动就能够)

1.4 按照顺序分别启动3个eureka server服务

启动服务后分别访问 eureka1.server.com:8701eureka1.server.com:8702eureka1.server.com:8703 如图显示:spring-boot

三个页面如上图显示就表明服务所有启动成功。至此,一个简单的服务中心集群就搭建完成。

总结

能够将application-server1.yml和application-server2.yml的配置信息都放到原application.yml配置中,经过‘---’ 三横杠模加spring.profiles模式来启动,同时增长启动参数: --spring.profiles.active=config-server1。本文采用过个application.yml的方式,方便之后的维护。

本文只是简单的搭建了服务注册中心的单机和集群应用,对eureka作为服务注册中心有一个简单对认识,后续会更新eureka的其余内容。

代码仓库

gitHub地址


《Srping Cloud 2.X小白教程》目录

转载请注明出处,

  • 联系方式:4272231@163.com
相关文章
相关标签/搜索