SOA架构
Tomcat极限: 单台Tomcat极限基本是可以同时抗住500个并发.
传统行业项目: 并发低, 数据量小, 不需要高可用, 例如: oa, erp, crm项目, 或者政府项目, 银行项目等.
互联网项目: 高并发, 大数据量, 高可用 例如: 京东, 百度, 淘宝, 天猫, 饿了么, 美团, 滴滴, ofo, 钉钉
soa架构也叫分布式架构,由互联网项目使用
Dubbo
dubbo作用
跨项目调用方法, 例如从A项目中的controller调用B项目中的service方法
dubbo同类型的技术都有哪些
rpc协议实现框架: dubbo, dubbox, springCloud
- 优点: 传输效率快
- 缺点: controller和service两个项目必须都是Java语言实现
webService技术: 代表框架有cxf
底层使用的协议叫做soap协议, 其实传输的就是xml数据
- 优点: 可以跨语言跨平台, controller可以是Java语言实现, service可以是.net或者C语言等语言实现
- 缺点: 传输效率低
dubbo用法
- service实现类上写@Service注解, 注意这个
@Service
注解不是spring包的是阿里的dubbo包下的 - controller注入的时候使用
@Refrence
注解来进行注入, 也是阿里dubbo包下面的. - 如果controller和service之间传输pojo实体类, 那么实体类必须实现Java的序列化接口
dubbo调用流程
Zookeeper
在linux下安装zookeeper
安装完成
将目录改成zookeeper
查看zoo.cfg配置文件
启动zookeeper
zookeeper命令:
启动: ./zkServer.sh start
关闭: ./zkServer.sh stop
查看状态: ./zkServer.sh status
dubbo入门
项目准备
首先创建两个项目
引入依赖
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- dubbo相关 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.8.4</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.11.0.GA</version>
</dependency>
</dependencies>
再配置两个tomcat,名字与两个项目相同
服务提供者web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<!-- 加载spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
服务提供者dubbo配置文件
创建applicationContext-dao.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:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--给当前项目服务起名-->
<dubbo:application name="dubboxdemo-service"/>
<!--配置连接zookeeper的ip和端口-->
<dubbo:registry address="zookeeper://192.168.200.128:2181"/>
<!--包扫描,在这个包下面的实现类中使用@Service注解才会生效-->
<dubbo:annotation package="cn.itcast.service" />
</beans>
服务消费者web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<!-- 解决post乱码 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 指定加载的配置文件 ,通过参数contextConfigLocation加载-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-web.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
服务消费者dubbo配置文件
<?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:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--注解驱动-->
<mvc:annotation-driven >
<!--转换器,将传输的字符串强制转换成utf-8编码-->
<mvc:message-converters register-defaults="false">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!--给当前项目起名-->
<dubbo:application name="dubboxdemo-web" />
<!--配置连接zookeeper-->
<dubbo:registry address="zookeeper://192.168.200.128:2181"/>
<!--配置包扫描,只有在这个包下面才可以注入service-->
<dubbo:annotation package="cn.itcast.controller" />
</beans>
测试
TestController
package cn.itcast.controller;
import cn.itcast.service.TestService;
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 org.springframework.web.bind.annotation.RestController;
/**
* @RestController注解作用:相当于在类上加上@Controller注解,并且在类的所以方法上@ResponseBody注解
*/
@RestController
@RequestMapping("/test")
public class TestController {
@Reference
private TestService testService;
@RequestMapping("/getName")
public String getName(){
String name = testService.getName();
return name;
}
}
TestService(接口省略)
package cn.itcast.service.impl;
import cn.itcast.service.TestService;
import com.alibaba.dubbo.config.annotation.Service;
@Service
public class TestServiceImpl implements TestService {
@Override
public String getName() {
return "zhangsan";
}
}