一、dubbo官方推荐使用的项目结构如下:
- 服务提供者工程
实现接口工程中的业务接口 (web工程) - 服务消费者工程
消费服务提供者提供的业务接口 (web工程) - 接口工程
业务接口和实体类 (java工程)
二、官方推荐使用的项目结构实战(直联方式)
- 直联方式项目结构图
001-link-provider
:表示服务提供者
002-link-consumer
:表示服务消费者
003-interface
: 接口工程
三、接口
- 项目结构图
- DubboService
public interface DubboService {
public String sayDubbo();
}
- pom.xml
<?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>com.cjw.dubbo</groupId>
<artifactId>003-interface</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
</project>
四、提供者代码
- 项目结构图
- DubboServiceImpl
public class DubboServiceImpl implements DubboService {
public String sayDubbo() {
return "001-link-provider-sayDubbo";
}
}
- dubbo-link-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="001-link-provider"/>
<!--指定协议名称以及端口号-->
<dubbo:protocol name="dubbo" port="20880"/>
<!--暴露服务-->
<dubbo:service interface="com.cjw.dubbo.service.DubboService" ref="dubboServiceImpl" registry="N/A"/>
<!--接口实现类-->
<bean id="dubboServiceImpl" class="com.cjw.dubbo.service.imp.DubboServiceImpl"/>
</beans>
- web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dubbo-link-provider.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
- pom.xml
?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>com.cjw.dubbo</groupId>
<artifactId>001-link-provider</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.16.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.16.RELEASE</version>
</dependency>
<!--接口工程-->
<dependency>
<groupId>com.cjw.dubbo</groupId>
<artifactId>003-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
五、服务消费者
- 项目结构图
- pom.xml
<?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>com.cjw.dubbo</groupId>
<artifactId>002-link-consumer</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.16.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.16.RELEASE</version>
</dependency>
<dependency>
<groupId>com.cjw.dubbo</groupId>
<artifactId>003-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
- web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml,classpath:dubbo-link-consumer.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
- DubboController
@Controller
public class DubboController {
@Autowired
private DubboService dubboService;
@RequestMapping("/say")
public String sayDubbo(Model model){
String sayDubbo = dubboService.sayDubbo();
System.out.println("sayDubbo = " + sayDubbo);
model.addAttribute("sayDubbo", sayDubbo);
return "sayDubbo";
}
}
- dubbo-link-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="002-link-consumer"/>
<!--不需要执行协议,调用时已经知道了-->
<!--引用远程接口
id: 远程接口服务的代理对象名称
interface:接口的全限定类名
url:调用远程接口服务的url地址
registry 直联方式 N/A
-->
<dubbo:reference id="dubboService" interface="com.cjw.dubbo.service.DubboService"
url="dubbo://localhost:20880"
registry="N/A"/>
<!--
缺少id 或者id错误(意思是和Controller中使用的service对应不上)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type 'com.cjw.dubbo.service.DubboService' available: expected at
least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org
-->
</beans>
- springmvc.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: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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.cjw.dubbo.web"/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
- sayDubbo.jsp
<title>Title</title>
<hr/>
测试dubbo直联---》${sayDubbo}
<hr/>
六、测试结果
七、注意
- idea 配置tomcat
一个tomcat
就可以 ,只需要在不同的Tomcat Server
中需要修改HTTP Port
和JMX port