博客
关于我
SpringCloud之服务注册Eureka及集群
阅读量:512 次
发布时间:2019-03-07

本文共 4101 字,大约阅读时间需要 13 分钟。

Spring Cloud之服务注册Eureka及集群配置说明

1. Eureka注册中心

1.1 Eureka服务器端的配置

要搭建一个Eureka注册中心,首先需要配置服务器端的pom文件和启动类。

1.1.1 pom文件依赖

在项目的pom文件中添加以下依赖:

org.springframework.cloud
spring-cloud-starter-eureka-server

完整的pom文件示例如下:

4.0.0
com.atguigu.springcloud
microservicecloud
0.0.1-SNAPSHOT
microservicecloud-eureka-7001
org.springframework.cloud
spring-cloud-starter-eureka-server
org.springframework
springloaded

1.1.2 YAML配置文件

Eureka服务器端的服务端配置文件(eureka.yml)内容如下:

server:    port: 7001eureka:    instance:        hostname: localhost        client:            register-with-eureka: false            fetch-registry: false        service-url:            defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

1.1.3 主启动类

启动服务器端的类使用Spring Boot注解:

@SpringBootApplication@EnableEurekaServerpublic class EurekaServer7001_App {    public static void main(String[] args) {        SpringApplication.run(EurekaServer7001_App.class, args);    }}

2. Eureka客户端的配置

2.1 微服务Provider的客户端配置

Provider服务需要注册到Eureka服务中。同样在项目的pom文件中添加以下依赖:

org.springframework.cloud
spring-cloud-starter-eureka

完整的pom文件示例如下:

4.0.0
com.atguigu.springcloud
microservicecloud
0.0.1-SNAPSHOT
microservicecloud-provider-dept-8001
com.atguigu.springcloud
microservicecloud-api
${project.version}
org.springframework.cloud
spring-cloud-starter-eureka

2.1.1 YAML配置文件

服务提供者的配置文件(application.yml)内容如下:

server:    port: 8001mybatis:    config-location: classpath:mybatis/mybatis.cfg.xml    type-aliases-package: com.atguigu.springcloud.entities    mapper-locations: classpath:mybatis/mapper/**/*.xmlspring:    application:        name: microservicecloud-dept    datasource:        type: com.alibaba.druid.pool.DruidDataSource        driver-class-name: org.gjt.mm.mysql.Driver        url: jdbc:mysql://localhost:3306/cloudDB01        username: root        password: 123456        dbcp2:            min-idle: 5            initial-size: 5            max-total: 5            max-wait-millis: 200eureka:    client:        service-url:            defaultZone: http://localhost:7001/eureka/    instance:        instance-id: microservicecloud-dept8001    prefer-ip-address: true    info:        app.name: atguigu-microservicecloud        company.name: www.atguigu.com        build.artifactId: ${project.artifactId}        build.version: ${project.version}

2.1.2 主启动类

启动服务的Provider类:

@SpringBootApplication@EnableEurekaClientpublic class DeptProvider8001_App {    public static void main(String[] args) {        SpringApplication.run(DeptProvider8001_App.class, args);    }}

3. Eureka集群配置

为了实现Eureka的集群,我们需要配置多个注册中心并确保各服务能够正确 discoveries。

3.1 ect配置

在Eureka服务器端的配置文件中添加服务的 listen地址:

server:    port: 7001eureka:    instance:        hostname: eureka7001.com    client:        service-url:            defaultZone: http://${eureka.instance.hostname}:7001/eureka/

对于其他注册中心(如eureka7002.com和eureka7003.com),配置方式类似。

3.2 客户端配置

在客户端的配置文件中指定多个 registering address:

server:    port: 80spring:    application:        name: microservicecloud-dept    eureka:        client:            service-url:                defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/

3.3 访问验证

可以通过访问http://eureka7001.com:7001/或其他节点地址查看注册信息,确保服务注册和发现正常。


通过以上配置,可以实现Eureka服务的注册中心搭建及集群部署,方便各服务之间的功能调用和负载均衡。

转载地址:http://skojz.baihongyu.com/

你可能感兴趣的文章
MySQL经常使用技巧
查看>>
mysql给账号授权相关功能 | 表、视图等
查看>>
MySQL缓存使用率超过80%的解决方法
查看>>
Mysql缓存调优的基本知识(附Demo)
查看>>
mysql网站打开慢问题排查&数据库优化
查看>>
mysql网络部分代码
查看>>
mysql联合索引的最左前缀匹配原则
查看>>
mysql自动化同步校验_Shell: 分享MySQL数据同步+主从复制自动化脚本_20190313_七侠镇莫尛貝...
查看>>
mysql自增id超大问题查询
查看>>
MySQL自带information_schema数据库使用
查看>>
MySQL获取分组后的TOP 1和TOP N记录
查看>>
mysql虚拟列表_动态网页制作-官方版合集下载-多特
查看>>
MySQL蜜罐反制获取攻击者信息
查看>>
Mysql表创建外键报错
查看>>
mysql表格调取数据库信息_MySQL™ 参考手册(获取有关数据库和表的信息)
查看>>
mysql表检查分析优化
查看>>
WARN: Establishing SSL connection without server‘s identity verification is not recommended.
查看>>
MySQL视图
查看>>
MySQL视图
查看>>
Mysql视图、触发器、事务、储存过程、函数
查看>>