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";
}
}