SpringBoot和微服务


####微服务CAP Consistency(数据强一致性),Availability(服务可用性),Partition-tolerance(分区容错) ####微服务功能 #####服务的注册与发现 Eureka集中化管理来注册和发现服务,服务注册中心通过心跳检测,检查被注册的服务是否可用 #####容错 熔断器Hystrix(Circuit Breaker),当请求失败次数超过阈值后,开启熔断不执行业务操作,执行快速失败,直接返回请求失败的消息;自我修复机制,一段时间后,会半打开熔断器检查一部分请求是否正常。 + 将资源剥离,当出现故障时,只会隔离该接口;被隔离的接口执行快速失败,不会阻塞超时 + 服务降级,当超过服务处理能力时, + 自我修复能力

负载均衡组件

Ribbon讲请求根据负载均衡策略分配到不同实例中

服务网关

Zuul把所有服务的API接口资源整合,对外透明;可以做身份权限的合法性验证;流量监控

服务链路追踪

Sleuth定位复杂复杂系统中出错的位置,实时观察链路的调用情况

安全控制

Security


####Boot 引导,快速启动spring应用 ####@SpringBootApplication SpringBootApplication启动类注解包括了多个注解,是一个符合注解的简写,主要包括@Configuration,@EnableAutoConfiguration,@ComponentScan ####@EnableAutoConfiguration 自动获取所有配置 ####配置方式优先级 高优先级会覆盖低优先级的配置项 + 命令行参数 + 系统环境变量 + 位于文件系统中的配置 + 位于classpath中的配置 + 代码中的配置

spring-boot-starter-web

讲spring-boot-starter-web加入到依赖中,就可以用内置的tomcat启动web应用

SOA和微服务

SOA喜欢重用,微服务喜欢重写

微服务的注册与发现

同一个微服务启动多个实例形成集群,让服务器集群作为一个逻辑服务主体对外提供服务,这个过程就是注册(Service Registry)。
服务访问者(Service Accessors)如何访问到这个逻辑服务主体的过程,就是发现

集群日志

ELK(ElasticSearch,Logstash,Kibana),es对对于高频写入没有很高的承受力,可以用kafka当做数据采集的缓冲区,减轻写入es的负担

ribbon的LoadBalanced

restTemplate.getForObject的请求url是生产者的yml里的spring.application.name


####server配置 ``` server: port: 8761

eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://({eureka.instance.hostname}:){server.port}/eureka/

####启动eureka-server,访问server端
<img style="800px;height:400px" src="https://img2018.cnblogs.com/blog/841731/201903/841731-20190316201254974-746091965.png" align=center />
<img style="800px;height:400px" src="https://img2018.cnblogs.com/blog/841731/201903/841731-20190316195540326-237600615.png" align=center />
####eureka多client配置,2个生产者的配置文件  
application-client1.yml,application-client2.yml  

server:
port: 8762

spring:
application:
name: eureka-client
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka


server:
port: 8763

spring:
application:
name: eureka-client
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka

####读取不同的配置,启动2个客户端
java -jar target/eureka-client.jar --spring.profiles.active=client1    
java -jar target/eureka-client.jar --spring.profiles.active=client2

####访问2个client端地址,输出测试数据和启动端口 

http://localhost:8762/hi?name=test
http://localhost:8763/hi?name=fore
Hi,test!I am from port 8762
Hi,test!I am from port 8763

####查看eureka-server的注册中心
<img style="800px;height:400px" src="https://img2018.cnblogs.com/blog/841731/201903/841731-20190316201809542-381591445.png" align=center />
####consumer的配置,启动LoadBalanced实现访问生产者的负载均衡

server:
port: 8764
spring:
application:
name: eureka-ribbon-client
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka


####请求consumer的接口,发现输出的端口号在2个生产者之间切换

http://127.0.0.1:8764/getHi
Hi,test!I am from port 8762
Hi,test!I am from port 8763

####查看eureka-server的注册中心
<img style="800px;height:400px" src="https://img2018.cnblogs.com/blog/841731/201903/841731-20190316202614202-1346654588.png" align=center />
原文地址:https://www.cnblogs.com/wanli002/p/10543968.html