spring 整合dubbo + zookper

1.单机安装zooker插件

 zooker 安装和IEDA插件

2.安装生成者

 

package com.unj.dubbotest.provider.impl;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Provider {

    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] { "applicationContext.xml" });
        context.start();
        System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟
    }
}

bean 配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        ">

    <!-- 具体的实现bean -->
    <bean id="demoService" class="com.unj.dubbotest.provider.impl.DemoServiceImpl" />

    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="xs_provider" />

    <!-- 使用multicast广播注册中心暴露服务地址 -->
    <!--<dubbo:registry address="multicast://224.5.6.7:1234" /> -->

    <!-- 使用zookeeper注册中心暴露服务地址 -即zookeeper的所在服务器ip地址和端口号 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />

    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />

    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.unj.dubbotest.provider.DemoService"
                   ref="demoService" />

</beans>

3.安装消费者

package com.alibaba.dubbo.demo.pp;

import java.util.List;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.unj.dubbotest.provider.DemoService;

public class Consumer {

    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] { "application.xml" });
        context.start();

        DemoService demoService = (DemoService) context.getBean("demoService");
        String hello = demoService.sayHello("hejingyuan");
        System.out.println(hello);

        List list = demoService.getUsers();
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                System.out.println(list.get(i));
            }
        }
        System.in.read();
    }

}

相应的配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        ">

    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
    <dubbo:application name="hjy_consumer" />

    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />

    <!-- 生成远程服务代理,可以像使用本地bean一样使用demoService -->
    <dubbo:reference id="demoService"
                     interface="com.unj.dubbotest.provider.DemoService" />

</beans>

4.运行结果:

4.1 启动zk ,查看服务注册状态

在IEDA的插件中可以看到服务的注册状态。

启动provider:

4.3 启动customer

4.4 结果

 

3.注意问题:

设置IEDA插件的时,需要关闭Zk 服务器,否则IEDA无法响应。

 

参考博文:

https://blog.csdn.net/qq_42556955/article/details/81184254

https://blog.csdn.net/sinat_15946141/article/details/79591212

https://blog.csdn.net/hejingyuan6/article/details/47403299

4.下载代码:

下载代码

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
下面是Spring Cloud Alibaba + Spring Boot + Dubbo + Nacos + Mybatis Plus + MySQL 项目搭建步骤: 1. 创建一个Spring Boot项目,并添加以下依赖: ```xml <!--Spring Boot依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--Dubbo依赖--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-dubbo</artifactId> <version>2.2.1.RELEASE</version> </dependency> <!--Nacos依赖--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>2.2.1.RELEASE</version> </dependency> <!--Mybatis Plus依赖--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.3</version> </dependency> <!--MySQL依赖--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.23</version> </dependency> ``` 2. 配置application.yml文件,添加以下内容: ```yaml spring: application: name: service-provider # 服务名称 datasource: url: jdbc:mysql://localhost:3306/db_example # 数据库URL username: root # 数据库用户名 password: root # 数据库密码 driver-class-name: com.mysql.cj.jdbc.Driver # 数据库驱动 mybatis-plus: mapper-locations: classpath:mapper/*.xml # Mybatis Plus的mapper文件路径 dubbo: application: name: dubbo-service-provider # Dubbo应用名称 registry: address: nacos://localhost:8848 # Nacos注册中心地址 protocol: name: dubbo # Dubbo协议名称 port: 20880 # Dubbo协议端口号 ``` 3. 创建一个数据模型类,例如: ```java @Data public class User { private Long id; private String username; private String password; private String email; } ``` 4. 创建一个Mapper类,例如: ```java @Mapper public interface UserMapper extends BaseMapper<User> { } ``` 5. 创建一个Service类,例如: ```java @Service public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService { } ``` 6. 创建一个Controller类,例如: ```java @RestController @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @GetMapping("/{id}") public User getUser(@PathVariable Long id) { return userService.getById(id); } } ``` 7. 在Nacos控制台中添加服务提供者的配置信息。 8. 启动服务提供者,并在Nacos控制台中查看服务是否注册成功。 9. 创建一个服务消费者项目,并添加以下依赖: ```xml <!--Spring Boot依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--Dubbo依赖--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-dubbo</artifactId> <version>2.2.1.RELEASE</version> </dependency> <!--Nacos依赖--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>2.2.1.RELEASE</version> </dependency> ``` 10. 配置application.yml文件,添加以下内容: ```yaml spring: application: name: service-consumer # 服务名称 dubbo: application: name: dubbo-service-consumer # Dubbo应用名称 registry: address: nacos://localhost:8848 # Nacos注册中心地址 ``` 11. 创建一个Service类,例如: ```java @Service public class UserServiceImpl implements UserService { @Reference private UserService userService; @Override public User getUser(Long id) { return userService.getById(id); } } ``` 12. 创建一个Controller类,例如: ```java @RestController @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @GetMapping("/{id}") public User getUser(@PathVariable Long id) { return userService.getUser(id); } } ``` 13. 启动服务消费者,访问http://localhost:8080/user/{id},查看服务是否调用成功。 以上就是Spring Cloud Alibaba + Spring Boot + Dubbo + Nacos + Mybatis Plus + MySQL项目搭建步骤。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

执于代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值