Dubbo+ZooKeeper简单搭建

一、公共模块项(dbo-core)

包含有:实体类、对外Service接口

1. 实体类

package org.yf.entity;
import java.io.Serializable;

public class Person implements Serializable{
    private Integer id;
    private String name;
    private String address;
    构造方法/getter()/setter()/toString()
}

2. 对外接口PersonService

package org.yf.service;
import org.yf.entity.Person;

public interface PersonService {
    public Person selectPersonById(Integer id);
}

二、生产者模块项

包含有:Service实现、orders-provider.xml生产者配置文件、spring-mvc.xml以及生产者启动文件

1. PersonService接口实现

package org.yf.service.impl;

import org.yf.entity.Person;
import org.yf.service.PersonService;
import com.alibaba.dubbo.config.annotation.Service;

@Service
public class PersonServiceImpl implements PersonService{
    public Person selectPersonById(Integer id) {
        Person person = new Person();
        person.setId(123);
        if(123 == person.getId()) {
            person.setName("张三");
            person.setAddress("aaa");
        }
        return person;
    }
}

2. orders-provider.xml

<?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="orders-provider" />
	
	<!-- 组播注册中心 ,用于快速体验入门-->
	<!-- <dubbo:registry address="multicast://224.5.6.7:1234"/> -->
	<!-- 使用zookeeper注册中心暴露服务地址 -->  
	<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry>
	
	<!-- 用dubbo协议在20880端口暴露服务 --> 
	<dubbo:protocol name="dubbo" port="20880"/>
	
	<!-- 采用注解的形式,所以不用使用<dubbo:service> -->
	<!-- <dubbo:service interface="org.yf.service.PersonService" class="org.yf.service.impl.PersonServiceImpl"/> -->
	<!-- 扫描注解包路径,多个包用逗号分隔,不填pacakge表示扫描当前ApplicationContext中所有的类 -->
	<dubbo:annotation package="org.yf.service.impl"></dubbo:annotation>
</beans>

3. spring-mvc.xml

<?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:context="http://www.springframework.org/schema/context"
	   xmlns:mvc="http://www.springframework.org/schema/mvc"
	   xmlns:p="http://www.springframework.org/schema/p"
	   xmlns:util="http://www.springframework.org/schema/util"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

	<!-- 这里配置MyBatis持久层相关的配置 -->
	
	<!-- dubbo 配置文件 -->
	<!-- <import resource="classpath:/META-INF/spring/orders-provider.xml" /> -->
</beans>

4. provider生产者启动文件

package test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Provider {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] {"META-INF/spring/orders-provider.xml"});
        context.start();
        System.in.read();
    }
}

三、消费者模块项

包含:Controller、user-consumer.xml消费者配置文件、spring-mvc.xml以及消费者启动文件

1. Controller

@Controller
public class PersonController {
	//注解使用dubbo服务端服务
	@Reference
	private PersonService personService;
	
	public Person selectPersonById(Integer id) {
	    System.out.println("通过Spring方式去远程调用");
	    Person person = personService.selectPersonById(123);
	    return person;
	}
}

2. user-consumer.xml消费者配置文件

<?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="user-consumer"/>
	
	<!-- 组播注册中心 ,用于快速体验入门-->
	<!-- <dubbo:registry address="multicast://224.5.6.7:1234"/> -->
	<!-- zookeeper注册中心,官方推荐 -->
	<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry>
	
	<!-- 采用@Reference注解的形式获取服务,所以不用使用<dubbo:reference -->
	<!-- <dubbo:reference id="personService" interface="org.yf.service.PersonService"/> -->
	<dubbo:annotation package="org.yf"></dubbo:annotation>
</beans>

3. spring-mvc.xml配置文件

<?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:context="http://www.springframework.org/schema/context"
	   xmlns:mvc="http://www.springframework.org/schema/mvc"
	   xmlns:p="http://www.springframework.org/schema/p"
	   xmlns:util="http://www.springframework.org/schema/util"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

	<!-- 这里配置SpringMVC相关的配置 -->
	
	<!-- dubbo 配置文件 -->
	<!-- <import resource="classpath:/META-INF/spring/user-consumer.xml" /> -->
</beans>

4. 消费者启动文件

public class Consumer {
    public static void main(String[] args) throws Exception {
    	// ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/user-consumer.xml","META-INF/spring/spring-mvc.xml"});
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/user-consumer.xml"});
        context.start();
        
        //user-consumer.xml中使用<dubbo:reference id="personService" interface="XXX"/>才可从容器中获取
        /*PersonService demoService = (PersonService) context.getBean("personService");
        Person hello = demoService.selectPersonById(123);
        System.out.println(hello);*/
        
        PersonController personController = (PersonController) context.getBean("personController");
        Person person = personController.selectPersonById(123);
        System.out.println(person);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值