SpringCloud-Eureka(1)

未见彩虹时,我知道我的雨季还没结束

简介

Eureka作为服务注册中心,主要是用于存储服务提供者地址信息,服务发布相关的属性信息,消费者通过主动(消费者拉取)或者被动通知(注册中心推送)的方式获取服务提供者的地址信息,从而将服务提供者与消费者之间的硬编码通信变得更为透明,更为灵活。

Eureka使用步骤

  1. 服务提供者启动
  2. 服务提供者将服务信息注册到注册中心当中
  3. 服务消费者获取服务注册信息:
    1. poll模式:服务消费者基于触发式,或者根据一定时间间隔去注册中心拉取提供者清单(Eureka)
    2. push模式:服务消费者订阅服务,当服务提供者发生变化,在注册中心发生信息变更时,注册中心会主动推送更新后的服务清单给消费者
  4. 服务消费者根据从注册中心获取的清单,根据自己的负载均衡策略来选择直接调用哪个服务提供者
  5. 注册中心会和服务提供者保持健康检查,以便及时剔除失效的服务提供者

在这里插入图片描述

代码开撸

注册中心(非集群模式)

1.项目结构

在这里插入图片描述

2.父pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.amose</groupId>
    <artifactId>springcloud-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <modules>
				<!-- 引入eureka模块 -->
        <module>springcloud-eureka-6661</module>
    </modules>
    <packaging>pom</packaging>

    <dependencyManagement>
        <dependencies>
            <!-- spring cloud 依赖管理-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

3.eureka的pom文件

在这里我使用的是JDK8+SpringCloud Greenwich.RELEASE 在启动时遇到一个eureka引入的jar包版本过低问题,如果您有遇到,请参考 Eureka踩坑事件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springcloud-parent</artifactId>
        <groupId>com.amose</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>spring-cloud-eureka-6661</artifactId>

    <dependencies>
        <!--Eureka server依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            <exclusions>
                <!-- 排除gson2.1-->
                <exclusion>
                    <groupId>com.google.code.gson</groupId>
                    <artifactId>gson</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
<!--        引入gson 2.8.4-->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.4</version>
        </dependency>
    </dependencies>
</project>

4.配置文件

# eureka server 注册中心配置(非集群模式)
server:
  port: 6661
spring:
  application:
    name: springcloud-eureka #应用名称,在eureka中会作为服务名称

# eureka 客户端配置(和Server交互),Eureka Server其实也是个client
eureka:
  instance:
    hostname: localhost # 当前eureka的主机名
  client:
    service-url: #配置客户端所交互的注册中心地址
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka # 这里自己是注册中心,所以填写当前地址
    # 这里,该项目是作为注册中心,所以不需要将该项目注册到注册中心去
    register-with-eureka: false
    # 同时作为注册中心,也不需要查询注册服务信息
    fetch-registry: false

5.启动类

package com.amose;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
//声明当前项目为eureka注册中心
@EnableEurekaServer
public class EurekaApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class, args);
    }
}

启动后(这是web翻译后的结果)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GNgVJOlz-1604384170935)(https://s3-us-west-2.amazonaws.com/secure.notion-static.com/6f9dab6f-b71d-4c21-87cc-aec15736dc62/Untitled.png)]

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值