1、创建服务提供者
1.1、创建maven web工程
1.2、整理pom文件,添加spring依赖和dubbo依赖和jdk1.8编译插件
maven中央仓库地址
下面是完整的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.why</groupId>
<artifactId>ch03-service-provide</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<dependencies>
<!--spring依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.12</version>
</dependency>
<!--dubbo依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<!--jdk1.8编译插件-->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
1.3、在src/main目录下创建java和resources资源文件夹
选中文件夹右键 >> Mark Directory as
1.4、创建实体类,服务接口和接口实现类
实体类
public class Student implements Serializable{
private Integer id;
private String name;
private boolean sex;
private Integer age;
getter和setter方法;
}
服务接口
public interface StudentService {
Student QueryById(Integer id);
}
接口实现类
public class StudentServiceImp implements StudentService {
/*
这里只是为了模拟dubbo的直连方式,
为了方便就省略了dao层
用student对象来模拟dao层返回的数据
@Autowired
StudentDao studentDao;
*/
@Override
public Student QueryById(Integer id) {
Student student = new Student();
student.setId(001);
student.setName("张三");
student.setSex(true);
student.setAge(20);
return student;
}
}
1.5、创建dubbo核心配置文件(spring的xml配置文件)
a.声明dubbo服务提供者名称:保证唯一
b.声明dubbo使用的协议和端口
c.暴露服务(直连方式)
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:dubbo="http://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!--声明dubbo服务提供者名称:保证唯一-->
<!--选择dubbo.apache.org的那个-->
<dubbo:application name="StudentService-provide"/>
<!--声明dubbo使用的协议和端口,dubbo官方推荐使用的协议为dubbo,端口号默认为20880-->
<dubbo:protocol name="dubbo" port="20880"/>
<!--暴露服务(直连方式)-->
<!--
暴露服务标签<dubbo:service>
interface:暴露的服务接口的全限定类名
ref:接口引用的实现类在spring中的标识
registry:如果不使用注册中心(使用直连方式),则值为N/A
-->
<dubbo:service interface="com.why.service.StudentService" ref="StudentService" registry="N/A"/>
<bean id="StudentService" class="com.why.service.serviceImp.StudentServiceImp"/>
</beans>
1.6、web.xml中创建监听器
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dubbo-service-provide.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
1.7、配置Tomcat修改端口
双击ctrl进行打包
打包前先注释掉<packaging>war</packaging>标签,打包完成后放开注释
输入mvn ch03-service-provide install
服务提供者就创建好了,接下来创建服务消费者
2、创建服务消费者
2.1、创建maven web工程
2.2、整理pom文件,添加spring依赖、dubbo依赖、服务提供者jar包和jdk1.8编译插件
<?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.why</groupId>
<artifactId>ch04-service-consumer</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.12</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>com.why</groupId>
<artifactId>ch03-service-provide</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<!--jdk1.8编译插件-->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.3、在src/main目录下创建java和resources资源文件夹
2.4、创建dubbo核心配置文件和springmvc配置文件(spring的xml配置文件)
创建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:dubbo="http://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!--声明dubbo服务提供者名称:保证唯一-->
<!--选择dubbo.apache.org的那个-->
<dubbo:application name="StudentService-consumer"/>
<!--
引用远程服务接口<dubbo:reference>标签
id;远程服务接口对象名称
interface:调用远程服务接口的全限定名称
url:访问服务接口的地址 dubbo规定用dubbo://+ip地址+端口号
registey:直连N/A
-->
<!--由于我这是用一台电脑模拟两台服务器,所以ip地址填了localhost-->
<dubbo:reference id="userService" interface="com.why.service.StudentService" url="dubbo://localhost:20880" registry="N/A"/>
</beans>
创建springMVC的配置文件
<?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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--配置组件扫描器-->
<context:component-scan base-package="com.why.controller"/>
<!--配置注解驱动-->
<!--选mvc结尾的-->
<mvc:annotation-driven/>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
2.5、编写controller
@Controller
public class Mycontroller {
@Resource
StudentService studentService;
@RequestMapping("")
public String studnetQuery(Model model, Integer id){
Student student = studentService.QueryById(id);
model.addAttribute("student",student);
return "student";
}
}
2.6、配置中央调度器
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<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:application.xml,classpath:dubbo-service-consumer.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
2.7创建jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
学生id=${student.id}<br>
学生name=${student.name}<br>
学生age=${student.age}
</body>
</html>