SpringCloud(2)Eureka与Ribbon
一:主要内容
什么是注册中心,常见的注册中心有哪些,为什么需要注册中心,什么是Eureka注册中心,Eureka的快递Demo,Eureka的架构原理,Ribbon是什么,有哪些负债均衡策略,区别是什么,Ribbon的负载均衡策略设置。
一:Eureka注册中心学习目标
Eureka服务注册中心,服务注册中心是服务实现化管理的核心组件,类似目录服务的作用,主要用来存储服务信心,譬如提供者url串,路由信息等,服务注册中心是被微服务架构中最基础的设施之一。
在微服务架构流行之前,注册中心就已经开始出现在分布式架构的系统中,Dubbo是一个在国内比较流行的分布式架构,被大量的中小型互联网公司多采用,它提供了比较完善的服务治理功能,而服务治理的实现主要依靠的就是注册中心。
目标:什么是注册中心,常见的注册中心有哪些,为什么需要注册中心,注册中心解决了什么问题,什么是Eureka注册中心,Eureka注册中心三种角色,Eureka安全认证,Eureka优雅停服,Eureka自我保护,CAP原则,Eureka架构原理,高可用Eureka注册中心。
1.什么是注册中心
注册中心可以说是微服务架构中的通讯录,它记录了服务和服务地址的映射关系,在分布式架构中,服务会注册到这里,当服务需要调用其它服务时,就到这里找到服务的地址,进行调用。
举个现实生活中的例子,比如说,我们手机中的通讯录的两个使用场景: 当我想给张三打电话时,那我需要在通讯录中按照名字找到张三,然后就可以找到他的手机号拨打电话。—— 服务发现 李四办了手机号并把手机号告诉了我,我把李四的号码存进通讯录,后续,我就可以从通讯录找到他。—— 服 务注册 通讯录 —— ?什么角色(服务注册中心)
3.为什么需要注册中心
了解了什么是注册中心,那么我们继续谈谈,为什么需要注册中心。在分布式系统中,我们不仅仅是需要在注册
6.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">
<modelVersion>4.0.0</modelVersion>
<!-- 项目坐标地址 -->
<groupId>com.example</groupId>
<!-- 项目模块名称 -->
<artifactId>eureka-demo</artifactId>
<!-- 项目版本名称 快照版本SNAPSHOT、正式版本RELEASE -->
<version>1.0-SNAPSHOT</version>
<!-- 继承 spring-boot-starter-parent 依赖 -->
<!-- 使用继承方式,实现复用,符合继承的都可以被使用 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.7.RELEASE</version>
</parent>
<!--
集中定义依赖组件版本号,但不引入,
在子工程中用到声明的依赖时,可以不加依赖的版本号,
这样可以统一管理工程中用到的依赖版本
-->
<properties>
<!-- Spring Cloud Hoxton.SR1 依赖 -->
<spring-cloud.version>Hoxton.SR1</spring-cloud.version>
</properties>
<!-- 项目依赖管理 父项目只是声明依赖,子项目需要写明需要的依赖(可以省略版本信息) -->
<dependencyManagement>
<dependencies>
<!-- spring cloud 依赖 -->
<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>
</project>
②:创建子项目

名字叫eurkea-server
<?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.xxxx</groupId>
<artifactId>eureka-server</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>com.xxxx</groupId>
<artifactId>eureka-demo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.2.4.RELEASE</version>
</dependency>
</dependencies>
</project>
③:创建一个启动类和一个yml文件

配置yml文件

package com.xxxx;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer //通过注册中心来启动这个应用
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class);
}
}
如果启动的时候出现错误的时候;

我们可以在这个加这个:

打开网页输入localhost:8761

浙公网安备 33010602011771号