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常用组件

Eureka和zookeeper的区别

什么是CAP原则

CAP介绍

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

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

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

选择

P是必须保证的

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

AP:数据可能不一致

CAP对二者的影响

ZK遵循CP原则

eureka注重AP高可用

使用

父模块

目录结构

父亲pom

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">

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.3.12.RELEASE

edu.bcy

SpringCloudStudy

pom

1.0-SNAPSHOT

Eureka-server

02-eureka-client-a

02-eureka-client-b

8

8

1.8

Hoxton.SR12

01-eureka-server

pom

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">

org.springframework.boot

spring-boot-starter-parent

2.3.12.RELEASE

01-eureka-server

4.0.0

Eureka-server

1.8

Hoxton.SR12

org.springframework.cloud

spring-cloud-starter-netflix-eureka-server

org.springframework.boot

spring-boot-starter-test

test

org.springframework.cloud

spring-cloud-dependencies

${spring-cloud.version}

pom

import

org.springframework.boot

spring-boot-maven-plugin

启动类

@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

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">

SpringCloudStudy

edu.bcy

1.0-SNAPSHOT

4.0.0

02-eureka-client-a

02-eureka-client-a

1.8

Hoxton.SR12

org.springframework.boot

spring-boot-starter-web

org.springframework.cloud

spring-cloud-starter-netflix-eureka-client

org.springframework.boot

spring-boot-starter-test

test

org.springframework.cloud

spring-cloud-dependencies

${spring-cloud.version}

pom

import

org.springframework.boot

spring-boot-maven-plugin

启动类

@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

02-eureka-client-b

pom

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">

SpringCloudStudy

edu.bcy

1.0-SNAPSHOT

4.0.0

02-eureka-client-b

02-eureka-client-b

Demo project for Spring Boot

1.8

Hoxton.SR12

org.springframework.boot

spring-boot-starter-web

org.springframework.cloud

spring-cloud-starter-netflix-eureka-client

org.springframework.boot

spring-boot-starter-test

test

org.springframework.cloud

spring-cloud-dependencies

${spring-cloud.version}

pom

import

org.springframework.boot

spring-boot-maven-plugin

启动类

@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

注册结果

服务发现

指服务之间相互找到

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 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();

}

}

运行

访问接口

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

推荐链接

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: