Dubbo是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和 Spring框架无缝集成。
Dubbo是一款高性能、轻量级的开源Java RPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。
搭建一个dubbo入门项目
1、服务提供者
ProviderDemo
package com.gg.demo;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ProviderDemo {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext ioc =
new ClassPathXmlApplicationContext("classpath:dubbo-provider.xml");
ioc.start();
System.out.println("server start.....");
System.in.read();
}
}
Student
package com.gg.entity;
import java.io.Serializable;
public class Student implements Serializable {
private String sid ;
private String sname ;
private Integer sage ;
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public Integer getSage() {
return sage;
}
public void setSage(Integer sage) {
this.sage = sage;
}
@Override
public String toString() {
return "Student [sid=" + sid + ", sname=" + sname + ", sage=" + sage + "]";
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String sid, String sname, Integer sage) {
super();
this.sid = sid;
this.sname = sname;
this.sage = sage;
}
}
StudentServiceImpl
package com.gg.service.impl;
//import org.springframework.stereotype.Service;
import com.alibaba.dubbo.config.annotation.Service;
import com.gg.entity.Student;
import com.gg.service.StudentService;
//@Service("studentServiceImpl")
@Service(interfaceName="com.gg.service.StudentService")
// 1.把当前impl放到ioc中, 2. 声明需要暴露的服务接口
// @Service = @Service(Spring的) + <dubbo:service>
public class StudentServiceImpl implements StudentService {
@Override
public Student selectStudentBySid(String sid) {
return new Student(sid+"[provider1]", "张三", 8);
}
}
StudentService
package com.gg.service;
import com.gg.entity.Student;
public interface StudentService {
Student selectStudentBySid(String sid);
}
dubbo-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:p="http://www.springframework.org/schema/p"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
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.1.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
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<context:component-scan base-package="com.gg"></context:component-scan>
<!-- 开启dubbo注解的支持 ,自带扫描器-->
<dubbo:annotation package="com.gg.service"/>
<!-- 告知注册中心,哪个应用发布了该服务 -->
<dubbo:application name="dubbo-student-provider-1" />
<!-- 把服务暴露给 注册中心的地址 , multicast 是局域网广播模拟注册中心-->
<!--address="zookeeper://localhost:2181"-->
<!-- <dubbo:registry address="multicast://224.1.1.1:1111"></dubbo:registry> -->
<dubbo:registry address="zookeeper://localhost:2181"></dubbo:registry>
<!-- 用于和消费者通信指定的协议、端口 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<!--<dubbo:service registry="N/A" interface="com.gg.service.StudentService" ref="studentServiceImpl" />-->
<!--<dubbo:service interface="com.gg.service.StudentService" ref="studentServiceImpl" />-->
</beans>
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gg</groupId>
<artifactId>dubbo-student-provider-1</artifactId>
<version>1.0</version>
<dependencies>
<!-- dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.6</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.10</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.5</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.32.Final</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.8.0</version>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.16.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.spring</groupId>
<artifactId>spring-context-support</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.16.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
2、服务消费者
StudentController
package com.gg.controller;
import org.springframework.stereotype.Controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.gg.entity.Student;
import com.gg.service.StudentService;
@Controller
public class StudentController {
// @Autowired
//@Reference(url="dubbo://localhost:20880")
@Reference
//1. 远程帮助你生成studentService的代理对象, 2. 自动装配
//@Reference = <dubbo:reference> +@Autowired
private StudentService studentService;
public void GetStudentById(String sid) {
Student stu = studentService.selectStudentBySid(sid);
System.out.println(stu);
}
}
ConsumeDemo
package com.gg.demo;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.gg.controller.StudentController;
import com.gg.entity.Student;
import com.gg.service.StudentService;
public class ConsumeDemo {
public static void main(String[] args) {
ClassPathXmlApplicationContext ioc =
new ClassPathXmlApplicationContext("classpath:dubbo-consume.xml");
// StudentService proxy = ioc.getBean(StudentService.class);
StudentController controller = ioc.getBean(StudentController.class);
controller.GetStudentById("11111111");
}
}
3、启动