学习记录 微服务(SpringCloud简述,Eureka入门案例)
1. 初始化 Spring Cloud
1.1 什么是微服务
- 微服务是一种架构风格,将单个应用程序划分为小型的服务单元
1.2 什么是 Spring Cloud
- Spring Cloud 是一系列框架的集合
- 利用Spring Boot的开发便利性,简化了分布式系统开发
1.3 Spring Cloud 常见模块
- SpringCloud主要涉及的组件包括:
- Eureka:服务注册中心,用于管理服务
- Ribbon:负载均衡(集群)
- Hystix:熔断器,能够防止服务的雪崩效应。
- Zuul:服务网关,提供路由转发、请求过滤等功能。
- Feign:服务调用,简化Http接口的调用。
2. Erueka注册中心
2.1 概述
-
当开发大型项目时,服务的提供方和服务的调用方,将会非常庞大,管理服务的成本将几何倍的增长。
Eureka将负责服务的注册、发现、状态监控
-
注册:Eureka负责管理、记录服务提供者.的信息
-
发现:服务调用者无需自己寻找服务,而是把自己的需求告诉Eureka,
然后Eureka会把符合你需求的服务告诉你
-
监控:服务提供方与Eureka之间通过“心跳”机制进行监控,当某个服务提供方出现问题,
Eureka自然会把它从服务列表中剔除
-
2.2 原理图
-
Eureka:就是服务注册中心(可以是一个集群),对外暴露自己的地址
-
提供者:启动后向Eureka注册自己信息(地址,提供什么服务)
-
消费者:向Eureka订阅服务,Eureka会将对应服务的所有提供者地址列表发送给消费者,并且定期更新
-
心跳(续约):提供者定期通过http方式向Eureka刷新自己的状态
3. Eureka入门案例
3.1 原理图
3.2 配置父项目
- 修改pom.xml,确定spring cloud的版本
<!-- 1 确定spring boot的版本-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
<!--2 确定版本-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<spring-cloud-release.version>Greenwich.RELEASE</spring-cloud-release.version>
</properties>
<!-- 3 锁定sprig cloud版本-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud-release.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 4 确定spring cloud私有仓库-->
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
3.3 配置注册中心
-
步骤一 : 修改pom.xml文件,添加依赖
<dependencies> <!--web起步依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Eureka服务端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies>
-
步骤二 : 修改application.yml文件配置
# 端口号 server: port: 10086 # 服务名称 spring: application: name: eureka_demo # eureka的配置 eureka: client: service-url: defaultZone: http://localhost:${server.port}/eureka/ register-with-eureka: false #不向注册中心注册自己 fetch-registry: false #不向注册中心拉取自己的注册信息
-
步骤三 : 编写启动类,添加注解
package com.czxy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* Created by 澈 on 2019/12/5.
*/
@SpringBootApplication
@EnableEurekaServer //开启eureka服务
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class,args);
}
}
3.4 配置服务提供方
- 步骤一:修改pom.xml文件,添加 eureka client依赖
<dependencies>
<!--web起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Eureka客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--spring boot监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
-
步骤二: 创建yml文件,确定eureka注册中心的位置
#端口号 server: port: 8080 #名称 spring: application: name: service #eureka配置信息 eureka: client: service-url: #注册中心位置 defaultZone: http://localhost:10086/eureka/ instance: #在web页面中的显示效果 instance-id: ${spring.application.name}:${eureka.instance.prefer-ip-address}:${server.port} prefer-ip-address: true #在注册中心中显示IP
-
步骤三: 添加启动类注解
package com.czxy; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; /** * Created by 澈 on 2019/12/5. */ @SpringBootApplication @EnableEurekaClient //开始eureka客户端 public class ServiceApplication { public static void main(String[] args) { SpringApplication.run(ServiceApplication.class,args); } }
-
步骤四: 编写测试数据(提供服务)
- 创建TestController
package com.czxy.controller; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * Created by 澈 on 2019/12/5. */ @RestController @RequestMapping("/test") public class TestController { @GetMapping public ResponseEntity<String> testDemo01(){ return ResponseEntity.ok("testDemo01"); } }
3.5 配置服务使用方
- 步骤一:修改pom.xml文件,添加 eureka client依赖
<dependencies>
<!--web起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Eureka客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--spring boot监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
-
步骤二: 创建yml文件,确定eureka注册中心的位置
server: port: 9090 spring: application: name: client eureka: client: service-url: #注册中心位置 defaultZone: http://localhost:10086/eureka/ instance: #web页面显示效果和访问路径 instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port} prefer-ip-address: true
-
步骤三: 添加启动类注解
package com.czxy; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; /** * Created by 澈 on 2019/12/5. */ @SpringBootApplication @EnableEurekaClient //开启eureka客户端 public class ClientApplication { public static void main(String[] args) { SpringApplication.run(ClientApplication.class,args); } }
-
步骤四: 编写测试类访问服务提供方
- 使用RestTemplate进行远程调用
- 访问路径 : http://localhost:9090/data
package com.czxy.controller; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; /** * Created by 澈 on 2019/12/5. */ @RestController @RequestMapping("/data") public class DataController { @GetMapping public ResponseEntity<String> dataDemo01(){ RestTemplate restTemplate = new RestTemplate(); return restTemplate.getForEntity("http://localhost:8080/test", String.class); } }