- 一、ribbon
- 1.1 ribbon概念
- 1.2 负载均衡
- 1.3 ribbon架构
- 1.4 ribbom pom
- 1.5 restemplate 中的object和entity
- 1.6 IRule
- 1.7 ribbon规则替换
- 二、feign
- 2.1 概念
- 2.2 Feign和openFeign
- 2.3 openFeign的调用步骤
- 2.4 feign过程图
- 2.5 feign超市控制
- 2.6 日志增强
- 集中式LB
- 进程内LB
1.5 restemplate 中的object和entityorg.springframework.cloud spring-cloud-starter-netflix-ribbon2.2.10.RELEASE
- entity举例
public CommenResult1.6 IRulegetPaymentEntity(@PathVariable("id") int id){ ResponseEntity forEntity = restTemplate.getForEntity(PAYMENT_URL + "/getPayment?id=" + id, CommenResult.class); if(forEntity.getStatusCode().is2xxSuccessful()){ return forEntity.getBody(); }else { return new CommenResult<>(444,"failed"); } }
- 根据特定的算法从服务列表中选择一个要访问的服务
- 步骤
- 注意
替换规则不能与componentscan注解扫描到的在同一个包或者子包中 - 替换配置类
@Configuration
public class ruleConfig {
@Bean
public IRule myRule(){
//定义为随机
return new RandomRule();
}
}
- 主启动类添加注解:@RibbonClient
- name:服务名称,configuration :规则配置类
@SpringBootApplication
@EnableEurekaClient
@RibbonClient(name = "payment-services-8002",configuration = ruleConfig.class)
public class OrderBoot {
public static void main(String[] args) {
SpringApplication.run(OrderBoot.class, args);
}
}
二、feign
2.1 概念
2.2 Feign和openFeign
2.3 openFeign的调用步骤
- pom
org.example CommModule${project.version} org.springframework.cloud spring-cloud-starter-openfeignorg.springframework.cloud spring-cloud-starter-netflix-eureka-clientorg.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-actuatororg.projectlombok lomboktrue org.springframework.boot spring-boot-starter-testtest
- yml
server:
port: 80
#spring:
# application:
# name: cloud-order-service
eureka:
client:
register-with-eureka: false # 不注册进eureka服务中心
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
- 主启动类:@EnableFeignClients
@SpringBootApplication
@EnableFeignClients // 启动feign
public class feignMain {
public static void main(String[] args) {
SpringApplication.run(feignMain.class, args);
System.out.println("启动成功");
}
}
- 业务类:与远程接口提供的业务对应,@FeignClient
@Component
//微服务名称
@FeignClient(value = "PAYMENT-SERVICE")
public interface PaymentService {
//服务端的接口
@GetMapping("/getPayment")
public CommenResult getPayment(@RequestParam("id")int id);
}
- controller
@RestController
@RequestMapping("consumer")
@Slf4j
public class feignController{
@Resource
private PaymentService paymentService;
@GetMapping(value = "/feign/payment/get/{id}")
public CommenResult getPaymentById(@PathVariable("id") int id) {
return paymentService.getPayment(id);
}
}
- 负载均衡
feign自身集合了
- feign把远程提供的服务在本地端包装到接口中,自己就可以调用
- 测试超时控制,服务端8081业务
@GetMapping("/payment/timeout")
public String testTimeOut() throws InterruptedException {
TimeUnit.SECONDS.sleep(3);
return serverport;
}
- 相关yml配置
ribbon: #指的是建立连接所用的时间,适用于网络状况正常的情况下, 两端连接所用的时间 ReadTimeout: 5000 #指的是建立连接后从服务器读取到可用资源所用的时间 ConnectTimeout: 50002.6 日志增强
- 日志级别
- 配置类
@Configuration
public class feignConfig {
@Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
}
- 对应yml配置
logging:
level:
# feign日志以 debug 级别监控 com.atguigu.springcloud.service.PaymentFeignService 接口
com.atguigu.springcloud.service.PaymentFeignService: debug



