dubbo入门第一课--简单项目
项目里在使用dubbo,我一直想要知道dubbo是干什么的,怎么用,具体的原理。
那么第一课,我首先来介绍dubbo能干什么?
-
dubbo能干什么?
我以一个我们公司用到的简单功能来分析:我们是P2P平台,平台发布了一些标的供用户购买,首先用户需要登录网站,然后支付订单,然后平台结标开始起息。
如果是传统的web应用,用户登录,支付订单,结标起息这三个功能块应该部署在一台机器上,然后顺序执行。随着业务量的增加,这一台服务器的压力越来越大,我们不能在这样一条直线式的把所有项目部署到一台机器上。这个时候就需要分布式系统了,我们可以把这块功能分解成三个独立的系统:用户系统,订单系统,结标起息系统,而且把这三个系统分别部署到三台服务器上,这样服务器的压力就小了很多。但是这样一来,就又麻烦了,这三台服务器之间怎么沟通呢?原来一台服务器,由于是本地调用,直接调用就可以了。可这里需要机器与机器时间交流了,如何做到远程通信像本地通信那样呢?
其实到了这里,我们就可以知道Dubbo是一个分布式的RPC服务框架了。官方介绍的是:
Dubbo 是一个分布式、高性能、透明化的 RPC 服务框架,提供服务自动注册、自动发现等高效服务治理方案。 -
怎么用?
我们已经大概知道dubbo是干什么的,现在就来聊聊怎么用吧。我的项目包含四个部分:
- spring-dubbo
父项目 - spring-dubbo-api
子项目,主要包括公共的东西,比如service,model之类的。 - spring-dubbo-provider
服务的提供者,provider - spring-dubbo-consumer
dubbo的使用者,consumer.
2.1 spring-dubbo
这里看下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.wang.dubbo</groupId>
<artifactId>spring-dubbo</artifactId>
<version>${dubbodemo.version}</version>
<packaging>pom</packaging>
<name>spring-dubbo</name>
<modules>
<module>spring-dubbo-provider</module>
<module>spring-dubbo-consumer</module>
<module>spring-dubbo-api</module>
</modules>
<properties>
<dubbodemo.version>0.0.1-SNAPSHOT</dubbodemo.version>
<springside.version>4.1.0.GA</springside.version>
<spring.version>4.0.9.RELEASE</spring.version>
<jstl.version>1.2</jstl.version>
<jackson.version>2.3.4</jackson.version>
<commons-lang3.version>3.2.1</commons-lang3.version>
<guava.version>14.0.1</guava.version>
<commons-beanutils.version>1.9.2</commons-beanutils.version>
<slf4j.version>1.7.10</slf4j.version>
<logback.version>1.1.3</logback.version>
<log4jdbc.version>1.15</log4jdbc.version>
<lombok.version>1.16.4</lombok.version>
<junit.version>4.12</junit.version>
<jdk.version>1.8</jdk.version>
<httpclient.version>4.5</httpclient.version>
<aspectj.version>1.7.4</aspectj.version>
<commons-collections.version>3.2.2</commons-collections.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- SpringSide -->
<dependency>
<groupId>org.springside</groupId>
<artifactId>springside-core</artifactId>
<version>${springside.version}</version>
</dependency>
<!-- Spring MVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-jaxb-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-joda</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- Common Utilitis -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>${commons-beanutils.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.bgee.log4jdbc-log4j2</groupId>
<artifactId>log4jdbc-log4j2-jdbc4</artifactId>
<version>${log4jdbc.version}</version>
</dependency>
<!-- AOP -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${httpclient.version}</version>
</dependency>
<!-- Code Generator -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<!-- compiler插件, 设定JDK版本 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
<!-- 忽略单元测试 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<!-- 生成域对象的QDomain -->
</plugins>
</pluginManagement>
</build>
</project>
2.2 spring-dubbo-api
先看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>
<parent>
<groupId>com.wang.dubbo</groupId>
<artifactId>spring-dubbo</artifactId>
<version>${dubbodemo.version}</version>
</parent>
<artifactId>spring-dubbo-api</artifactId>
<name>spring-dubbo-api</name>
<dependencies>
<!-- SpringSide -->
<dependency>
<groupId>org.springside</groupId>
<artifactId>springside-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<!-- Jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-jaxb-annotations</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-joda</artifactId>
</dependency>
<!-- Common Utilitis -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<!-- Logback -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
</dependency>
<dependency>
<groupId>org.bgee.log4jdbc-log4j2</groupId>
<artifactId>log4jdbc-log4j2-jdbc4</artifactId>
</dependency>
<!-- Code Generator -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
</dependencies>
</project>
建立com.wang.api.model的包,放LogModel.java,如下所示:
package com.wang.api.model;
import java.io.Serializable;
import java.util.Date;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class LogModel implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* id
*/
private String id;
/**
* 操作对象
*/
private String entityId;
/**
* 操作人
*/
private String userId;
/**
* 名称
*/
private String name;
/**
* 内容
*/
private String content;
/**
* 操作时间
*/
private Date createTime;
}
记得model一定要序列化。
然后我们再看看service层,com.wang.api.service包下建立LogService.java
package com.wang.api.service;
import com.wang.api.model.LogModel;
/**
* 日志服务
* @author wxe
* @since 0.0.1
*/
public interface LogService {
/**
* 添加日志
* @param log
* @return
*/
public LogModel add(LogModel logModel);
/**
* 删除日志
* @param log
* @return
*/
public boolean delete(LogModel llogModelog);
}
2.3 spring-dubbo-provider
先看这个服务提供者的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>
<parent>
<groupId>com.wang.dubbo</groupId>
<artifactId>spring-dubbo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>spring-dubbo-provider</artifactId>
<packaging>war</packaging>
<name>spring-dubbo-provider</name>
<dependencies>
<dependency>
<groupId>com.wang.dubbo</groupId>
<artifactId>spring-dubbo-api</artifactId>
<version>${dubbodemo.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.8.4</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</exclusion>
<!-- <exclusion> -->
<!-- <groupId>org.javassist</groupId> -->
<!-- <artifactId>javassist</artifactId> -->
<!-- </exclusion> -->
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.7</version>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty</artifactId>
<version>6.1.24</version>
</dependency>
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.38</version>
</dependency>
</dependencies>
</project>
我这里建立的是一个web项目,所以先来介绍web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>spring-dubbo-provider</display-name>
<!-- Spring ApplicationContext配置文件的路径,可使用通配符,用于后面的Spring Context Loader -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:applicationContext.xml
classpath*:applicationContext-*.xml
</param-value>
</context-param>
<!--Spring ApplicationContext 载入 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Filter 定义 -->
<!-- Character Encoding filter -->
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Support for HiddentHttpMethod -->
<filter>
<filter-name>hiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hiddenHttpMethodFilter</filter-name>
<servlet-name>springServlet</servlet-name>
</filter-mapping>
<!-- Spring MVC Servlet -->
<servlet>
<servlet-name>springServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>120</session-timeout>
</session-config>
</web-app>
再来介绍下springmvc的配置文件,spring-mvc.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:c="http://www.springframework.org/schema/c" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 自动扫描且只扫描@Controller -->
<context:component-scan base-package="com.wang" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" />
</context:component-scan>
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter" c:defaultCharset="UTF-8" />
</mvc:message-converters>
</mvc:annotation-driven>
<!-- 定义JSP文件的位置 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:viewClass="org.springframework.web.servlet.view.JstlView"
p:prefix="/WEB-INF/views/"
p:suffix=".jsp" />
<!-- 容器默认的DefaultServletHandler处理 所有静态内容与无RequestMapping处理的URL -->
<mvc:default-servlet-handler />
</beans>
然后来介绍下应用配置文件,主要来定义spring扫描除了controller注解的其他注解,applicationContext.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:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
">
<context:component-scan base-package="com.wang">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" />
</context:component-scan>
</beans>
现在我们的服务提供者该提供服务了,就需要实现LogService.java,如下所示:
package com.wang.provider.service.impl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.wang.api.model.LogModel;
import com.wang.api.service.LogService;
@Service("logService")
@Slf4j
public class LogServiceImpl implements LogService {
public LogModel add(LogModel logModel) {
log.info("哇,有人调用我的服务了哦! :{}", logModel);
return logModel;
}
public boolean delete(LogModel log) {
// TODO Auto-generated method stub
return false;
}
}
现在服务实现好了,那么我们知道这个是要为别的客户端提供远程服务的,我们需要暴露这些服务,配置文件applicationContext-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:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
">
<!-- 当前应用配置 -->
<dubbo:application name="dubbo-provider" owner="dubbo" />
<!-- 延迟暴露:delay="-1"延迟到spring初始化完成后,再暴露服务 -->
<dubbo:provider delay="-1" />
<!--使用 zookeeper 注册中心暴露服务,会在启动时检查依赖的服务是否可用,不可用时会抛出异常,阻止 Spring 初始化完成,以便上线时,能及早发现问题,默认 check="true" -->
<dubbo:registry protocol="zookeeper" check="true" address="127.0.0.1:2181" />
<!-- 服务提供者协议配置,服务线程池大小(固定大小) ,默认为100,threadpool:线程池类型,可选:fixed/cached ,默认fixed -->
<dubbo:protocol name="dubbo" port="20880" threads="200" accesslog="true" />
<dubbo:protocol name="hessian" port="20881" threads="200" accesslog="true" />
<!-- 服务提供者暴露服务,当一个接口有多种实现时,可以用group区分 -->
<dubbo:service interface="com.wang.api.service.LogService" ref="logService" timeout="3000" loadbalance="random" protocol="dubbo"/>
</beans>
到这里为止,我们的服务提供者已经开发完毕了。后面来介绍下服务是消费者。
2.4 spring-dubbo-consumer
先来介绍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>
<parent>
<groupId>com.wang.dubbo</groupId>
<artifactId>spring-dubbo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>spring-dubbo-consumer</artifactId>
<packaging>war</packaging>
<name>spring-dubbo-consumer</name>
<dependencies>
<dependency>
<groupId>com.wang.dubbo</groupId>
<artifactId>spring-dubbo-api</artifactId>
<version>${dubbodemo.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.8.4</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</exclusion>
<!-- <exclusion> -->
<!-- <groupId>org.javassist</groupId> -->
<!-- <artifactId>javassist</artifactId> -->
<!-- </exclusion> -->
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.7</version>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty</artifactId>
<version>6.1.24</version>
</dependency>
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.38</version>
</dependency>
</dependencies>
</project>
这个项目我建的也是一个web项目,所以介绍web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>spring-dubbo-consumer</display-name>
<!-- Spring ApplicationContext配置文件的路径,可使用通配符,用于后面的Spring Context Loader -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:applicationContext.xml
classpath*:applicationContext-*.xml
</param-value>
</context-param>
<!--Spring ApplicationContext 载入 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring MVC Servlet -->
<servlet>
<servlet-name>springServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>120</session-timeout>
</session-config>
</web-app>
再来看看spring-mvc.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:c="http://www.springframework.org/schema/c" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 自动扫描且只扫描@Controller -->
<context:component-scan base-package="com.wang" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" />
</context:component-scan>
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter"
c:defaultCharset="UTF-8" />
</mvc:message-converters>
</mvc:annotation-driven>
<!-- 定义JSP文件的位置 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:viewClass="org.springframework.web.servlet.view.JstlView" p:prefix="/WEB-INF/views/"
p:suffix=".jsp" />
<!-- 容器默认的DefaultServletHandler处理 所有静态内容与无RequestMapping处理的URL -->
<mvc:default-servlet-handler />
</beans>
然后看看相关的配置文件,主要用来扫描注解用的配置文件applicationContext.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:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
">
<context:component-scan base-package="com.wang">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" />
</context:component-scan>
</beans>
既然我们想要使用服务,那么我们首先需要配置好使用服务的配置文件applicationContext-dubbo-consumer.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:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
">
<!-- 当前应用配置 -->
<dubbo:application name="dubbo-consumer" owner="dubbo" />
<!-- 延迟暴露:delay="-1"延迟到spring初始化完成后,再暴露服务 -->
<dubbo:provider delay="-1" />
<!--使用 zookeeper 注册中心暴露服务,会在启动时检查依赖的服务是否可用,不可用时会抛出异常,阻止 Spring 初始化完成,以便上线时,能及早发现问题,默认 check="true" -->
<dubbo:registry protocol="zookeeper" check="true" address="172.0.0.1:2181" />
<!-- 服务提供者协议配置,服务线程池大小(固定大小) ,默认为100,threadpool:线程池类型,可选:fixed/cached ,默认fixed -->
<dubbo:protocol name="dubbo" port="20880" threads="200" accesslog="true" />
<dubbo:protocol name="hessian" port="20881" threads="200" accesslog="true" />
<!-- 远程服务 -->
<dubbo:reference protocol="dubbo" check="false" id="logService" interface="com.wang.api.service.LogService" />
</beans>
好了,既然已经配置好服务了,我们来试试好用不。简历controller,
package com.wang.consumer;
import java.util.Date;
import java.util.UUID;
import org.mortbay.log.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.wang.api.model.LogModel;
import com.wang.api.service.LogService;
@Controller
@RequestMapping(value="/main")
public class MainController {
@Autowired
private LogService logService;
@RequestMapping(value="/add",method = RequestMethod.GET)
@ResponseBody
public LogModel addLog(){
LogModel logModel = new LogModel(UUID.randomUUID().toString(), "1111", "2222", "您好", "保存合同", new Date());
Log.info("开始调用远程服务");
LogModel newLog = logService.add(logModel);
Log.info("调用服务完毕!{}",newLog.toString());
return newLog;
}
}
好了,点击http://localhost:8080/consumer/main/add,就可以看到打印出来的结果:
{"id":"748594b9-13f2-41dd-961a-880abf9d79dd","entityId":"1111","userId":"2222","name":"您好","content":"保存合同","createTime":1547103471019}