irpas技术客

springcloud微服务--eureka_对象

网络投稿 1275

作为服务注册中心,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://·/wadmwz/p/10309304.html


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #1导入依赖amplt