本文共 7813 字,大约阅读时间需要 26 分钟。
官网地址:
Eureka2.0已经停止更新了。Spring Cloud提供了多种注册中心的支持,如:Eureka、Consul、ZooKeeper、Nacos等。Eureka已经闭源了。所以不建议使用了,目前推荐使用阿里提供的Nacos注册中心。
原理:
首先创建一个主Maven工程,在其pom文件引入依赖。这个pom文件作为父pom文件,起到依赖版本控制的作用,其他module工程继承该pom。
快速构建SpringBoot微服务工程。
父pom依赖:
4.0.0 pom eureka-client eureka-server org.springframework.boot spring-boot-starter-parent 2.0.3.RELEASE com.yq springcloud-eureka 0.0.1-SNAPSHOT springcloud-eureka UTF-8 UTF-8 1.8 Finchley.RELEASE org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-devtools runtime true org.springframework.boot spring-boot-configuration-processor true org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin
创建一个model工程作为服务注册中心,即eureka-server
。
com.yq springcloud-eureka 0.0.1-SNAPSHOT eureka-server org.springframework.cloud spring-cloud-starter-netflix-eureka-server
启动一个服务注册中心,只需要在SpringBoot启动类上加上注解@EnableEurekaServer即可。
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication// 申明这是一个Eureka服务@EnableEurekaServerpublic class EurekaServerApplication { }
配置文件appication.yml:
spring: application: name: app-eureka-centerserver: port: 8100eureka: instance: prefer-ip-address: true ip-address: 127.0.0.1 client: service-url: defaultZone: http://${ eureka.instance.ip-address}:${ server.port}/eureka/ register-with-eureka: false fetch-registry: false server: enable-self-preservation: false
spring.application.name
:注册到eureka注册中心的服务名称eureka.client.service-url.defaultZon
:服务注册中心地址eureka.client.register-with-eureka
:是否将自己注册到Eureka服务注册中心,默认为true(单机模式为false,集群模式为true)eureka.client.fetch-registry
:是否从Eureka中检索服务信息,默认为true(单机的注册中心为false,集群的注册中心或客户端为true)eureka.instance.prefer-ip-address
:是否将自己的IP地址注册到Eureka服务中,默认为falseeureka.instance.ip-address
:自定义IP地址,默认是本机地址eureka.server.enable-self-preservation
:是否开启自我保护机制,默认true,测试环境、开发环境可以设置false关闭自我保护机制,保证服务不可用时及时剔除创建一个model工程作为eureka-client
。
springcloud-eureka com.yq 0.0.1-SNAPSHOT eureka-client org.springframework.cloud spring-cloud-starter-netflix-eureka-client
通过注解@EnableEurekaClient
或者@EnableDiscoveryClient
开启服务注册和发现功能。
@EnableEurekaClient
只兼容Eureka。 @EnableDiscoveryClient
兼容所有注册中心,比如:Zookeeper,Consul。 建议还是使用@EnableDiscoveryClient
,方便以后切换注册中心。 package com.yq.eurekaclient;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@SpringBootApplication// 申明这是一个Eureka客户端@EnableEurekaClient@RestControllerpublic class EurekaClientApplication { public static void main(String[] args) { SpringApplication.run(EurekaClientApplication.class, args); } @Value("${server.port}") String port; @GetMapping("/hello") public String home() { return "hello port:" + port; }}
配置文件中注明自己的服务注册中心的地址。
需要指明spring.application.name
,这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个name
。 application.yml配置文件如下: server: port: 8081spring: application: name: eureka-clienteureka: client: serviceUrl: defaultZone: http://localhost:8100/eureka/ instance: prefer-ip-address: true
打开http://localhost:8100
,即eureka server的网址:
搭建Eureka集群非常简单,只要启动多个Eureka Server服务并且让这些Server端之间彼此进行注册即可实现。
更改eureka-server
的配置文件application.yml
, 在该配置文件中采用多 profile
的格式。
---spring: profiles: server1 application: name: app-eureka-centerserver: port: 8100eureka: instance: prefer-ip-address: true ip-address: 192.168.112.187 client: service-url: # 端口为8100的机器注册到端口为9100的注册中心 defaultZone: http://127.0.0.1:9100/eureka/ server: enable-self-preservation: false---spring: profiles: server2 application: name: app-eureka-centerserver: port: 9100eureka: instance: prefer-ip-address: true ip-address: 192.168.112.187 client: service-url: # 端口为9100的机器注册到端口为8100的注册中心 defaultZone: http://127.0.0.1:8100/eureka server: enable-self-preservation: false
通过 spring.profiles.active
指定启动的配置文件。
服务注册到Eureka集群时,可以指定多个,也可以指定一个Eureka服务(因为Eureka服务集群间彼此互联)。
如图,表示当前Eureka进入了自我保护模式。(先开启Eureka server端和client端,然后再断开client端,此时刷新Eureka界面,就会看到红色字样)
在默认情况下, Eureka Server 的自我保护模式是开启的,如果需要关闭,则在配置文件中添加:
eureka: server: enable-self-preservation: false #禁用自我保护模式
如图表示禁用自我保护模式:
转载地址:http://jgdhb.baihongyu.com/