Spring Cloud Alibaba服务治理Nacos

分布式服务治理 什么是Nacos

Nacos相当于Spring Cloud组件中的注册中心eureka服务总线Bus配置中心config
Nacos是阿里巴巴开源的服务注册中心和配置中心

Dubbo与OpenFeign的区别

Dubbo底层使用RPC进行远程调用,保持了长连接,高性能
OpenFeign底层采用http通信协议,属于短链接
Dubbo具有性能上的优势,OpenFeign使用起来更加便携

使用Spring Cloud Alibaba的注意事项

在逻辑工程pom.xml需要进行统一版本的规划,选择合适的版本,避免版本不一致出现一些意想不到的bug

使用OpenFeign进行远程调用

OpenFeign是采用接口+注解的形式进行远程调用的,而且OpenFeign还支持SpringMVC的注解
代码示例:
@EnableFeignClients扫描feign的注解
@EnableDiscoveryClient开启服务注册和发现

主启动类

@SpringBootApplication
@EnableDiscoveryClient
@Slf4j
@EnableFeignClients
public class OrderFeign80 {

    public static void main(String[] args) {
        SpringApplication.run(OrderFeign80.class,args);
        log.info("************* 远程调用启动成功 *****************");
    }

}

远程调用接口_兜底方法fallback

fallback为兜底方法,当超过读取时间或者远程调用失败时会触发该方法

@Service
@FeignClient(value = "cloud-payment",fallback = IFeignServiceImpl.class)
public interface IFeignService {

    @GetMapping("/payment/index")
    String index();

}

@Component
public class IFeignServiceImpl implements IFeignService {
    @Override
    public String index() {
        return "服务器繁忙,请稍后重试...";
    }
}

OpenFeign进行远程调用时需要引入sentinel的起步依赖并在配置文件中开启OpenFeign对sentinel的支持

 <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
# feign配置
feign:
  sentinel:
    enabled: true # 开启openfeign对sentinel的支持
  client:
    config:
      default:
        connectTimeout: 1000
        readTimeout: 2000

控制层

@RestController
@RequestMapping("/feign")
public class FeignController {

    @Autowired
    private IFeignService iFeignService;

    @GetMapping("/index")
    public String feign(){
        String index = iFeignService.index();
        return index;
    }

}

使用Dubbo进行远程调用

需要创建一个公共接口的模块,在使用Dubbo时生产者和消费者是不能相互引用的

服务生产者


@DubboService(timeout = 5000,methods = {@Method(name = "index",retries = 2)},
        // 快速失败模式 调用只执行一次失败则立即报错
cluster = "failfast")
public class PaymentServiceImpl implements IPaymentService {
    @Override
    public String index() {
        return "支付成功";
    }
}

服务消费者

在Dubbo中实现服务降级需要使用mock值为接口全路径

@Service
public class OrderService {

    @DubboReference(mock = "com.itbaizhan.dubbo.service.fallback.OrderServiceFallBack")
    private IPaymentService iPaymentService;

    public String order(){
        String index = iPaymentService.index();
        return index;
    }

}
public class OrderServiceFallBack implements IPaymentService {
    @Override
    public String index() {
        return "服务器繁忙,请稍后重试...";
    }
}

yml文件

server:
  port: 80
spring:
  main:
    allow-circular-references: true
    allow-bean-definition-overriding: true
  application:
    name: dubbo-consumer-order
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.66.121:8848

dubbo:
  cloud:
    # 订阅所有消费者
    subscribed-services: "*"
  registry:
    address: nacos://192.168.66.121:8848
    timeout: 10000
  protocol:
    name: dubbo
    port: -1


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序猿晓晓

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值