Spring cloud day(4) ribbon和feign
一、ribbon
1.1 ribbon概念
1.2 负载均衡
- 集中式LB
- 进程内LB
1.3 ribbon架构
1.4 ribbom pom
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-ribbon -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
<version>2.2.10.RELEASE</version>
</dependency>
1.5 restemplate 中的object和entity
- entity举例
public CommenResult<Payment> getPaymentEntity(@PathVariable("id") int id){
ResponseEntity<CommenResult> forEntity = restTemplate.getForEntity(PAYMENT_URL + "/getPayment?id=" + id, CommenResult.class);
if(forEntity.getStatusCode().is2xxSuccessful()){
return forEntity.getBody();
}else {
return new CommenResult<>(444,"failed");
}
}
1.6 IRule
- 根据特定的算法从服务列表中选择一个要访问的服务
1.7 ribbon规则替换
- 步骤
- 注意
替换规则不能与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
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>CommModule</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- 引入自己定义的api通用包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-devtools</artifactId>-->
<!-- <scope>runtime</scope>-->
<!-- <optional>true</optional>-->
<!-- </dependency>-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
- 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<Payment> getPaymentById(@PathVariable("id") int id) {
return paymentService.getPayment(id);
}
}
- 负载均衡
feign自身集合了
2.4 feign过程图
- feign把远程提供的服务在本地端包装到接口中,自己就可以调用
2.5 feign超市控制
- 测试超时控制,服务端8081业务
@GetMapping("/payment/timeout")
public String testTimeOut() throws InterruptedException {
TimeUnit.SECONDS.sleep(3);
return serverport;
}
- 相关yml配置
ribbon:
#指的是建立连接所用的时间,适用于网络状况正常的情况下, 两端连接所用的时间
ReadTimeout: 5000
#指的是建立连接后从服务器读取到可用资源所用的时间
ConnectTimeout: 5000
2.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
三、 feign案例
3.1 调用方
3.3.1 pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
3.3.2 @EnableFeignClients
- 开启feign功能
@SpringBootApplication
@ComponentScan({"com.atguigu.guli"})
@EnableFeignClients
@EnableDiscoveryClient
public class ServiceEduApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceEduApplication.class, args);
}
}
3.3.3 远程接口本地化
- value:目标服务的名称
@Service
@FeignClient(value = "service-cms", fallback = TestFeignFall.class)
public interface TestLogService {
@GetMapping("/test/log/first")
public String getSth();
@PostMapping("/test/log/second")
public String setSth(String name);
}
- fallback:就是实现代理接口,当远程接口出错或者宕机都会启用里面的方法。
@Service
public class TestFeignFall implements TestLogService {
@Override
public String getSth() {
return "error get";
}
@Override
public String setSth(String name) {
return "error set";
}
}