作为服务注册中心,Eureka与Zookeeper好在哪里?
一个分布式系统不可能同时满足C(一致性)、A(可用性)、P(容错性)
ZooKeeper保障的是CP
Eureka保障的是AP
所以,Eureka可以很好应对因网络故障导致部分结点失去联系的情况,而不会像zookeeper那样使整个注册服务瘫痪。
1.导入依赖
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
2.编写配置文件
server:
port: 7001
eureka:
instance:
hostname: localhost #服务端的实例名称
client:
register-with-eureka: false #是否注册自己
fetch-registry: false #如果为false 则表示自己为注册中心,所以在客户端时为true
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
3.在启动类上开启Eureka
@SpringBootApplication
@EnableEurekaServer //开启 Eureka server,接受其他微服务的注册
public class MicroServiceCloudEureka7001Application {
public static void main(String[] args) {
SpringApplication.run(MicroServiceCloudEureka7001Application.class, args);
}
}
4.浏览器访问Eureka服务注册中心
http://localhost:7001/
客户端
1.导入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
2.配置文件
server:
port: 8001 #服务端口号
spring:
application:
name: microServiceCloudProviderDept #微服务名称,对外暴漏的微服务名称
datasource:
username: root #数据库登陆用户名
password: 123456 #数据库登陆密码
url: jdbc:mysql://127.0.0.1:3306/bianchengbang_jdbc #数据库url
driver-class-name: com.mysql.jdbc.Driver #数据库驱动
###################################### MyBatis 配置
mybatis:
# 指定 mapper.xml 的位置
mapper-locations: classpath:mybatis/mapper/*.xml
#扫描实体类的位置,在 mapper.xml 中就可以不写实体类的全路径名
type-aliases-package: net.biancheng.c.entity
config-location: classpath:mybatis/mybatis-config.xml
######################################### Spring cloud 自定义服务名称和 ip 地址
eureka:
client: #将客户端注册到 eureka 服务列表内
service-url:
defaultZone: http://localhost:7001/eureka/ #这个地址是 7001注册中心在 application.yml 中暴露出来额注册地址
instance:
instance-id: spring-cloud-provider-8001 #自定义服务名称信息,修改eureka上的默认描述信息
prefer-ip-address: true #显示访问路径的 ip 地址
相关文章
https://www.cnblogs.com/wadmwz/p/10309304.html