Loading

Dubbo:服务提供者、消费者相关配置

1.架构

 

2.提供者配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
 
    <!--指定当前服务的名字,不要与别的服务同名-->
    <dubbo:application name="dubbo-server-provider"/>
    <!--指定注册中心位置-->
    <dubbo:registry address="zookeeper://192.168.10.132:2181"/>
    <!--指定通信规则(协议/端口)-->
    <dubbo:protocol name="dubbo" port="10000"/>
    <!--暴露服务 ref:指向服务真正的实现对象-->
    <dubbo:service interface="com.wj.UserService" ref="userServiceImpl"/>
 
    <!--服务实现-->
    <bean id="userServiceImpl" class="com.wj.service.impl.UserServiceImpl"/>
</beans>

 service实现:

注意实体类需要实现Serializable接口,否则会报错,因为远程调用过程有序列化和反序列化过程。

1
2
3
4
5
6
7
8
9
public class UserServiceImpl implements UserService {
 
    @Override
    public List<User> getAll() {
        User user1 = new User(1, "张三", 12, "北京");
        User user2 = new User(2, "李四", 13, "北京");
        return Arrays.asList(user1, user2);
    }
}

Main方法:

1
2
3
4
5
6
7
public class ProviderMain {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        context.start();
        System.in.read();
    }
}

  

3.消费方配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.3.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
 
    <!--消费者服务名-->
    <dubbo:application name="dubbo-server-consumer"/>
    <!--注册中心地址-->
    <dubbo:registry address="zookeeper://192.168.10.132:2181"/>
    <!--声明需要远程调用的远程服务接口,生成远程服务代理-->
    <dubbo:reference interface="com.wj.UserService" id="userServiceImpl"/>
 
    <context:component-scan base-package="com.wj"/>
 
</beans>

 消费者类:

1
2
3
4
5
6
7
8
@Controller
public class UserController {
    @Autowired
    UserService userService;
    public List<User> getAll(){
        return userService.getAll();
    }
}

Main方法:

1
2
3
4
5
6
7
8
9
10
11
public class ConsumerMain {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserController bean = context.getBean(UserController.class);
        List<User> users = bean.getAll();
        for (User user : users) {
            System.out.println(user);
        }
        System.in.read();
    }
}

 4.分别运行两个main方法 

消费方打印结果:

 

 dubbo admin上服务调用关系:

代码已上传至github

posted @   秋风飒飒吹  阅读(2209)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示