springboot 整合nacos 和 dubbo

导入依赖

<!--dubbo-->
    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo-spring-boot-starter</artifactId>
        <version>2.7.8</version>
    </dependency>
    <!--dubbo 注册nacos-->
    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo-registry-nacos</artifactId>
        <version>2.7.8</version>
    </dependency>

写配置

生产者

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

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        http://dubbo.apache.org/schema/dubbo        http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="nacos-stock"  />

    <!-- 使用multicast广播注册中心暴露服务地址 -->
    <dubbo:registry address="nacos://localhost:8848" />

    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />

    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="service.StockService" ref="stockService" />
    <!-- 和本地bean一样实现服务 -->
    <bean id="stockService" class="com.stock.service.impl.StockServiceImpl" >
    </bean>
</beans>

consumer.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        http://dubbo.apache.org/schema/dubbo        http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="nacos-order"  />

    <!-- 使用multicast广播注册中心暴露服务地址 -->
    <dubbo:registry address="nacos://localhost:8848" />

    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <dubbo:reference id="demoService" interface="service.StockService" />
</beans>

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

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

再将yml中刚配置的相关配置注释 QQ截图20201012172121.png

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值