springboot中使用dubbo(dubbo-spring-boot-starter:2.7.15,jdk:1.8)

淳于恺
2023-12-01

目录

springboot中使用dubbo

参考

生产者(先启动)

依赖

配置

服务类

启动类

消费者(要等生产者启动,不然报connection失败)

依赖

配置

服务类

RESTful接口

启动类


springboot中使用dubbo

参考

springboot-guide/springboot-dubbo.md at master · CodingDocs/springboot-guide · GitHub

生产者(先启动)

注:版本为3.0以上的dubbo已经无法被1.8jdk所支持了

依赖

<dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.15</version>
        </dependency>
        <!--ZooKeeper客户端-->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>5.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>5.2.1</version>
        </dependency>

配置

需要连接zookeeper,需要提供dubbo协议(name,port)

server.port=8333
​
spring.application.name=dubbo-provider
​
# Base packages to scan Dubbo Component: @org.apache.dubbo.config.annotation.Service
dubbo.scan.base-packages=com.jsq.service
​
# Dubbo Protocol
dubbo.protocol.name=dubbo
dubbo.protocol.port=12345
​
## Dubbo Registry
dubbo.registry.address=zookeeper://localhost:2181

服务类

服务接口

package com.jsq.service;
​
public interface HelloService {
    public String sayHello(String s);
}

服务实现类

package com.jsq.service;
​
import org.apache.dubbo.config.annotation.DubboService;
​
@DubboService(version = "1.0.0")//标记为dubbo的服务
public class HelloServiceImpl implements HelloService {
    @Override
    public String sayHello(String s) {
        return "Hello "+s;
    }
}

启动类

package com.jsq;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
​
@SpringBootApplication
@EnableAutoConfiguration
public class DubboProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(DubboProviderApplication.class, args);
    }
}

消费者(要等生产者启动,不然报connection失败)

依赖

 <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.15</version>
        </dependency>

配置

不需要对dubbo配置

# 配置端口
server.port=8330

服务类

服务接口(与生产者的相同)

package com.jsq.service;
​
public interface HelloService {
    public String sayHello(String s);
}

RESTful接口

package com.jsq.controller;
​
import com.jsq.service.HelloService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
​
@RestController
@RequestMapping("/dubbo")
public class HelloController {
​
    @DubboReference(version = "1.0.0", url = "dubbo://127.0.0.1:12345")//标记连接的dubbo中注册的服务
    private HelloService helloService;
​
    @GetMapping("/hello/{name}")
    public String hello(@PathVariable("name") String name) {
        String world = helloService.sayHello(name);
        return world;
    }
}

启动类

package com.jsq;
​
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
​
@SpringBootApplication
@EnableAutoConfiguration
public class DubboConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(DubboConsumerApplication.class, args);
    }
}

 类似资料: