Dubbo生产者和消费者
生产者
引入依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javaee</groupId>
<artifactId>javaee-api</artifactId>
<version>5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.22</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-core -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.17.1-GA</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.31</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
<exclusions>
<exclusion>
<artifactId>log4j</artifactId>
<groupId>log4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>service接口
public interface DoSomeService {
public String doSome();
}service实现类
@Service("doSomeService")
public class DoSomeServiceImpl implements DoSomeService {
@Override
public String doSome() {
System.out.println("生产者提供的服务方法");
return "provider";
}
}配置
<context:component-scan base-package="com.dubbo"/>
<!--声明服务提供方-->
<dubbo:application name="provider"/>
<!--注册中心地址-->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!--dubbo服务端口-->
<dubbo:protocol name="dubbo" port="20880"/>
<!--服务注册-->
<dubbo:service interface="com.dubbo.service.DoSomeService" ref="doSomeService"/>测试类
public class DubboProviderTest {
public static void main(String[] args) throws IOException {
//加载配置文件:配置文件中通过SPring将Dubbo服务注册到注册中心当中去
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext-provider.xml");
System.out.println("服务已经发布!!!!!");
//阻塞
System.in.read();
}
}消费者
service接口
public interface DoSomeService {
public String doSome();
}配置
<context:component-scan base-package="com.dubbo"/>
<!--声明服务提供方-->
<dubbo:application name="consumer"/>
<!--注册中心地址-->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!--服务消费-->
<dubbo:reference interface="com.dubbo.service.DoSomeService" id="doSomeService"/>测试类
public class DubboConsumer {
public static void main(String[] args) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext-consumer.xml");
DoSomeService doSomeService = (DoSomeService)ctx.getBean("doSomeService");
doSomeService.doSome();
UserService userService = (UserService)ctx.getBean("userService");
userService.userServicePro();
}
}
相关推荐
shirleypaddy 2020-10-19
qingmumu 2020-10-19
敏敏张 2020-11-11
SCNUHB 2020-11-10
小木兮子 2020-11-11
WasteLand 2020-10-18
Cocolada 2020-11-12
杜鲁门 2020-11-05
Testingba工作室 2020-09-15
周公周金桥 2020-09-13
专注前端开发 2020-08-16
emagtestage 2020-08-16
heniancheng 2020-08-15
hanjinixng00 2020-08-12
小方哥哥 2020-08-09
83327712 2020-07-30
卖小孩的咖啡 2020-07-21