搭建SpringBoot项目dubbo-demo

搭建SpringBoot项目dubbo-demo

  • api 建立接口
  • provider 实现接口,
  • consumer 调用接口。

Api模块

DemoService
package dubbodemoapi;

public interface DemoService {
    String sayHello(String name);
}
DubboDemoApiApplication
package dubbodemoapi;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

将Api模块打成jar,在服务提供者和服务消费者的pom中引入

  1. Api模块中打开maven → Lifecycle → package,target / xxxapi.jar

  2. 复制到,桌面创建xxxapi / 0.0.1-SNAPSHOT / xxxapi.jar

  3. cmd 命令行运行

    示例:

    mvn install:install-file -Dfile=C:\Users\yh\Desktop\dubbo-demo-api\0.0.1-SNAPSHOT\dubbo-demo-api-0.0.1-SNAPSHOT.jar -DgroupId=com.example -DartifactId=dubbo-demo-api -Dversion=0.0.1-SNAPSHOT -Dpackaging=jar
    

    -DgroupId= : 设置项目代码的包名(一般用组织名)
    -DartifactId= : 设置项目名或模块名
    -Dversion=1.0.0 : 版本号
    -Dpackaging=jar : 什么类型的文件(jar包)
    -Dfile=<myfile.jar> : 指定jar文件路径与文件名(同目录只需文件名)

Provider模块

pom.xml+
<!-- springboot dubbo starter -->
        <dependency>
            <groupId>io.dubbo.springboot</groupId>
            <artifactId>spring-boot-starter-dubbo</artifactId>
            <version>1.0.0</version>
        </dependency>
        <!-- api dependence -->
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>dubbo-demo-api</artifactId>
        </dependency>
DemoServiceImpl
package dubbodemoprovider;

import com.alibaba.dubbo.config.annotation.Service;
import dubbodemoapi.DemoService;

@Service
public class DemoServiceImpl implements DemoService {
    public String sayHello(String name) {
        return "Hello, " + name + " (from Spring Boot)";
    }
}
Provider
package dubbodemoprovider;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Provider {
    public static void main(String[] args) {
        SpringApplication.run(Provider.class,args);
    }
}
application.properties
spring.dubbo.application.name=demo-provider
#使用广播的注册方式,
#如果有Can't assign address异常需要加vm参数:
#-Djava.net.preferIPv4Stack=true
spring.dubbo.registry.address=multicast://224.5.6.7:1234
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
spring.dubbo.scan=dubbodemoprovider

Consumer模块

pom.xml+
<!-- springboot dubbo starter -->
        <dependency>
            <groupId>io.dubbo.springboot</groupId>
            <artifactId>spring-boot-starter-dubbo</artifactId>
            <version>1.0.0</version>
        </dependency>
        <!-- api dependence -->
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>dubbo-demo-api</artifactId>
        </dependency>
ConsumerController
package dubbodemoconsumer;

import com.alibaba.dubbo.config.annotation.Reference;
import dubbodemoapi.DemoService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConsumerController {
    @Reference
    private DemoService demoService;

    @RequestMapping("/sayHello")
    public String sayHello(@RequestParam String name){
        return  demoService.sayHello(name);
    }
}
Main
package dubbodemoconsumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class,args);
    }
}
application.properties
server.port=8080
#dubbo config
spring.dubbo.application.name=demo-consumer
#使用广播的注册方式,
#如果有Can't assign address异常需要加vm参数:
#-Djava.net.preferIPv4Stack=true
spring.dubbo.registry.address=multicast://224.5.6.7:1234
spring.dubbo.scan=dubbodemoconsumer
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值