1.为什么需要spring cloud
1.1. 什么是单体应用(Monolith)
这种将所有的代码及功能都包含在一个WAR包中的项目组织方式被称为Monolith
1.2缺点
在项目很小的情况下这种单体应用比较简单,但是随着项目越变越大,代码越来越多。就会存在以下缺点。
①编译难,部署难,测试难
②技术选择难
③扩展难
2.1 MicroService(微服务)架构
2.2 为什么需要
使用微服务架构就可以解决单体项目缺点。解决单体应用的不足
2.3 什么是MicroService架构
把一个系统拆分为多个独立技术选型,独立开发,独立部署,独立运维的微服务
微服务架构使用场景 规模大
微服务架构spring解决方案
开发单个服务springboot,协调多个服务用springcloud http协议
单体应用架构:中小型项目(功能相对较少) crm 档案管理 库存管理,公司官网等
微服务架构:大型项目(功能比较多) 商城 erp,人力资源等
springcloud就是一个基于Spring Boot实现的服务治理工具包
2. Spring cloud概述
1.1 是什么
Spring cloud是一个基于Spring Boot实现的服务治理工具包,用于微服务架构中管理和协调服务的。
五大神兽:
服务注册发现——Netflix Eureka : 注册所有微服务的通信地址
客服端负载均衡——Netflix Ribbon\Feign :服务之间的调用问题
断路器——Netflix Hystrix :解决微服务故障问题,微服务的隔离
服务网关——Netflix Zuul :微服务的统一入口
分布式配置——Spring Cloud Config :统一管理微服务的配置文件
Spring cloud是微服务架构中服务治理工具集,有很多产品组成。核心为五大神兽。相较于dubbo更加靠谱。
1.2 为什么需要Springcloud?
他是用来管理和协调微服务架构中多个服务
3.springcloud入门环境搭建
1)搭建环境
以maven多模块化的方法搭建
2)服务提供者=提供服务
3)服务消费者-调用服务
1.1parent-. 搭建父模块
Parent:
限定springboot版本-开发单个服务
限定springcloud版本-协调多个服务
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
<springboot.version>2.0.5.RELEASE</springboot.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${springboot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
1.2服务提供者product-service/product-common
user_interface/facade 公共代码
domain,query,接口,util
user_provider_8001 :服务提供者
public User getUser
<!--springboot支持-->
<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>
</dependency>
1.3 服务消费者
ser_consumer_9001:服务消费者
User user = 调用代码
<!--springboot支持-->
<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>
</dependency>
yml:
server:
port: 9001
spring:
application:
name: USER-CONSUMER
通过springboot实现服务提供者,然后再服务消费者以restTemplate以restful的方式完成服务调用。
4.springcloud Eureka
1.1 是什么
服务注册中心,c/s架构模式,EurekaServer,EurekaClient(服务提供者注册服务,服务消费者获取服务列表完成)
Eureka是netflix的一个子模块,也是核心模块之一
1.2. Eureka注册中心搭建
<!--springboot支持-->
<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>
</dependency>
<!--Eureka服务端支持-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
yml配置
server:
port: 7001
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false #是否要注册到eureka
fetchRegistry: false #表示是否从Eureka Server获取注册信息
serviceUrl:
defaultZone: http://
e
u
r
e
k
a
.
i
n
s
t
a
n
c
e
.
h
o
s
t
n
a
m
e
:
{eureka.instance.hostname}:
eureka.instance.hostname:{server.port}/eureka/ #单机配置
主类
@SpringBootApplication
@EnableEurekaServer //标识是eureka服务端
public class EnrekaServerApplication_7001 {
public static void main(String[] args) {
SpringApplication.run(EnrekaServerApplication_7001.class);
}
}
1.3. 服务提供者注册到Eureka
<!--eureka客户端支持 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
修改配置
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka #告诉服务提供者要把服务注册到哪儿
instance:
prefer-ip-address: true # 当调用getHostname获取实例的hostname时,返回ip而不是host名称
ip-address: 127.0.0.1 # 指定自己的ip信息,不指定的话会自己寻找
1.4. 服务消费者从Eureka调用服务
<!-- Eureka客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
配置
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka #告诉服务提供者要把服务注册到哪儿
instance:
prefer-ip-address: true # 当调用getHostname获取实例的hostname时,返回ip而不是host名称
ip-address: 127.0.0.1 # 指定自己的ip信息,不指定的话会自己寻找
整改服务调用
@Autowired
private DiscoveryClient discoveryClient;// Eureka客户端,可以获取到服务实例信息
// String baseUrl = "http://localhost:8081/user/";
// 根据服务名称,获取服务实例
List<ServiceInstance> instances = discoveryClient.getInstances("user-service");
// 因为只有一个UserService,因此我们直接get(0)获取
ServiceInstance instance = instances.get(0);
// 获取ip和端口信息
String baseUrl = "http://"+instance.getHost() + ":" + instance.getPort()+"/user/";
this.restTemplate.getForObject(baseUrl + id, User.class)
5.Eureka注册中心集群
1.1集群和分布式概念的理解
分布式:把不同业务分散到不同的服务器集群。
两个班长 一个收作业 一个点名
1. 2为什么需要集群
如果只有一个注册中心服务器,会存在单点故障所以要集群。
1.3 搭建集群
主类
改名,该端口
映射hosts
127.0.0.1 eureka-7001.com
127.0.0.1 eureka-7002.com
配置
7001:
server:
port: 7001
eureka:
instance:
hostname: eureka-7001.com
client:
registerWithEureka: false #
fetchRegistry: false #
serviceUrl:
#defaultZone: http://
e
u
r
e
k
a
.
i
n
s
t
a
n
c
e
.
h
o
s
t
n
a
m
e
:
{eureka.instance.hostname}:
eureka.instance.hostname:{server.port}/eureka/ #单机配置
defaultZone: http://eureka-7002.com:7002/eureka/
7002:
server:
port: 7002
eureka:
instance:
hostname: eureka-7002.com
client:
registerWithEureka: false #
fetchRegistry: false #
serviceUrl:
#defaultZone: http://
e
u
r
e
k
a
.
i
n
s
t
a
n
c
e
.
h
o
s
t
n
a
m
e
:
{eureka.instance.hostname}:
eureka.instance.hostname:{server.port}/eureka/ # 单机配置
defaultZone: http://eureka-7001.com:7001/eureka/
1.4 服务提供者和服务消费者修改
#defaultZone: http://localhost:7001/eureka
defaultZone: http://eureka-7001.com:7001/eureka,http://eureka-7002.com:7002/eureka
如果是只有一个Eureka注册中心,会存在单点故障,要做集群,做了集群后,服务提供者和消费者注册服务或发现服务时要配置集群(把多个Eureka都配置上).