dubbo-spring-cloud-Demo

  • 服务提供者 and 消费者 添加pom依赖
<dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.7.8</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-dubbo</artifactId>
<version>2.7.8</version>
        </dependency>
  • 服务提供者配置properties dubbo相关配置
dubbo.protocol.name=dubbo #协议名称
dubbo.protocol.port=20882 #对外暴露端口 可以配置-1 自动生成端口

dubbo.scan.base-packages=com.xlw.service.impl #扫描服务所在的包

spring.cloud.nacos.discovery.server-addr=192.168.25.5:8848 #nacos注册中心地址 
  • 服务消费者配置properties dubbo相关配置
spring.cloud.nacos.discovery.server-addr=192.168.25.5:8848 #如果不需要对外提供服务,只需要配置注册中心地址

 

  • 服务提供者相关代码demo
package com.xlw.service.impl;

import com.xlw.service.LoginService;
import org.apache.dubbo.config.annotation.DubboService;


/**
 * @author xlw
 * @datetime 17:05
 * LoginServiceImpl
 */
//在dubbo2.7.7之前使用的使用@Service(服务发布)@Reference(服务引用),在2.7.8被替代@DubboService
@DubboService
public class LoginServiceImpl implements LoginService {
    @Override
    public String sayHello() {

        return "hello boss";
    }
}
  • 服务消费者相关代码demo
package com.xlw.consumer;

import com.xlw.service.LoginService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class MySpringCloudDubboConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(MySpringCloudDubboConsumerApplication.class, args);
    }

//在dubbo2.7.7之前使用的使用@Service(服务发布)@Reference(服务引用),在2.7.8被替代@DubboReference
    @DubboReference
    LoginService loginService;
    @GetMapping("/get")
    public String get(){

        return loginService.sayHello();
    }

}

dubbo与spring boot集成

dubbo如果存在多注册中心,与spring cloud进行集成会需要关闭相关默认配置,比较繁琐,与spring boot集成会更加灵活

  • 提供者and消费者引入相关pom依赖
<dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.8</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
            <version>1.3.2</version>
        </dependency>

        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper</artifactId>
            <version>2.7.8</version>
        </dependency>

 

  • 服务生产者配置多注册中心properties 
dubbo.protocol.name=dubbo
dubbo.protocol.port=-1

#注册中心1 nacos
dubbo.registries.shanghai.address=nacos://192.168.25.5:8848

#注册中心1 zookeeper
dubbo.registries.beijing.address=zookeeper://192.168.25.5:2181
dubbo.registries.beijing.timeout=100000 #zookeeper需要配置一定时间的超时时间(有的10足够,按照自己所需配置),否则可能导致连接不上。
  • 发布服务相关代码demo
package com.xlw.dubbo.service.impl;

import com.xlw.service.SayHelloService;
import org.apache.dubbo.config.annotation.DubboService;

/**
 * @author xlw
 * @datetime 19:06
 * SayHelloServiceImpl
*可以发布到多个注册中心上
 */
@DubboService(registry = {"shanghai","beijing"})
public class SayHelloServiceImpl implements SayHelloService {
    @Override
    public String sayHello() {

        return "Hello Boss";
    }
}
  • 服务消费者properties
#注册中心1 nacos
dubbo.registries.shanghai.address=nacos://192.168.25.5:8848

#注册中心1 zookeeper
dubbo.registries.beijing.address=zookeeper://192.168.25.5:2181
dubbo.registries.beijing.timeout=100000
  • 服务消费者引用服务相关代码demo
package com.xlw.dubbo.consumer;

import com.xlw.service.SayHelloService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class SpringBootDuboConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootDuboConsumerApplication.class, args);
    }

    @DubboReference
    SayHelloService sayHelloService;

    @GetMapping("/say")
    public String say(){

        return sayHelloService.sayHello();
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值