Loading

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

1.架构

 

2.提供者配置

<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接口,否则会报错,因为远程调用过程有序列化和反序列化过程。

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方法:

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

  

3.消费方配置

<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>

 消费者类:

@Controller
public class UserController {
    @Autowired
    UserService userService;
    public List<User> getAll(){
        return userService.getAll();
    }
}

Main方法:

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 @ 2020-05-26 11:03  秋风飒飒吹  阅读(2204)  评论(0编辑  收藏  举报