Springboot2整合Dubbo+zookeeper

2 篇文章 0 订阅
2 篇文章 0 订阅

上一篇环境搭建:https://blog.csdn.net/qq_41085151/article/details/107984842

1.添加pom依赖

<!-- springboot版本2.1.5 -->

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>


  <!--整合dubbo+zk-->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.7.0</version>
        </dependency>
        <!-- Zookeeper -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.9</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.12.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>2.12.0</version>
        </dependency>

2.添加配置

server.port=8093
dubbo.application.name=provider
dubbo.registry.address=zookeeper://192.168.221.150:2181
dubbo.scan.base-packages=com.example.springbootprovider.service

3.编写service,注册到zk中去。

public interface UserService {

    public String getUser();
}


@Component
@Service     //dubbo里的service
public class UserServiceImpl implements UserService {
    @Override
    public String getUser() {
        return "获取用户";
    }
}

4.运行服务,可以到dubbo-admin中查看

5.再同目录创建Module消费者项目,添加依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

      
        <!--整合dubbo-->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.7.0</version>
        </dependency>
        <!-- Zookeeper -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.9</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.12.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>2.12.0</version>
        </dependency>
        <!-- 添加提供者的依赖-->
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>springboot-provider</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

6.添加配置

server.port=8084
dubbo.application.name=consumer
dubbo.registry.address=zookeeper://192.168.221.150:2181

7.创建service调用

@Service  //spring的service注解
public class TestService {
    @Reference  //远程调用
     UserService userService;

    public void buyUser(){

        System.out.println(userService.getUser());
    }

}

8.配置控制层调用,打印台就会拿到数据了

@RestController
public class TestController {

    @Autowired
    UserService userService;

    @RequestMapping("/testdubbo")
    public void test(){
        userService.buyTicker();
    }
}

9.启动服务到dubbo-admin中查看

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值