1. 程式人生 > 其它 >dubbo-spring-cloud-Demo

dubbo-spring-cloud-Demo

技術標籤:Spring cloudSpring Bootspring bootrpc

  • 服務提供者 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();
    }
}