IntelliJ IDEA 2018.2创建Maven项目dubbo部署
演示demo已经上传到github上了,有需要到朋友可以自行查看:
https://github.com/dsYangTM/dubbodemo
基于maven的dubbo部署,自己建了个demo跑了一下,环境配的我头都炸了,不过最后还是跑通了,写此文总结一下:
一:Cerate New Project(新建项目)
创建maven项目,选择jdk,这里不用选择create from archetype(进去以后再配maven),直接下一步
填写GroupId和ArtifactId
GroupId和ArtifactId是确定项目在Maven仓库中的坐标,具有唯一性,可以通过他们去查找项目。
GroupId一般分为多个段,第一段为域,第二段为公司名称。域又分为org、com、cn等等。
ArtifactId是工程项目名。
填写Project name以及项目的物理地址。
点击ok
选择Enable Auto-Imported.(自动导入)
找到配置输入maven设置
二:填写Maven home directory,设置settings.xml文件、local repository![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c34b4177fd13a06a289486fa33ca62aa.png)
dubbo-service的pom.xml添加依赖,端口为8081,跟8080区分开
<?xml version="1.0" encoding="UTF-8"?>
<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>cn.dsyang.dubbodemo</groupId>
<artifactId>dubbo-service</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<spring.version>4.2.4.RELEASE</spring.version>
</properties>
<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>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<!-- 指定端口 -->
<port>8081</port>
<!-- 请求路径 -->
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
在工程的webapp
下创建WEB-INF文件夹,创建web.xml
,(此时的webapp文件上一定要有小蓝点)
<?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>
三:这里需要检查项目配置。
打开File>Project Struction。
project:![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a267317625d0d8f7628cd2ee40fb76e5.png)
Modules:
选择项目名字:
Sources:
将java设置为Sources,resources设置为Resources,target设置为Excluded。
paths:
设置java源代码编译的目标目录。
Dependencies:
设置依赖。
Facets:
选择web.xml和webapp文件夹位置:
这些配置对了基本上就没太大问题了
接下来创建业务接口
创建包cn.dsyang.dubbodemo.service
,用于存放业务接口,创建接口
创建业务实现类
创建包cn.dsyang.dubbodemo.service.impl
,用于存放业务实现类。创建业务实现类:注意:Service注解与原来不同,需要引入com.alibaba包下的
在resources下创建applicationContext-service.xml
application name:填项目名称即可
registry address:你远程zookeeper的ip地址(我是装在linux虚拟机里的)
annotation package:你实现类的包地址(用于扫描@Service注解。)
<?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="dubbo-service"/>
<dubbo:registry address="zookeeper://172.16.161.130:2181"/>
<dubbo:annotation package="cn.dsyang.dubbodemo.service.impl" />
</beans>
这里service服务提供者就创建完了,可以右键项目运行clean install,然后tomcat7:run,把项目跑起来(然而这个项目单独跑并没什么卵用,需要搭配服务消费者一起使用)
接下来是创建服务消费者也就是web的demo
创建Maven工程(WAR)dubbo-web (这里一定要注意,之前就是因为选中了dubbo-service新建的module项目一直跑起不来
)
接下来跟新建service项目差不多,直接next
输入GroupId和ArtifactId
输入module name,
content root
module file location
然后finish
在webapp目录下创建WEB-INF 目录,并创建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-service”工程的cn.dsyang.dubbodemo.service 包以及下面的接口拷贝至此工程。
编写Controller
编写spring配置文件
在src/main/resources下创建applicationContext-web.xml
参数同service项目类似(不再赘述了)
<?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 >
<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 服务 -->
<dubbo:application name="dubbo-web" />
<dubbo:registry address="zookeeper://172.16.161.130:2181"/>
<dubbo:annotation package="cn.dsyang.dubbodemo.controller" />
</beans>
最后测试运行
tomcat7:run(先运行dubbo-service,再运行dubbo-web
)
在浏览器输入http://localhost:8082/user/showName.do,查看浏览器输出结果
大功告成,总结不易,请支持原创!若需引用或转载请附上本文地址;