一、搭建微服务系统核心中枢
1、服务治理的核心组件
注册中心
服务提供者
服务消费者
2、服务治理概述
| 功能 | 说明 |
|---|---|
| 服务注册 | 分布式系统架构中,每个微服务在启动时,会将自己的信息存储在注册中心 |
| 服务发现 | 服务消费者从注册中心查询服务提供者的网络信息,并通过此信息调用服务提供者的接口 |
- Spring Cloud 的服务治理可以使用 Eureka 组件
3、注册中心对各个微服务的管理
通过心跳机制,即每隔一定的时间微服务会向注册中心进行汇报,如果注册中心⻓时间无法与某个微服务通信,就会自动销毁该服务
当某个微服务的网络信息发生变化时,会重新注册
4、服务提供者、服务消费者、注册中心的关联
启动注册中心
服务提供者启动,在注册中心注册一个可以提供服务的实例
服务消费者启动,在注册中心订阅需要调用的服务
注册中心将服务提供者的信息推送给服务调用者
服务消费者通过相关信息(IP、端口)调用服务提供者的服务
5、注册中心核心模块
服务注册表
服务注册
服务发现
服务检查
二、Spring Cloud Eureka 概述
1、基本介绍
- 提供服务注册和服务发现功能
2、Spring Cloud Eureka 的组成
* Eureka Server 服务端
* Eureka Client 客户端
三、微服务快速入门
1、父工程
(1)创建父工程
- 创建 Maven 工程(父工程,不使用模板),在 pom.xml 文件中配置相关依赖
<?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.my</groupId>
<artifactId>SpringCloudTest</artifactId>
<version>1.0-SNAPSHOT</version><!-- Spring Boot --><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.7.RELEASE</version></parent><dependencies><!-- Spring Web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- JDK 9 以上额外配置 --><!-- <dependency>--><!-- <groupId>javax.xml.bind</groupId>--><!-- <artifactId>jaxb-api</artifactId>--><!-- <version>2.3.0</version>--><!-- </dependency>--><!-- <dependency>--><!-- <groupId>com.sun.xml.bind</groupId>--><!-- <artifactId>jaxb-impl</artifactId>--><!-- <version>2.3.0</version>--><!-- </dependency>--><!-- <dependency>--><!-- <groupId>com.sun.xml.bind</groupId>--><!-- <artifactId>jaxb-core</artifactId>--><!-- <version>2.3.0</version>--><!-- </dependency>--><!-- <dependency>--><!-- <groupId>javax.activation</groupId>--><!-- <artifactId>activation</artifactId>--><!-- <version>1.1.1</version>--><!-- </dependency>--></dependencies><!-- Spring Cloud --><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Finchley.SR2</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement></project>
2、注册中心
(1)创建子工程
- 在父工程目录下创建子工程(Module),实现 Eureka 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"><parent><artifactId>SpringCloudTest</artifactId><groupId>com.my</groupId><version>1.0-SNAPSHOT</version></parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>eurekaserver</artifactId><!-- Eureka Server --><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency></dependencies></project>
(2)配置文件
- 在 resources 目录下,创建并配置 application.yaml 配置文件
server:
port: 8761
eureka:
client:
# 是否将当前的 Eureka 服务作为客户端注册
register-with-eureka: false
# 是否获取其他 Eureka 服务
fetch-registry: false
# 注册中心 URL
service-url:
defaultZone: http://localhost:8761/eureka/
(3)启动类
- 创建并配置启动类
package com.my;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
// 注册中心
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
(4)启动注册中心
- 访问地址:http://localhost:8761/
3、服务提供者
- 服务提供者和服务消费者都是通过 Eureka Client 连接到 Eureka Server 完成注册
(1)创建子工程
- 在父工程目录下创建子工程(Module),实现 Eureka Client
<?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>SpringCloudTest</artifactId><groupId>com.my</groupId><version>1.0-SNAPSHOT</version></parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ServerProvider</artifactId><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency></dependencies>
</project>
(2)配置文件
- 在 resources 目录下,创建并配置 application.yaml 配置文件
server:
port: 8010
spring:
application:
# 服务名
name: ServerProvider
eureka:
client:
# 注册路径
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
# 是否将 IP 进行注册
prefer-ip-address: true
(3)启动类
- 创建并配置启动类
package com.my;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ServerProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ServerProviderApplication.class, args);
}
}
(4)启动服务提供者
- 在注册中心启动后启动服务提供者,访问注册中心,查看注册的服务消费者
四、微服务应用实例
1、需求
- 在服务提供者中完成简单的增删改查操作服务
2、具体实现
(1)依赖配置
- 在父工程中引入 Lombok 简化开发
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
(2)Entity
- 创建 Student 实体类
package com.my.entity;
import lombok.Data;
@Data
@AllArgsConstructor
public class Student {
private Integer id;
private String name;
}
(3)Repository
- 创建 StudentRepository 接口
package com.my.repository;
import com.my.entity.Student;
import java.util.Collection;
public interface StudentRepository {
public Collection<Student> findAll();public Student findById(Integer id);public void saveOrUpdate(Student student);public void deleteById(Integer id);}
- 创建 StudentRepositoryImpl 类,实现 StudentRepository 接口
package com.my.repository.impl;
import com.my.entity.Student;
import com.my.repository.StudentRepository;
import org.springframework.stereotype.Repository;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@Repository
public class StudentRepositoryImpl implements StudentRepository {
private static Map<Integer, Student> map;static {map = new HashMap<>();map.put(1, new Student(1,"张三"));map.put(2, new Student(2,"李四"));map.put(3, new Student(3,"王五"));}@Overridepublic Collection<Student> findAll() {return map.values();}@Overridepublic Student findById(Integer id) {return map.get(id);}@Overridepublic void saveOrUpdate(Student student) {map.put(student.getId(), student);}@Overridepublic void deleteById(Integer id) {map.remove(id);}}
(4)Controller
- 创建 StudentHandler 类
package com.my.controller;
import com.my.entity.Student;
import com.my.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Collection;
@RestController
@RequestMapping("/student")
public class StudentHandler {
@Autowired
private StudentRepository studentRepository;
@GetMapping("/findAll")
public Collection<Student> findAll() {return studentRepository.findAll();}@GetMapping("findById/{id}")public Student findById(@PathVariable("id") Integer id) {return studentRepository.findById(id);}@PostMapping("/save")public void save(@RequestBody Student student) {studentRepository.saveOrUpdate(student);}@PutMapping("/update")public void update(@RequestBody Student student) {studentRepository.saveOrUpdate(student);}@DeleteMapping("/deleteById/{id}")public void deleteById(@PathVariable("id") Integer id) {studentRepository.deleteById(id);}}
3、测试
- 依次启动注册中心和服务消费者,使用接口测试工具 Postman 进行测试
(1)findAll
| 参数项 | 说明 |
|---|---|
| 请求类型 | GET 请求 |
| 访问地址 | http://localhost:8010/student/findAll |
(2)findById
| 参数项 | 说明 |
|---|---|
| 请求类型 | GET 请求 |
| 请求地址 | http://localhost:8010/student/findById/1 |
(3)save
| 参数项 | 说明 |
|---|---|
| 请求类型 | POST 请求 |
| 请求地址 | http://localhost:8010/student/save |
- JSON 数据
{
"id": 4,
"name": "nono"
}
(4)update
| 参数项 | 说明 |
|---|---|
| PUT | 请求 |
| 请求地址 | http://localhost:8010/student/update |
- JSON 数据
{
"id": 3,
"name": "jack"
}
(5)deleteById
| 参数项 | 说明 |
|---|---|
| DELETE | 请求 |
| 请求地址 | http://localhost:8010/student/deleteById/4 |