服务注册与发现
EUREKA(停更),Zookeeper,Consul,Nocos
服务调用
RIBBON(NETFLIX网飞), LoadBalancer, FELGN(停更) , OpenFeign
服务熔断降级
HYSTRIX (不推荐) ,resilience4j (国外推荐) ,sentienl (国内推荐)
服务网关
ZUUL (不推荐) , gateWay
服务分布配置
SpringCloud Config (不推荐) , Nocos
服务总线
Bus (不推荐) , Nocos
是什么?
客户端的负载均衡和服务调用工具
能干什么?
负载均衡+RestTemplate
LB负载均衡?
Load Balance – 将用户的请求平摊分配到多个服务上,从而到达系统的HA(高可用)
Ribbon与Nginx的区别?
Ribbon :本地的负载均衡 --在调用微服务接口时,会在注册中心获取注册信息服务列表,存到Jvm,从而在本地实现PRC
Nginx : 服务器的负载均衡 --客户端的所有请求都会交给nginx ,然后再转发到不同的服务器
RestTemplate类可用于在应用中调用rest服务,它简化了与http服务的通信方式,统一了RESTful的标准,封装了http链接, 我们只需要传入url及返回值类型即可
RestTemplate
@LoadBalanced //负载均衡
默认 轮询算法
@Configuration
public class ApplicationConfiguration {
@Bean
@LoadBalanced //负载均衡
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
注意不能在==@ComponentScan== 注解能扫描的地方定义配置
package com.wu.myRule;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import com.netflix.loadbalancer.RoundRobinRule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
/**
* @program: IClould2020Wu
* @author: Mr-Jies
* @create: 2020-05-16 10:54
**/
@Configuration
public class MySelfRule {
@Bean
public IRule iRule(){
//定义为随机
return new RandomRule();
}
}
package com.wu.icloud.controller;
@RestController
public class OrderController {
@GetMapping(value = "/consumer/payment1/get/{id}")
public CommonResult getPaymentById2(@PathVariable("id") Long id) {
ResponseEntity<CommonResult> forEntity = restTemplate.getForEntity(URL_PATH + "/payment/get/" + id, CommonResult.class);
if (forEntity.getStatusCode().is2xxSuccessful()){
CommonResult body = forEntity.getBody();
return body;
}
return new CommonResult(444,"error");
}
}
@EnableEurekaClient
@SpringBootApplication
@RibbonClient(name = "CLOUD-PAYMENT-SERVICE",configuration = MySelfRule.class)
public class OrderMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderMain80.class);
}
}
学完juc再回头写
是什么?
声明式的web服务客户端,只需创建一个接口并在注解上添加注解即可做到服务调用
能干什么?
使编写Java HTTP客户端变的简单 在使用Ribbon+ RestTemplate时, 利用RestTemplate对http请求的封装处理, 形成了一套模版化的调用方法。在Feign的实现下我们只需创建一个接口并使用注解的方式来配置它(以前是Dao接口上面标注Mapper注解,现在是一个微服务接口上面标注一个Feign注解即可),(方便了)
Feign集成了Ribbon
利用Ribbon维护了Payment的服务列表信息,并且通过轮询实现了客户端的负载均衡。而与Ribbon不同的是,通过feign只需要定义服务绑定接口且以声明式的方法,优雅而简单的实现了服务调用
@EnableFeignClients
@FeignClient(name = “CLOUD-PAYMENT-SERVICE”)
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-openfeignartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
server:
port: 80
spring:
application:
name: cloud-order-service
eureka:
client:
register-with-eureka: true
fetchRegistry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka # 集群版
@EnableFeignClients
@SpringBootApplication
@EnableFeignClients
@EnableEurekaClient
public class OpenFeignMain80 {
public static void main(String[] args) {
SpringApplication.run(OpenFeignMain80.class,args);
}
}
通过接口与服务端映射
@FeignClient(name = “CLOUD-PAYMENT-SERVICE”)
@Component
@FeignClient(name = "CLOUD-PAYMENT-SERVICE")
public interface OpenfeignPaymentService {
@GetMapping(value = "/payment/get/{id}")
public CommonResult getPaymentById(@PathVariable("id") Long id);
@GetMapping("/payment/timeout")
public String timeout();
}
直接调用service就行了,不需要自己再用RestTempate请求
@RestController
@Slf4j
public class OpenFeignOrder80Controller {
@Autowired
private OpenfeignPaymentService openfeignPaymentService;
@GetMapping(value = "/consumer/payment/get/{id}")
public CommonResult getPaymentById(@PathVariable("id") Long id) {
return openfeignPaymentService.getPaymentById(id);
}
@GetMapping("/consumer/payment/timeout")
public String timeout(){
return openfeignPaymentService.timeout();
}
}
OpenFeign客户端只等待一秒,如果服务端处理需要超过一秒,导致客户端不想等待,直接返回报错
openFeign是ribbon的集成
ribbon:
# 指的是建立连接所用的时间,适用于网络状态正常的情况下,两端连接所用的时间
ReadTimeout: 5000
# 指的是建立连接后从服务器读取到可用资源所用的时间
ConnectTimeout: 5000
@Configuration
public class FeignConfig {
@Bean
public Logger.Level feignLogger(){
return Logger.Level.FULL;
}
}
logging:
level:
com.wu.scf.service.OpenfeignPaymentService: debug