文章目录
Eureka概述
服务注册
微服务是不同的服务分布在不同的主机上,服务跟服务之间可能存在调用关系。
我们可能是通过硬编码写死服务地址的方式来调用其他服务,这样对于后期服务的新增,服务的重新部署都有很多不便利的地方,所以注册中心解决的就是这样一个问题。
注册中心是微服务架构中的通讯录,记录了服务和服务地址的映射关系。
在分布式架构中,服务都会注册到这里,当服务需要调用其他服务时,就通过注册中心寻找服务地址,当新增一个服务时,也会将服务地址注册到注册中心
服务治理
服务治理可以说是微服务架构中最为核心和基础的模块,主要用来实现各个微服务实例的自动化注册与发现。
使用服务治理的原因:
在服务引用并不算多的时候,可以通过静态配置来完成服务的调用,但随着业务的发展,系统功能越来越复杂,相应的微服务也不断增加,此时静态配置会变得越来越难以维护。
并且面对不断发展的业务,集群规模,服务的位置、服务的命名等都有可能发生变化,如果还是通过手工维护的方式,极易发生错误或是命名冲突等问题。
同时,也将消耗大量的人力来维护静态配置的内容。
为了解决微服务架构中的服务实例维护问题,就产生了大量的服务治理框架和产品。
这些框架和产品的实现都围绕着服务注册与服务发现机制来完成对微服务应用实例的自动化管理。
Eureka组成
Eureka服务架构
单机版Eureka实现
配置Eureka服务端(注册中心)
1、建module
cloud-eureka-server7001
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">
<parent>
<artifactId>cloud2021</artifactId>
<groupId>com.lejia.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-eureka-server7001</artifactId>
<dependencies>
<!--eureka-server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>com.lejia.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--一般为通用配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
3、改yml
server:
port: 7001
eureka:
instance:
hostname: localhost #eureka服务端的实例名称
client:
#false表示不向注册中心注册自己
register-with-eureka: false
#false表示自己端就是注册中心,我的职责就是维护服务,并不需要去检索
fetch-registry: false
service-url:
#设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
4、主启动
@SpringBootApplication
@EnableEurekaServer
public class EurekaMain7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaMain7001.class, args);
}
}
5、测试
http://localhost:7001/
2、将provider 8001微服务注册进Eureka中心(注册中心的客户 | 服务提供者)
1、改POM
pom中添加依赖
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2、改yml
yum中添加配置
server:
port: 8001
spring:
application:
name: cloud-payment-service8001
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/cloud2020?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
username: root
password: 1234
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.lejia.springcloud.entities
####################此为新增配置#########################
eureka:
client:
#表示是否将自己注册进EurekaServer默认为true
register-with-eureka: true
#是否从EurekaServe抓取已有的注册信息,默认为true
# 单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka
3、设置主启动
@SpringBootApplication
@EnableEurekaClient
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class,args);
}
}
4、启动测试
http://localhost:7001/
4、将consumer 80微服务注册进Eureka中心(注册中心的客户 | 服务消费者)
同8001服务提供者配置类似
1、改pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2.写yml
server:
port: 80
spring:
application:
name: cloud-consumer-order80
#####################此为新增##################
eureka:
client:
#表示是否将自己注册进EurekaServer默认为true
register-with-eureka: true
#是否从EurekaServe抓取已有的注册信息,默认为true
# 单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka
3、配置主启动
@SpringBootApplication
@EnableEurekaClient
public class OrderMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderMain80.class,args);
}
}
4、测试
http://localhost:7001/
服务调用测试
此时可能有读者会有疑问,这个服务注册中心存不存在都能实现远程服务的调用,那么为什么我们需要使用服务注册中心来多此一举呢!
原因在于当我们的服务变多时,此时如果仅仅是由restTemplate来操作多服务集群时,程序会变得很复杂,我们使用注册中心后,可以便捷的实现集群服务的调用以及任务的调度,负载均衡等功能