断路器简介
断路器本身是电路上的一种过载保护装置,当线路中有电器发生短路时,它能够及时的切断故障电路以防止严重后果发生
以下是软件系统中的熔断机制
断路器状态
完全打开
一定时间内 达到多少次C无法调用 并且多次检测C没有恢复迹象 断路器完全打开
直接返回一个错误提示给页面
半开
一定时间内 达到多少次C无法调用 短时间内 C有恢复迹象 断路器会将部分请求发送给C 部分请求通过断路器返回回调 当部分请求完成能正常调用
可能会关闭断路器
关闭
如果C服务一直处于正常状态 B都可以正常调用 断路器处于关闭状态
降级
针对服务限流,当服务不可用时能够熔断并降级,防止级联故障。
让目前使用人数最多的服务占用最大的资源,其它服务降低资源,保持正常运行状态
配置中心简介
Spring Cloud Config为服务端和客户端提供了分布式系统的外部化配置支持。配置服务器为各应用的所有环境提供了一个中心化的外部配置。它实现了对服务端和客户端对Spring Environment和PropertySource抽象的映射,所以它除了适用于Spring构建的应用程序,也可以在任何其他语言运行的应用程序中使用。作为一个应用可以通过部署管道来进行测试或者投入生产,我们可以分别为这些环境创建配置,并且在需要迁移环境的时候获取对应环境的配置来运行
配置中心原理
创建配置中心微服务
pom.xml
<dependencies>
<!-- 引入配置中心 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
资源文件application.yml
注意:不要使用Tab键
server:
port: 8888
spring:
cloud:
config:
server:
git:
#配置中心的路径
uri: https://github.com/HuJunFeiXM/configcenter
入口ConfigServer
package cn.ps;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class ConfigServer {
public static void main(String[] args) {
SpringApplication.run(ConfigServer.class, args);
}
}
启动后访问:
创建EmpService微服务
pom.xml添加
<dependencies>
<!-- 引入配置中心读取bootstrap.yml文件 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
bootstrap.yml配置
spring:
application:
name: empservice
profiles:
active: dev
#配置中心的ip和端口等价于http://localhost:8888/empservice-dev.yml
cloud:
config:
uri: http://localhost:8888
入口EmpService
package cn.ps;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableEurekaClient
//启动feign
@EnableFeignClients
@RestController
public class EmpService {
public static void main(String[] args) {
new SpringApplicationBuilder(EmpService.class).web(true).run(args);
}
}
启动后访问:
创建Zuui路由微服务
pom.xml添加
<dependencies>
<!-- 引入zuul -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<!-- 引入微服务 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
application.yml配置
eureka:
instance:
#将来注册到注册中心的微服务包括注册中心都是用ip地址
preferIpAddress: true
client:
#是否注册到注册中心 注册中心自己不需要注册
registerWithEureka: true
#是否抓取注册中心的注册信息
fetchRegistry: true
serviceUrl:
defaultZone: http://192.168.153.132:8761/eureka/
spring:
application:
name: empzuul
server:
#程序入口使用80端口
port: 80
入口ZuulRoute
package cn.ps;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication
//启动zuui
@EnableZuulProxy
//启动微服务
@EnableEurekaClient
public class ZuulRoute {
public static void main(String[] args) {
SpringApplication.run(ZuulRoute.class, args);
}
}
启动后可进行均衡负载测试