SpringCloud(Finchley.SR2版本)踩坑笔记(二)-------Eureka

        Eureka:Spring Cloud 体系中最重要的组件之一,作用是服务的注册和发现。本文建立在上一篇文章《SpringCloud(Finchley.SR2版本)踩坑笔记(一)-------项目初建》之上。

一、配置并运行 Eureka 服务

1. pom 中引入依赖 spring-cloud-starter-netflix-eureka-server

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
</dependencies>

2. application.yml 中声明配置,Eureka Server 以 8080 端口启动。

spring:
    application:
        name: spring-cloud-eureka
server:
    port: 8080
# eureka 配置    
eureka:
    client:
        # 表示是否将自己注册到Eureka Server,默认为true。因为本服务本身即为 Eureka Server,所以为 false
        register-with-eureka: false
        # 表示是否从Eureka Server获取注册信息,默认为true。单例运行为false,集群时为true
        fetch-registry: false
        # 设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。
        serviceUrl:
            # 默认是http://localhost:8761/eureka ;多个地址可使用 , 分隔。
            defaultZone: http://localhost:8080/eureka/  

3. 主函数入口声明 @EnableEurekaServer

@SpringBootApplication
@EnableEurekaServer
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

然后启动程序,再浏览器中输入 http://localhost:8080 即可访问,8080端口可以替换为你自己项目的启动端口。

上述配置中,将 register-with-eureka 设置为 false 之后,在管理后台中  Instances currently registered with Eureka 就看不到这个服务本身了。

二、Eureka 集群

        Eureka 作为很核心的服务,如果这个服务本身挂了,那么对整个 Spring Cloud 服务体系造成的影响是巨大的,所以有必要考虑 Eureka 的集群来保证运行的稳定性。

        Eureka 服务集群,其实说白了很简单,就是多运行几个 Eureka 服务,并且将他们相互注册服务而已。关键就是用到上面配置中的 serviceUrl.defaultZone。

        先把三个域名 client1.com, client2.com, client3.com 三个域名配置到 hosts 文件中(仅仅为了测试,真实情况下,填写服务实际运行的地址即可),为了便于查看,我们开启 register-with-eureka = true

        比如我把上面的Eureka服务,分别按照 8080, 8081, 8082 三个端口,启动三个实例,那么他们的 defaultZone 就分配配置为:

spring:
    application:
        name: spring-cloud-eureka

# client1 配置
server:
    port: 8080
eureka:
    instance:
        hostname: client1
    client:
        serviceUrl:
            defaultZone: http://client2.com:8081/eureka/,http://client3.com:8083/eureka/            
    
# client2 配置
---
spring:
    profiles: client2
server:
    port: 8081
eureka:
    instance:
        hostname: client2
    client:
        serviceUrl:
            defaultZone: http://client1.com:8080/eureka/,http://client3.com:8083/eureka/

# client3 配置
---
spring:
    profiles: client3
server:
    port: 8083
eureka:
    instance:
        hostname: client3
    client:
        serviceUrl:
            defaultZone: http://client1.com:8080/eureka/,http://client2.com:8081/eureka/  
java -jar spring-cloud-1.0.0.jar
java -jar spring-cloud-1.0.0.jar --spring.profiles.active=client2
java -jar spring-cloud-1.0.0.jar --spring.profiles.active=client3

然后在任意一台的 Eureka 服务的管理页面中,都能看到另外两台 Eureka 服务。比如登录 http://client1.com:8080 就能看到 client2 和 client3 

 

 

 

项目说明 该项目是一个典型的由Spring Cloud管理的微服务项目,主要包括如下模块 micro-service-cloud─────────────────顶层项目 ├──cloud-service-core───────────────基础核心模块 ├──cloud-service-tools──────────────全局通用工具类 ├──cloud-service-reids──────────────Redis次封装 ├──cloud-eureka-server──────────────服务注册中心[8761] ├──cloud-turbine-server─────────────断路器聚合监控[8769] ├──cloud-zipkin-server──────────────链路追踪监控[9411] ├──cloud-zuul-server────────────────第一代服务网关(Zuul)[8080] ├──cloud-gateway-server─────────────第代服务网关(Gateway)[8080] ├──cloud-modules-app────────────────App微服务模块 ├───────modules-app-user────────────App用户服务模块[努力更新中] ├───────modules-app-doctor──────────App医生服务模块[努力更新中] ├──cloud-modules-service────────────微服务通用服务模块 ├───────mongodb-file-service────────Mongodb文件服务模块[11010] ├───────redis-delay-service─────────延迟消费服务模块[11020] ├──cloud-modules-web────────────────Web微服务模块 ├───────modules-web-security────────Web医生服务模块[12010] ├───────modules-web-user────────────Web用户服务模块[12020] ├──cloud-modules-wechat─────────────Wechat微服务模块 ├───────modules-wechat-user─────────Wechat用户服务模块[努力更新中] └───────modules-wechat-doctor───────Wechat医生服务模块[努力更新中] 修改日志 修改日志 修改人 修改日期 版本计划 V1.0 刘岗强 2019-01-07 项目初始化 V1.1 刘岗强 待定 新增自动问答 项目介绍 基于Spring Cloud Finchley SR2 Spring Boot 2.0.7的最新版本。 核心基础项目内实现类自定义的权限注解,配合RBAC权限模型+拦截器即可实现权限的控制,具体的参考项目中的实现。同时也封装了一些顶层类和结果集等。 注册中心实现高可用配置,详情见eureka的one、two、three三个配置文件,摘要如下。 ------------------------------------------配置节点一---------------------------------------------- server: port: 8761 spring: application: name: cloud-eureka-server eureka: instance: hostname: cloud.server.one prefer-ip-address: true instance-id: ${spring.cloud.client.ip-address}:${server.port}:${spring.application.name} client: healthcheck: enabled: true register-with-eureka: false fetch-registry: false service-url: defaultZone: http://cloud.server.two:8762/eureka/,http://cloud.server.three:8763/eureka/ ------------------------------------------配置节点----------------------------------------------
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值