dubbo直连案例
一、创建工程
- 首先创建一个空工程,然后分别创建两个module
- Maven module创建
Maven module
创建方式, 创建的Module名称分别为001-link-provider
,002-link-consumer
创建方式如下图:
- 完成maven工程目录结构
001-link-provider,002-link-consumer
分别创建java resoureces
并创建资源目录
- 可以安装翻译插件 Translation(自定义)
使用方式选中单词然后快捷键ctrl + shift + Y
- 提供者配置
- 创建服务SomeService和服务实现类SomeServiceImpl
someService
代码:
public interface SomeService {
/**
* Hello
* @param message
* @return
*/
String hello(String message);
}
SomeServiceImp
l代码:
public class SomeServiceImpl implements SomeService {
public String hello(String message) {
//调用数据持久层
return "hello" + message;
}
}
- 将服务进行暴露,需要配置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内部使用的唯一标识-->
<dubbo:application name="001-link-provider"/>
<!--指定dubbo的协议名称和端口号
name 执行协议名称 官方推荐dubbo协议
port 协议的端口号,默认为20880
-->
<dubbo:protocol name="dubbo" port="20880"/>
<!--
暴露服务:dubbo:service
interface:暴露服务的接口全限定类名
ref: 引用接口在spring容器中的标识名称
registry:使用直连方式,不使用注册中心,值就必须写成:N/A
-->
<dubbo:service interface="com.bjpowermode.dubbo.service.SomeService" ref="someServiceImpl" registry="N/A"/>
<!--加载接口实现类-->
<bean id="someServiceImpl" class="com.bjpowermode.dubbo.service.impl.SomeServiceImpl"/>
</beans>
- provider中的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.bjpowermode.dubbo</groupId>
<artifactId>001-link-provider</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<!--dubbo依赖-->
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.0</version>
</dependency>
<!--spring依赖-->
<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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<port>8083</port>
<path>/</path>
<uriEncoding>GBK</uriEncoding>
</configuration>
</plugin>
</plugins>
</build>
</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">
<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>
- 消费者代码配置
- 配置dubbo的消费信息 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内部服务名称的唯一标识-->
<dubbo:application name="002-link-consumer"/>
<!--不需要执行协议,调用时已经知道了-->
<!--
引用远程接口
id: 远程接口服务的代理对象名称
interface:接口的全限定类名
url:调用远程接口服务的url地址
registry 直接方式 N/A
-->
<dubbo:reference id="someService"
interface="com.bjpowermode.dubbo.service.SomeService"
url="dubbo://localhost:20880"
registry="N/A"
/>
</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.bipowermode.dubbo.web"/>
<!--注解驱动-->
<mvc:annotation-driven/>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
</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">
<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:dubbo-link-consumer.xml,classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</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.bjpowermode.dubbo</groupId>
<artifactId>002-link-consumer</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<!--dubbo依赖-->
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.0</version>
</dependency>
<!--spring依赖-->
<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.bjpowermode.dubbo</groupId>
<artifactId>001-link-provider</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.bjpowermode.dubbo</groupId>
<artifactId>001-link-provider</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<port>8084</port>
<path>/</path>
<uriEncoding>GBK</uriEncoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
- 测试服务调用
需要配置ConsumeController以及创建hello.jsp
ConsumerController
@Controller
public class SomeController {
/* @Qualifier(value = "someService")
@Resource*/
@Autowired
private SomeService someService;
@RequestMapping(value = "/hello")
public String hello(Model model) {
//调用远程接口服务
String hello = someService.hello("world");
model.addAttribute("hello", hello);
System.out.println("hello = " + hello);
return "hello";
}
}
hello.jsp
二、修改web配置文件版本号(web.xml中的版本)
- create from archetype : 从原型中创建
org.apace.maven.archetypes:maven-archetype-webapp: : 这个maven仓库中有,和这个名称路径一样
2. 将maven-archetype-webapp-1.4.jar
中的web.xml
中的版本号进行修改,这样在新生成module项目
时,版本就会改为你所修改的版本
3. 将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">
</web-app>
三、 配置服务提供者监听器
这个需要在provider
中配置(上面配置完毕这里进行讲解
)
<?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>
四、添加消费者依赖
怎么知道服务者给我暴露那些接口
- 将
packaging注释掉,默认打成jar包
<groupId>com.bjpowermode.dubbo</groupId>
<artifactId>002-link-consumer</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- <packaging>war</packaging>-->
- 在
pom.xml
界面双击ctrl
输入mvn 001-link-provider install
<!--接口-->
<dependency>
<groupId>com.bjpowermode.dubbo</groupId>
<artifactId>001-link-provider</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
2. pom.xml
<dependencies>
<dependency>
<!--dubbo依赖-->
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.0</version>
</dependency>
<!--spring依赖-->
<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.bjpowermode.dubbo</groupId>
<artifactId>001-link-provider</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
五、配置服务消费者核心配置文件
- 消费者如何调用接口(如何调用001-link-provider)
<?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内部服务名称的唯一标识-->
<dubbo:application name="002-link-consumer"/>
<!--不需要执行协议,调用时已经知道了-->
<!--
引用远程接口
id: 远程接口服务的代理对象名称
interface:接口的全限定类名
url:调用远程接口服务的url地址
registry 直接方式 N/A
-->
<dubbo:reference id="someService"
interface="com.bjpowermode.dubbo.service.SomeService"
url="dubbo://localhost:20880"
registry="N/A"
/>
<dubbo:consumer/>
</beans>
六、配置springmvc配置文件
这个需要在consumer
中配置(上面配置完毕这里进行讲解
)
<?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.bipowermode.dubbo.web"/>
<!--注解驱动-->
<mvc:annotation-driven/>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
七、配置中央调试器
这个需要在provider
中配置(上面配置完毕这里进行讲解
)
<?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:dubbo-link-consumer.xml,classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
八、测试
- 配置tomcat插件
comsumer:
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<port>8084</port>
<path>/</path>
<uriEncoding>GBK</uriEncoding>
</configuration>
</plugin>
</plugins>
</build>
provider
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<port>8083</port>
<path>/</path>
<uriEncoding>GBK</uriEncoding>
</configuration>
</plugin>
</plugins>
</build>
添加hello.jsp
九、直连案例存在问题
暴露出底层impl,没有实际意义