springboot 整合nacos 和 dubbo

springboot 整合nacos 和 dubbo,第1张

springboot 整合nacos 和 dubbo 导入依赖
    
        org.apache.dubbo
        dubbo-spring-boot-starter
        2.7.8
    
    
    
        org.apache.dubbo
        dubbo-registry-nacos
        2.7.8
    
配置

生产者

server:
  port: 8082
spring:
  #数据库连接配置
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.01:3306/hmily_stock?useUnicode=true&characterEncoding=utf8
    username: root
    password:
  application:
    name: nacos-stock
#  cloud:
#    nacos:
#      config:
#        server-addr: 127.0.0.1:8848
#mybatis的相关配置
mybatis-plus:
  #mapper配置文件
  mapper-locations: classpath:com/hmily/test/mapper/xml/*.xml
  type-aliases-package: com.zhg.demo.mybatis.entity
  #开启驼峰命名
  configuration:
    map-underscore-to-camel-case: true

dubbo:
  application:
    name: nacos-stock
  protocol:
    port: 20880 
    name: dubbo
  registry:
    address: nacos://localhost:8848
  scan:
#    base-packages: service.StockService
    base-packages: com.stock.service.impl #这个需要配置到impl的包
  consumer:
    check: false  关闭这个可以防止启动的时候报错  一般生产者不会出现

消费者:

server:
  port: 8081
spring:
  #数据库连接配置
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.01:3306/hmily_order?useUnicode=true&characterEncoding=utf8
    username: root
    password:
  application:
    name: nacos-order
#  cloud:
#    nacos:
#      config:
#        server-addr: 127.0.0.1:8848
#mybatis的相关配置
mybatis-plus:
  #mapper配置文件
  mapper-locations: classpath:com/hmily/test/mapper/xml/*.xml
  type-aliases-package: com.zhg.demo.mybatis.entity
  #开启驼峰命名
  configuration:
    map-underscore-to-camel-case: true

dubbo:
  application:
    name: nacos-order
#  protocol:
#    port: 20881
#    name: dubbo
  registry:
    address: nacos://localhost:8848
#  scan:
#    base-packages: service.StockService 
  consumer:
    check: false # 启动时不检查 consumer
注解
  • 首先生产者 需要用到的注解有 @EnableDubbo -开启Dubbo 功能 @DubboService(version = "1.0") -暴露服务 使用@Service 这个版本已经过时了

  • 消费者 @DubboReference

使用xml配置的方式

provider.xml




    
    

    
    

    
    

    
    
    
    
    

consumer.xml




    
    

    
    

    
    

使用xml的方式 需要采用注解 再启动类引入 如:

@SpringBootApplication
@EnableDubbo
@importResource({"classpath:provider.xml"})
public class StockApplication {
    public static void main(String[] args) {
        SpringApplication.run(StockApplication.class, args);
    }
}

再将yml中刚配置的相关配置注释

部分测试源码:https://gitee.com/x1376646336/spring_nacos_dubbo

注意:本文归作者所有,未经作者允许,不得转载

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/zaji/5664979.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存