SpringCloud快速学习(1)——Eureka

SpringCloud快速学习(1)——Eureka

个人的动力节点视频学习笔记 视频地址:https://www.bilibili.com/video/BV1f94y1U7AB

基本介绍

​ Spring Cloud是实现微服务架构的一系列框架的有机集合。是在Spring Boot基础上构建的,用于简化分布式系统构建的工具集,是拥有众多子项目的项目集合,利用Spring Boot的开发便利性,巧妙地简化了分布式系统基础设施(服务注册与发现、熔断机制、网关路由、配置中心、消息总线、负载均衡、链路追踪等)的开发。

​ Eureka来源于古希腊词汇,意为“发现了”。在软件领域,Eureka是 Netflix在线影片公司开源的一个服务注册与发现的组件,和其Netflix 公司的服务组件(例如负载均衡、熔断器、网关等)一起,被Spring Cloud社区整合为Spring cloud Netflix模块。Eureka是Netflix贡献给Spring Cloud的一个框架!

SpringCloud常用组件

image-20220711213638393

Eureka和zookeeper的区别

什么是CAP原则
  • CAP介绍

一致性(Consistency):数据是一致的

可用性(Availability):一个节点挂了,整个集群可以继续对外提供服务

分区容错性(Partition tolerance):由于机房网络或者分区等原因会导致各个机器中的数据短暂不一致

  • 选择

P是必须保证的

CP:数据一致,但节点挂了系统会暂时不能提供服务

AP:数据可能不一致

CAP对二者的影响

ZK遵循CP原则

eureka注重AP高可用

使用

父模块

  • 目录结构

image-20220711224515277

  • 父亲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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.12.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>edu.bcy</groupId>
    <artifactId>SpringCloudStudy</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>Eureka-server</module>
        <module>02-eureka-client-a</module>
        <module>02-eureka-client-b</module>
    </modules>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR12</spring-cloud.version>
    </properties>

</project>

01-eureka-server

  • 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">
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.12.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <name>01-eureka-server</name>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>Eureka-server</artifactId>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR12</spring-cloud.version>
    </properties>


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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <!--  依赖管理 只是帮你去管理版本号以及子模块的依赖-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

  • 启动类
@SpringBootApplication
@EnableEurekaServer // 开启eureka的注册中心的功能
public class EurekaServerApplication {

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

}
  • 配置文件
#单机
server:
    port: 8761  # eureka的默认端口  6379  8080  3306   8848
spring:
    application:
        name: eureka-server  # 应用名称 不要使用特殊字符
eureka: # eureka的配置分为三类 server  client  实例的  eureka-server既是服务端又是客户端
    server:
        eviction-interval-timer-in-ms: 10000 # 服务端间隔多少毫秒做定期删除的操作
        renewal-percent-threshold: 0.85 # 续约百分比 超过85%的应用没有和你续约 那么eureka会保护服务 不会剔除任何一个
    instance: # 实例的配置
        instance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port}   # 主机名称 : 应用名称 : 端口号
        hostname: localhost  # 主机名称 或者服务的ip
        prefer-ip-address: true # 以ip的形式显示具体的服务信息
        lease-renewal-interval-in-seconds: 5  # 服务实例的续约的时间间隔
    client:
        service-url:
            defaultZone: ${EUREKA_SERVER_URL:http://localhost:8761/eureka}
        register-with-eureka: ${REGISTER_WITH_EUREKA:true}  # 先将server自己注册自己的开关 关掉
        fetch-registry: true
# docker run -p 端口  -d 后台运行 --link 指定网络host文件映射的  -e MYSQL_ROOT_PASSWORD=123456 -v 文件挂载

# 集群
#server:
#    port: 8761  # eureka的默认端口  6379  8080  3306   8848
#spring:
#    application:
#        name: eureka-server  # 应用名称 不要使用特殊字符
#eureka:
#    client:
#        service-url: # 你不写 默认 8761
#            defaultZone: http://peer2:8762/eureka,http://peer3:8763/eureka
#    instance: # 实例的配置
#        instance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port}   # 主机名称 : 应用名称 : 端口号
#        hostname: peer1  # 主机名称 或者服务的ip
#        prefer-ip-address: true # 以ip的形式显示具体的服务信息
#        lease-renewal-interval-in-seconds: 5  # 服务实例的续约的时间间隔

#集群的终极方案
#server:
#    port: 8761  # eureka的默认端口  6379  8080  3306   8848
#spring:
#    application:
#        name: eureka-server  # 应用名称 不要使用特殊字符
#eureka:
#    client:
#        service-url: # 你不写 默认 8761
#            defaultZone: http://peer1:8761/eureka,http://peer2:8762/eureka,http://peer3:8763/eureka
#    instance: # 实例的配置
#        instance-id: ${spring.application.name}:${server.port}   # 主机名称 : 应用名称 : 端口号
##        hostname: peer1  # 主机名称 或者服务的ip
#        prefer-ip-address: true # 以ip的形式显示具体的服务信息
#        lease-renewal-interval-in-seconds: 5  # 服务实例的续约的时间间隔

02-eureka-client-a

  • 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">
    <parent>
        <artifactId>SpringCloudStudy</artifactId>
        <groupId>edu.bcy</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>02-eureka-client-a</artifactId>
    <name>02-eureka-client-a</name>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR12</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

  • 启动类
@SpringBootApplication
@EnableEurekaClient // 开启客户端的功能
public class EurekaClientAApplication {

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

}
  • 配置
server:
    port: 8080 # 客户端的端口没有要求
spring:
    application:
        name: eureka-client-a
# 注册的含义是什么? 就是将自己的一些信息(什么信息ip port...) 发送过去 (发到哪里)
eureka:
    client: # 客户端的相关配置
        service-url: # 指定注册的地址
            defaultZone: http://localhost:8761/eureka
        register-with-eureka: true  # 可以不往eureka-server注册
        fetch-registry: true  # 应用是否去拉去服务列表到本地
        registry-fetch-interval-seconds: 10  # 为了缓解服务列表的脏读问题 时间越短脏读越少  性能消耗大
    instance:
        hostname: localhost # 应用的主机名称 最好写主机ip
        instance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port}
        prefer-ip-address: true # 显示ip
        lease-renewal-interval-in-seconds: 10 # 示例续约的时间




#server:
#    port: 8080 # 客户端的端口没有要求
#spring:
#    application:
#        name: eureka-client-a
## 注册的含义是什么? 就是将自己的一些信息(什么信息ip port...) 发送过去 (发到哪里)
#eureka:
#    client: # 客户端的相关配置
#        service-url: # 指定注册的地址
#            defaultZone: http://peer1:8761/eureka,http://peer2:8762/eureka,http://peer3:8763/eureka
#        register-with-eureka: true  # 可以不往eureka-server注册
#        fetch-registry: true  # 应用是否去拉去服务列表到本地
#        registry-fetch-interval-seconds: 10  # 为了缓解服务列表的脏读问题 时间越短脏读越少  性能消耗大
#    instance:
#        hostname: localhost # 应用的主机名称 最好写主机ip
#        instance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port}
#        prefer-ip-address: true # 显示ip
#        lease-renewal-interval-in-seconds: 10 # 示例续约的时间

  • 关于怎么启动两个02-eureka-client-a

image-20220711225746311

02-eureka-client-b

  • 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">
    <parent>
        <artifactId>SpringCloudStudy</artifactId>
        <groupId>edu.bcy</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>02-eureka-client-b</artifactId>

    <name>02-eureka-client-b</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR12</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

  • 启动类
@SpringBootApplication
@EnableEurekaClient
public class EurekaClientBApplication {

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

}
  • 配置
server:
    port: 8081
spring:
    application:
        name: eureka-client-b
eureka:
    client:
        service-url:
            defaultZone: http://localhost:8761/eureka
    instance:
        prefer-ip-address: true
        instance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port}
        hostname: localhost
        lease-renewal-interval-in-seconds: 10

注册结果

image-20220711225840249

服务发现

指服务之间相互找到

02-eureka-client-a

@RestController
public class DiscoveryController {

    @Autowired
    private DiscoveryClient discoveryClient;

    /**
     * 通过应用名称 找到服务的ip和port
     * 你会不会在java代码中发起http请求呢?
     * http://192.168.204.1:8081/api?
     *
     * @param serviceName
     * @return
     */
    @GetMapping("test")
    public String doDiscovery(String serviceName){
        // 这就是服务发现  通过服务的应用名 找到服务的具体信息
        List<ServiceInstance> instances = discoveryClient.getInstances(serviceName);
        instances.forEach(System.out::println);
        ServiceInstance serviceInstance = instances.get(0);
        String ip = serviceInstance.getHost();
        int port = serviceInstance.getPort();
        System.out.println(ip+":"+port);
        // 这里去找b的ip和port
        return instances.get(0).toString();
    }
}

运行

image-20220712231444294

访问接口

访问a的接口,拿到了客户端b的信息

image-20220712231350100

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值