Dubbo服務化最優實作
由于上篇dubbo直連方式文章直接提供了服務提供者的jar包,導致在服務消費者的工程中可以直接使用服務介面實作類.為了避免這樣的情況發生,dubbo官方推薦遠程呼叫RPC服務以介面為粒度,為開發者屏蔽遠程呼叫底層細節.必須有一個介面工程.它是一個maven java工程,對外提供服務的介面.要求介面工程里存放的內容如下:
1.對外暴露的服務介面(service介面)
2.物體bean物件
提示:由于是改造,大部分代碼可以復制之前寫好的,當然我在這里又寫了一邊,可以參考下面的步驟
1、創建介面工程
1.1、創建maven Java 工程

1.2、創建物體類和服務介面
物體類
public class Student implements Serializable {
private Integer id;
private String name;
private Integer age;
setter and getter方法;
服務介面
public interface StudentService {
//通過id查詢學生
Student QueryById(Integer id);
}
2、創建服務提供者工程(創建方法與之前直連的差不多)
2.1創建maven web工程

2.2、整理pom檔案,添加依賴(spring,dubbo,介面工程)和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>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>
<dependency>
<groupId>com.why</groupId>
<artifactId>ch05-interface</artifactId>
<version>1.0.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、創建服務介面實作類
/*
這里只是為了模擬dubbo的直連方式,
為了方便就省略了dao層
用student物件來模擬dao層回傳的資料
@Autowired
StudentDao studentDao;
*/
@Override
public Student QueryById(Integer id) {
Student student = new Student();
student.setId(1);
student.setName("張三");
student.setAge(20);
return student;
}
2.4、創建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.Imp.StudentServiceImp"/>
</beans>
2.5、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>
2.6、配置Tomcat修改埠

3、創建服務消費者工程
3.1創建maven web工程

3.2整理pom檔案,添加依賴(spring,dubbo,介面工程)和jdk1.8編譯插件(此時就不需要服務提供者的jar包了)
<?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>ch05-service-consumer</artifactId>
<version>1.0-SNAPSHOT</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>ch05-interface</artifactId>
<version>1.0.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>
3.3、創建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="ch05-StudentService-consumer"/>
<!--
參考遠程服務介面<dubbo:reference>標簽
id;遠程服務介面物件名稱
interface:呼叫遠程服務介面的全限定名稱
url:訪問服務介面的地址 dubbo規定用dubbo://+ip地址+埠號
registey:直連N/A
-->
<dubbo:reference id="userService" interface="com.why.service.StudentService" url="dubbo://localhost:20880" registry="N/A"/>
</beans>
spring核心組態檔
<?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>
3.4、撰寫controller
@Controller
public class MyController {
@Resource
StudentService studentService;
@RequestMapping("/student")
public String studnetQuery(Model model, Integer id){
Student student = studentService.QueryById(id);
model.addAttribute("student",student);
return "showStudent";
}
}
3.5、配置中央調度器
<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>
3.6、創建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>
測驗:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/356933.html
標籤:其他
上一篇:大資料分析,到底分析了啥?
下一篇:Dubbo(使用注冊中心)
