文章目录
spring-cloud official website : http://projects.spring.io/spring-cloud/
约定优于配置
快速启动
轻量级组件,组件功能齐全
适用于各种环境
分布式项目架构
根据业务需求进行拆分成N个子系统,多个子系统相互协作才能完成业务流程子系统之间通讯使用RPC远程通讯技术。
springboot不是微服务,只是简化开发,springcloud才是真正的微服务
一、springcloud (RPC框架)
-
springcloud是一个微服务框架,并提供全套分布式系统解决方案。支持配置管理,熔断机制,leader选举,服务治理,分布式session,微代理,控制总线,智能路由,一次性token。
-
它运行环境简单,可以在开发人员的电脑上跑。
-
spring cloud是基于Springboot的
核心五大组件
服务发现 —— Netflix Eureka(或zookeeper,consul)
客服端负载均衡 —— Netflix Ribbon,feign(http client)
断路器 —— Netflix Hystrix
服务网关 —— Netflix Zuul(停止更新)
监控 —— actuctor
分布式配置 —— Spring Cloud Config
解决了什么问题?
配置管理、注册中心、服务发现、服务注册、断路器、路由策略、负载均衡、全局锁
服务提供者与消费关系
服务提供者:提供服务被人调用
消费者:调用被人服务
1.服务的注册与发现
解决的硬编码问题
将注册的信息保存到服务注册表
服务注册与注销
健康检查
2.市场调查
Eureka
zookeeper(客户端发现,dubbo注册中心 hadoop,HBASE,solr) PORT:2181
etcd
consul(go语言编写)
二、Eureka服务注册发现
(客户端发现)
Spring Cloud Netflix的Eureka ,eureka是一个服务注册和发现模块。
Eureka由两个组件组成:Eureka服务器和Eureka客户端。
Eureka服务器用作服务注册服务器。
Eureka客户端是一个java客户端,用来简化与服务器的交互、作为轮询负载均衡器,并提供服务的故障切换支持。
Netflix在其生产环境中使用的是另外的客户端,它提供基于流量、资源利用率以及出错状态的加权负载均衡。
服务注册后,每30秒通过发送心跳给server续约,90秒没有收到心跳的话,会剔除这个节点
eureka server全部挂掉时,app客户端也会有缓存机制
在我看来,Eureka的吸引力来源于以下几点:
- 开源,基于Java,高可靠
- 功能齐全:不但提供了完整的注册发现服务,还有Ribbon等可以配合使用的服务。
- 可以使用Spring Cloud, 与Eureka进行了很好的集成,使用起来非常方便。
github - 源码地址
1.开始搭建 springcloud-all
springboot版本 与 cloud版本对应关系 点击查看
目录结构
父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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zjy.cloud</groupId>
<artifactId>springcloud-all</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<name>springcloud-all</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<!-- 严格注意springboot版本 和 cloud版本 -->
<version>2.1.9.RELEASE</version>
<relativePath/>
</parent>
<modules>
<module>eureka-server</module>
<module>eureka-client-user</module>
<module>eureka-client-movie</module>
</modules>
<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>Greenwich.BUILD-SNAPSHOT</spring-cloud.version>
<spring-boot.version>2.1.9.RELEASE</spring-boot.version>
<spring-cloud-netflix.version>2.1.2.RELEASE</spring-cloud-netflix.version>
</properties>
<dependencies>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
<version>1.18.10</version>
</dependency>
<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.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>${spring-cloud-netflix.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>${spring-cloud-netflix.version}</version>
</dependency>
<!--开启security 验证登录eureka server-->
<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-security</artifactId>-->
<!--</dependency>-->
</dependencies>
</dependencyManagement>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.搭建Eureka-server <服务端> [springboot项目1]
Pom file
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.zjy.cloud</groupId>
<artifactId>springcloud-all</artifactId>
<version>1.0.0</version>
</parent>
<groupId>com.zjy</groupId>
<artifactId>eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-server</name>
<description>注册中心</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.1.9.RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
config file
新旧版本配置密码不同 https://www.jianshu.com/p/a40219751264
server:
port: 8761
spring:
application:
name: eureka-server
# 关闭登录验证
# security:admin:123456@
# user:
# name: admin
# password: 123456
eureka:
instance:
hostname: localhost
prefer-ip-address: true
client:
# 由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己
register-with-eureka: false
# 由于注册中心的职责就是维护服务示例,它并不需要去检索服务,所以也设置为false
fetch-registry: false
serviceUrl:
# 使用Eureka服务器进行身份验证
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
boot file
注解@EnableDiscoveryClient,@EnableEurekaClient的区别
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);
}
}
下一步:
访问地址:http://localhost:8761/
系统状态
Environment test测试环境
数据中心
图中红色报错原因
阈值不达标(单机环境很容易不达标)
2.搭建Eureka-client-user[springboot被调用方]
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.zjy.cloud</groupId>
<artifactId>springcloud-all</artifactId>
<version>1.0.0</version>
</parent>
<groupId>com.zjy</groupId>
<artifactId>eureka-client-user</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-client-user</name>
<description>生产者 user模块 </description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</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>
<version>2.1.9.RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
配置文件yml
server:
port: 8762
spring:
application:
name: eureka-client-user
eureka:
instance:
# 设置应用实例id
instanceId: ${spring.cloud.client.hostname}:${spring.application.name}:${spring.application.instance_id:${server.port}}
client:
service-url:
# 设置eureka实例地址 admin:123456@
defaultZone: http://localhost:8761/eureka
启动类
@SpringBootApplication
@EnableEurekaClient
public class EurekaClientUserApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientUserApplication.class, args);
}
}
接口api
/**
* 用户controller
*
* @author zhaojianyu
* @date 2018-11-14 8:04 PM
*/
@RestController
public class UserController {
@GetMapping("/user")
public String getUser() {
return "我生产了用户"+new Date();
}
}
2.搭建Eureka-client-movie[springboot调用方]
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.zjy.cloud</groupId>
<artifactId>springcloud-all</artifactId>
<version>1.0.0</version>
</parent>
<groupId>com.zjy</groupId>
<artifactId>eureka-client-movie</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-client-movie</name>
<description>调用模块 消费者</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</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>
<version>2.1.9.RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
yml配置
server:
port: 8763
spring:
application:
name: eureka-client-movie
eureka:
instance:
# 设置应用实例id
instanceId: ${spring.cloud.client.hostname}:${spring.application.name}:${spring.application.instance_id:${server.port}}
client:
service-url:
# 设置eureka实例地址
defaultZone: http://localhost:8761/eureka
httpTemplate配置
@Configuration
public class RestClientConfig {
@Bean
@LoadBalanced//rest方式以别名的方式调用,需要依赖ribbon负载均衡器@LoadBalanced(已配置到restconfig)
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
启动类同client-user
/**
* 电影controller
*
* @author zhaojianyu
* @date 2018-11-14 8:04 PM
*/
@RestController
public class MovieController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/movie")
public String getUser() {
String url = "http://eureka-client-user/user";
return "result" + restTemplate.getForObject(url, String.class);
}
}
(上面的配置没加密码)加密码后client端找不到server
https://www.oschina.net/question/3688227_2275113
https://blog.youkuaiyun.com/qq_33802316/article/details/80677644
eureka chinese document:https://springcloud.cc/spring-cloud-dalston.html
三、feign/Ribbon
参考链接
https://blog.youkuaiyun.com/hemin1003/article/details/82043611