SpringCloud注册中心Eureka
springcloud常用的5大组件之一,服务发现–Netfix Eureka
微服务的本质是让服务与服务之间进行相互调用,不同的服务之间如何知道其他服务,这就要借用服务的发现eureka;eureka分为server端和client端,默认端口是8761;
一、单注册中心配置
1、eureka的server端
- 引入pom文件
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
- 启动类上添加注解(@EnableEurekaServer)
@SpringBootApplication
@EnableEurekaServer
public class RegistryServiceApplication {
public static void main(String[] args) {
SpringApplication.run(RegistryServiceApplication.class,args);
}
}
- yml文件配置
eureka:
instance:
prefer-ip-address: true
hostname: ${spring.cloud.client.ip-address}
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
client:
fetch-registry: false
register-with-eureka: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
2、eureka的client端
- 引入pom文件
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
- 启动类上加注解(@EnableDiscoveryClient或者
@EnableEurekaClient)
@SpringBootApplication
@EnableDiscoveryClient
public class MasterServiceApplication {
public static void main(String[] args) {
SpringApplication.run(MasterServiceApplication.class, args);
}
}
- yml文件配置
eureka:
instance:
prefer-ip-address: true
hostname: ${spring.cloud.client.ip-address}
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
client:
service-url:
defaultZone: http://${eureka.instance.hostname}:8761/eureka/
3、eureka client的注解说明
eureka的client可以用@EnableDiscoveryClient或者@EnableEurekaClient来注解;区别在于@EnableEurekaClient只能用于注册中心是eureka-server的时候,而@EnableDiscoveryClient可以用于注册中心是其他的server(如:zookeeper等);相比而言,还是用@EnableDiscoveryClient注解会更方便一些。@EnableDiscoveryClient注解与eureka组合运行可能会因为版本原因报错,需要加上如下的pom配置文件:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
为了保证注册中心的稳定运行,一般会存在多个注册中心,两个及两个以上的注册中心,相互注册配置。
二、双注册中心配置
1、eureka的server端
双注册中心与单注册中心的服务在server的区别是,双注册中心启动两个eureka的server端,两个server端指向不同的端口,差别主要体现在yml中
- yml文件配置
server:
port: 8761
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8762/eureka/
register-with-eureka: false
server:
enable-self-preservation: false
server:
port: 8762
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8761/eureka/
register-with-eureka: false
server:
enable-self-preservation: false
2、eureka的client端
client客户端要与两个eureka的server端都建立联系,才能保证一个eureka-server宕掉后不影响client使用,主要体现也是在yml文件中
- yml配置
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8761/eureka/,http://127.0.0.1:8762/eureka/
最后,三种及三种以上的eureka注册中心与两种的类似,server端相互配置来保证稳定性,client端配置所有的eureka-server。
springcloud常用的其他组件更新中…