这一章简单搭建一个分布式服务:
1、Dubbo简介:Dubbo 是一个分布式服务框架,是阿里巴巴开源项目。
Dubbo 致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。简单的说,Dubbo就是个服务框架,如果没有分布式的需求,其实是不需要用的,只有在分布式的时候,才有Dubbo这样的分布式服务框架的需求,并且本质上是个服务调用的东东,说白了就是个远程服务调用的分布式框架。
dubbo流程图如下:
节点角色说明:
Provider: 暴露服务的服务提供方。
Consumer: 调用远程服务的服务消费方。
Registry: 服务注册与发现的注册中心。
Monitor: 统计服务的调用次调和调用时间的监控中心。
Container: 服务运行容器。
调用关系说明:
服务容器负责启动,加载,运行服务提供者。
服务提供者在启动时,向注册中心注册自己提供的服务。
服务消费者在启动时,向注册中心订阅自己所需的服务。
注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。
2、Zookeeper注册中心:
官方推荐使用 zookeeper 注册中心。注册中心负责服务地址的注册与查找,相当于目录服务,服务提供者和消费者只在启动时与注册中心交互,注册中心不转发请求,压力较小。
Zookeeper 是 Apacahe Hadoop 的子项目,是一个树型的目录服务,支持变更推送,适合作为Dubbo 服务的注册中心,工业强度较高,可用于生产环境。
3、入门案例:
1、整个项目架构
2、Provider:
pom.xml:
org.springframework
spring-context
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-aspects
${spring.version}
org.springframework
spring-jms
${spring.version}
org.springframework
spring-context-support
${spring.version}
com.alibaba
dubbo
2.6.4
org.apache.zookeeper
zookeeper
3.4.6
org.apache.curator
curator-recipes
4.0.1
log4j
log4j
1.2.17
io.netty
netty-all
4.1.32.Final
org.apache.maven.plugins
maven-compiler-plugin
2.3.2
1.8
1.8
org.apache.tomcat.maven
tomcat7-maven-plugin
2.2
8083
/
applicationContext-server.xml:
web.xml
contextConfigLocation
classpath:applicationContext*.xml
org.springframework.web.context.ContextLoaderListener
UserService.java
public interface UserService {
String getName();
}
UserServiceImpl。java
import com.alibaba.dubbo.config.annotation.Service;
import com.test.service.UserService;
@Service
public class UserServiceImpl implements UserService {
@Override
public String getName() {
return "hello world";
}
}
注:@Service导包为:import com.alibaba.dubbo.config.annotation.Service 千万不要导错包了
3、Consumer:
pom.xml
org.springframework
spring-context
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-aspects
${spring.version}
org.springframework
spring-jms
${spring.version}
org.springframework
spring-context-support
${spring.version}
com.alibaba
dubbo
2.6.4
org.apache.zookeeper
zookeeper
3.4.10
org.apache.curator
curator-recipes
4.0.1
log4j
log4j
1.2.17
io.netty
netty-all
4.1.32.Final
org.apache.maven.plugins
maven-compiler-plugin
2.3.2
1.8
1.8
org.apache.tomcat.maven
tomcat7-maven-plugin
8082
/
SpringMVC.xml
web.xml
contextConfigLocation
classpath:SpringMVC.xml
org.springframework.web.context.ContextLoaderListener
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:SpringMVC.xml
springmvc
*.do
UserService.java
public interface UserService {
String getName();
}
UserController.java
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.test.service.UserService;
@Controller
@RequestMapping("/user")
public class UserController {
/**
* TODO 注意:@Reference 包信息是 com.alibaba.dubbo.config.annotation.Reference;
*/
@Reference
private UserService userService;
@RequestMapping("showName")
@ResponseBody
public String showName(){
return userService.getName();
}
}
注:@Reference 包信息是 com.alibaba.dubbo.config.annotation.Reference 千万不要导错包了