目录
1 Eureka服务注册中⼼
1.1 服务注册中⼼组件Eureka
Eureka基础架构:
Eureka交互流程及原理:
Eureka包含两个组件:Eureka Server和Eureka Client,Eureka Client是⼀个Java客户端,⽤于简化与Eureka Server的交互;Eureka Server提供服务发现的能⼒。各个微服务启动时,会通过Eureka Client向Eureka Server 进⾏注册⾃⼰的信息(例如⽹络信息),Eureka Server会存储该服务的信息。
1.图中us-east-1c、us-east-1d,us-east-1e代表不同地区,也就是不同的机房。
2.图中每⼀个Eureka Server都是⼀个集群。
3.图中Application Service作为服务提供者向Eureka Server中注册服务,Eureka Server接受到注册事件会在集群 和分区中进⾏数据同步,Application Client作为消费端(服务消费者)可以从Eureka Server中获取到服务注册信 息,进⾏服务调⽤。
4.微服务启动后,会周期性地向Eureka Server发送⼼跳(默认周期为30秒)以续约⾃⼰的信息。
5.Eureka Server在⼀定时间内没有接收到某个微服务节点的⼼跳,Eureka Server将会注销该微服务节点(默认90 秒)。
6.每个Eureka Server同时也是Eureka Client,多个Eureka Server之间通过复制的⽅式完成服务注册列表的同步。
7.Eureka Client会缓存Eureka Server中的信息。即使所有的Eureka Server节点都宕掉,服务消费者依然可以使⽤ 缓存中的信息找到服务提供者。
Eureka通过⼼跳检测、健康检查和客户端缓存等机制,提⾼系统的灵活性、可伸缩性和⾼可⽤性。
1.2 搭建单例Eureka Server服务注册中⼼
1.2.1 创建父工程
1. 新建父工程【csdn-parent】springboot项目,然后删除 除了 pom.xml 文件父工程下的文件及目录
2. 在⽗⼯程【csdn-parent】的pom.xml⽂件中引⼊Spring Cloud依赖(和 ‘dependencies’ 标签同级)。
<dependencyManagement>
<dependencies>
<!-- SCN -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
3.在⽗⼯程【csdn-parent】的pom.xml⽂件中⼿动引⼊jaxb的依赖。因为JDK9之后默认没有加载该模块,⽽Eureka Server依赖jaxb,所以需要⼿动导⼊,否则Eureka Server服务⽆法启动。
<dependencies>
<!-- Spring Boot的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- Spring Boot测试启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Web依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 日志依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<!-- Lombok工具 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
<scope>provided</scope>
</dependency>
<!-- Actuator可以帮助你监控和管理Spring Boot应用 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!-- 引入Jaxb开始 -->
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.2.10-b140310.1920</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
<!-- 引入Jaxb结束 -->
</dependencies>
4. 将springboot版本设置为 2.1.6.RELEASE,并将packaging设置为pom
<!-- 父项目 -->
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
1.2.2 搭建单例Eureka Server环境
1. 右键⽗⼯程【csdn-parent】选择【New】-【Module】选 项,然后选择创建【Maven】类型项⽬(不勾选模板),将项⽬名称设置为【csdn-cloud-eureka-9200】的普通maven项目。
2.在csdn-cloud-eureka-9200⼯程的pom.xml⽂件中引⼊Eureka Server依赖。
<dependencies>
<!-- Eureka Server依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
3.在csdn-cloud-eureka-9200⼯程的resources⽬录下创建application.yml配置⽂件,配置Eureka Server服务端⼝, 服务名等信息。
server:
port: 9200 # Eureka server服务端⼝
spring:
application:
name: csdn-cloud-eureka # 应⽤名称,会在Eureka中作为服务的id标识
eureka:
client: #Eureka Server本身也是Eureka的⼀个客户端,因为在集群下需要与其他Eureka Server进⾏数据的同步
service-url: # 客户端与Eureka Server交互的地址,如果是集群情况下defaultZone设置为集群下其他的Eureka Server地址,多个地址使⽤","隔开
defaultZone: http://localhost:9200/eureka
register-with-eureka: true # 表示是否向Eureka中⼼注册⾃⼰的信息,因为⾃⼰就是Eureka Server所以不进⾏注册,默认为true
fetch-registry: true # 是否查询/拉取Eureka Server服务注册列表,默认为true
instance:
#hostname: localhost # 当前Eureka实例的主机名
# 使⽤ip注册,否则会使⽤主机名注册(此处考虑到对⽼版本的兼容,新版本经过实验都是ip)
prefer-ip-address: true
# ⾃定义实例显示格式,加上版本号,便于多版本管理,注意是ip-address早期版本是ipAddress
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:@project.version@
6.在com.csdn.eureka包下,创建EurekaApplication9200启动类,声明当前服务为Eureka注册中⼼。
package com.csdn.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
// 声明本项⽬是⼀个Eureka Server
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication9200 {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication9200.class,args);
}
}
7.启动项⽬主类EurekaApplication9200访问 http://127.0.0.1:9200,如果看到如下⻚⾯(Eureka注册中⼼后 台),则表明Eureka Server发布成功。
1.2.3 创建公共模块
1.右键⽗⼯程【csdn-parent】选择【New】-【Module】选 项,然后选择创建【Maven】类型项⽬(不勾选模板),将项⽬名称设置为【csdn-service-common】的普通maven项目。
2. 在csdn-service-common 项目 pom.xml 中导入公共的 jar 包 ,比如 mysql ,mybatis 等。
<dependencies>
<!-- pojo持久化使用 -->
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
3. 在com.csdn.pojo包下,创建 Product 实体类。
package com.csdn.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Id;
import javax.persistence.Table;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "products")
public class Products {
@Id
private long id;
private String name;
private double price;
private String flag;
private String goodsDesc;
private String images;
private long goodsStock;
private String goodsType;
}
1.2.4 创建服务提供者
将商品微服务和⻚⾯静态化微服务注册到Eureka,通过以下的步骤来完成开发。
1. 右键⽗⼯程【csdn-parent】选择【New】-【Module】选 项,然后选择创建【Maven】类型项⽬(不勾选模板),将项⽬名称设置为【csdn-service-product-9000】的普通maven项目。
2.在 csdn-service-product-9000 项⽬的pom.xml⽂件中添加Eureka Client依赖 和 公共模块。
<dependencies>
<!-- 当前项目依赖于公共模块 -->
<dependency>
<groupId>com.example</groupId>
<artifactId>csdn-service-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- Eureka Cloud 依赖(Eureka客户端的依赖:注册中心) -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
2.在csdn-service-product-9000项⽬的application.yml⽂件中配置Eureka服务端信息。
server:
port: 9000
spring:
application:
name: csdn-service-product
eureka:
client:
serviceUrl: # Eureka Server的路径
defaultZone: http://localhost:9200/eureka/
instance:
# 使⽤ip注册,否则会使⽤主机名注册(此处考虑到对⽼版本的兼容,新版本经过实验都是ip)
prefer-ip-address: true
# ⾃定义实例显示格式,加上版本号,便于多版本管理,注意是ip-address早期版本是ipAddress
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:@project.version@
3. 在com.csdn.eureka 包下csdn-service-product-9000 项⽬的启动类ProductApplication9000,添加@EnableDiscoveryClient 注解。
package com.csdn.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
// 声明当前项目是Eureka Client(Eureka客户端)
@EnableDiscoveryClient // 将当前的项目标记为注册中心的客户端,然后向注册中心注册,可以向所有的注册中心注册信息
// @EnableEurekaClient // 将当前项目作为Eureka Server的客户端将信息注册到Eureka Server中。只能自动注册Eureka
// 项目的启动类
@SpringBootApplication
public class EurekaApplication9000 {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication9000.class,args);
}
}
4. 在com.csdn.eureka 包下,添加业务层(Service) 和 控制层(Controller)。
1.2.5 创建服务消费者
1. 右键⽗⼯程【csdn-parent】选择【New】-【Module】选 项,然后选择创建【Maven】类型项⽬(不勾选模板),将项⽬名称设置为【csdn-service-page】的普通maven项目。
2.在 csdn-service-page 项⽬的pom.xml⽂件中添加Eureka Client依赖 和 公共模块。
<dependencies>
<!-- 当前项目依赖于公共模块 -->
<dependency>
<groupId>com.example</groupId>
<artifactId>csdn-service-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- Eureka Cloud 依赖(Eureka客户端的依赖:注册中心) -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
3. 在com.csdn.page 包下的启动类PageApplication,添加@EnableDiscoveryClient注解。
package com.csdn.page;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
// 声明当前项目是Eureka Client(Eureka客户端)
@EnableDiscoveryClient // 将当前的项目标记为注册中心的客户端,然后向注册中心注册,可以向所有的注册中心注册信息
@SpringBootApplication
public class PageApplication {
public static void main(String[] args) {
SpringApplication.run(PageApplication.class,args);
}
@LoadBalanced // 调用Ribbon负载均衡算法,实现负载均衡功能
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
7.先启动csdn-cloud-eureka-9200注册中心,再启动 csdn-service-product-9000 和 csdn-service-page 微服务模块,然后访问http://127.0.0.1:9200进行测试(注:出现下面红字,则重新刷新⻚⾯即可)。