前言
微服务基础:
1、微服务通过业务区分的方式进行独立部署
2、微服务通过某种方式进行整合,从而可以做到最基本的相互调用,来满足业务的整体需求(SOA的架构体系(RPC))
3、微服务需要的基础组件:注册中心、全链路监控、熔断和降级机制、API网关、统一配置中心、消息体系、数据库
4、原生老版本的springcloud中调用服务的组件:ribbon(负载均衡)
5、consul对服务都要进行健康检查,所以每个注册到consul的服务必须引入spring-boot-starter-actuator,用来通过监控接口获取当前服务的状态
spring cloud原生rpc dubbo
协议 Http 可指定协议(如HTTP、TCP)
服务注册方式 eureka、consul zookeeper、nacos
服务调用方式 feign 通过Api提供的接口,直接进行调用
6、降级和熔断:两种功能,都是用来处理被调用方如果出现异常该如何处理的逻辑。
1.consul启动
./consul agent -dev
2. provider和consumer的模块构建
2.1 provider
spring:
profiles:
active: dev
application:
name: consul-provider
cloud:
consul:
host: localhost
port: 8500
config:
enabled: true #false禁用Consul配置,默认true
format: YAML # 表示consul上面文件的格式 有四种 YAML PROPERTIES KEY-VALUE FILES
#data-key: configuration #表示consul上面的KEY值(或者说文件的名字) 默认是data
data-key: data #表示consul上面的KEY值(或者说文件的名字) 默认是data
#prefix设置配置值的基本文件夹
prefix: config
#defaultContext设置所有应用程序使用的文件夹名称
default-context: consul-provider
#profileSeparator设置用于使用配置文件在属性源中分隔配置文件名称的分隔符的值
profile-separator: ','
server:
port: 10013
feign:
hystrix:
enabled: true
#监控点的配置
management:
endpoints:
web:
exposure:
include: "*"
2.2 consumer
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.cloud:spring-cloud-starter-consul-discovery')
compile('org.springframework.cloud:spring-cloud-starter-netflix-hystrix')
compile('org.springframework.cloud:spring-cloud-starter-openfeign')
}
@Component
//降级逻辑
public class ServiceFallback implements FallbackFactory<DemoService> {
@Override
public DemoService create(Throwable cause) {
return new DemoService() {
@Override
public String consumer() {
return "调用失败";
}
};
}
}
3.构建监控面板 hystrix
spring:
application:
name: hystrix-dashboard
server:
port: 20010
dependencies {
compile('org.springframework.boot:spring-boot-starter-actuator')
//配置监控面板
compile('org.springframework.cloud:spring-cloud-starter-netflix-hystrix')
//配置监控仪表盘
compile('org.springframework.cloud:spring-cloud-starter-netflix-hystrix-dashboard')
}
@SpringCloudApplication
@EnableHystrixDashboard
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class) ;
}
}
在http上查看 10014就是要监控的端口 点monitor stream来进行
http://localhost:20010/hystrix