发布时间:2024-03-20 19:01
Dubbo 最早是 Alibaba 开源的分布式服务框架,它最大的特点是按照分层的方式来架构, 使用这种方式可以使各个层之间解耦合(或者最大限度地松耦合)。从服务模型的角度来看, Dubbo 采用的是一种非常简单的模型,要么是提供方提供服务,要么是消费方消费服务,所 以基于这一点可以抽象出服务提供方(Provider)和服务消费方(Consumer)两个角色。
优点:可与原生Hessian互操作,基于HTTP协议
缺点:需hessian.jar支持,http短连接的开销大
应用结构
<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.0modelVersion>
<groupId>com.lanhgroupId>
<artifactId>parentartifactId>
<packaging>pompackaging>
<version>1.0-SNAPSHOTversion>
<modules>
<module>apimodule>
<module>providermodule>
<module>consumermodule>
modules>
<properties>
<spring.version>5.2.5.RELEASEspring.version>
<dubbo.version>2.7.6dubbo.version>
<curator.version>4.2.0curator.version>
<registry-zookeeper.version>2.7.6registry-zookeeper.version>
<servlet.version>3.1.0servlet.version>
<jsp.version>2.0jsp.version>
<jstl.version>1.2jstl.version>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubboartifactId>
<version>${dubbo.version}version>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-registry-zookeeperartifactId>
<version>${registry-zookeeper.version}version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-frameworkartifactId>
<version>${curator.version}version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>${servlet.version}version>
<scope>providedscope>
dependency>
<dependency>
<groupId>javax.servlet.jspgroupId>
<artifactId>jsp-apiartifactId>
<version>${jsp.version}version>
<scope>providedscope>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>jstlartifactId>
<version>${jstl.version}version>
dependency>
dependencies>
dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.tomcat.mavengroupId>
<artifactId>tomcat7-maven-pluginartifactId>
<version>2.2version>
plugin>
plugins>
pluginManagement>
build>
project>
创建功能接口
public interface DemoDubboService {
String showMsg(String msg);
}
<dependencies>
<dependency>
<groupId>com.lanhgroupId>
<artifactId>apiartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubboartifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-registry-zookeeperartifactId>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-frameworkartifactId>
dependency>
dependencies>
注意:provider项目和consumer项目都要依赖api项目
package com.lanh.dubbo.service.impl;
import com.lanh.dubbo.service.DemoDubboService;
import org.apache.dubbo.config.annotation.Service;
/**
* @Author Lanh
**/
@Service
public class DemoDubboServiceImpl implements DemoDubboService {
public String showMsg(String msg) {
return \"Helle Dubbo \"+msg;
}
}
需要注意的是,这里使用到的注解@Service不是spring中的注解,而是dubbo的注解
package com.lanh.dubbo;
import org.apache.dubbo.container.Main;
/**
* Dubbo的启动类
* @Author Lanh
**/
public class Start {
public static void main(String[] args) {
Main.main(args);
}
}
<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:application name=\"myProvider\" />
<dubbo:registry address=\"ip:2181,ip:2182,ip:2183\" protocol=\"zookeeper\" timeout=\"10000\"/>
<dubbo:protocol name=\"dubbo\" port=\"2002\" />
<dubbo:annotation package=\"com.lanh.dubbo.service.impl\" />
beans>
注意:
web项目是在spring中不需要写启动类,是通过tomcat插件启动的
package com.lanh.service;
public interface DemoService {
String showInfo(String msg);
}
package com.lanh.service.impl;
import com.lanh.dubbo.service.DemoDubboService;
import com.lanh.service.DemoService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;
/**
* Consumer的业务层
* @Author Lanh
**/
@Service
public class DemoServiceImpl implements DemoService {
@Reference
private DemoDubboService demoDubboService;
public String showInfo(String msg) {
return this.demoDubboService.showMsg(msg);
}
}
注意:
- 这里使用到的@Service不同于provider,这个是spring框架的注解
- 同时,这里进行注入时,因为这个不是本地的服务,使用的也不再是@Autowire注解,而是dubbo远程调用的@Reference注解
package com.lanh.web.controller;
import com.lanh.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author Lanh
**/
@RestController
public class DemoController {
@Autowired
private DemoService demoService;
@RequestMapping(\"/getMsg\")
public String getMsg(String str){
return this.demoService.showInfo(str);
}
}
<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-4.3.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd\">
<dubbo:application name=\"myConsumer\" />
<dubbo:registry address=\"192.168.126.128:2181,192.168.126.128:2182,192.168.126.128:2183\" protocol=\"zookeeper\" timeout=\"10000\"/>
<dubbo:annotation package=\"com.lanh.service.impl\"/>
beans>
applicationContext-service.xml
<beans xmlns=\"http://www.springframework.org/schema/beans\"
xmlns:context=\"http://www.springframework.org/schema/context\"
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
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\">
<context:component-scan base-package=\"com.lanh.service\" />
beans>
springmvc.xml
<beans xmlns=\"http://www.springframework.org/schema/beans\"
xmlns:context=\"http://www.springframework.org/schema/context\"
xmlns:mvc=\"http://www.springframework.org/schema/mvc\"
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
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://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd\">
<context:component-scan base-package=\"com.lanh.web.controller\" />
<mvc:annotation-driven/>
<bean class=\"org.springframework.web.servlet.view.InternalResourceViewResolver\">
<property name=\"prefix\" value=\"/WEB-INF/jsp/\"/>
<property name=\"suffix\" value=\".jsp\"/>
bean>
beans>
web.xml
<web-app xmlns=\"http://java.sun.com/xml/ns/javaee\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
xsi:schemaLocation=\"http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd\"
version=\"3.0\">
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring/applicationContext-*.xmlparam-value>
context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<servlet>
<servlet-name>springmvcservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
servlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring/springmvc.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>springmvcservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
<filter>
<filter-name>encodeFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>utf-8param-value>
init-param>
filter>
<filter-mapping>
<filter-name>encodeFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
web-app>
设置功能接口
package com.lanh.dubbo.service;
public interface DemoDubboService {
String showMsg(String str);
}
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd\">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.2.0.RELEASEversion>
<relativePath/>
parent>
<groupId>com.lanhgroupId>
<artifactId>springbootdubbo_providerartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>springbootdubbo_providername>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>com.lanhgroupId>
<artifactId>springbootdubbo_apiartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
<version>2.7.6version>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-registry-zookeeperartifactId>
<version>2.7.6version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
package com.lanh.springbootdubbo_provider.service.impl;
import com.lanh.dubbo.service.DemoDubboService;
import org.apache.dubbo.config.annotation.Service;
/**
* @Author Lanh
**/
@Service
public class DemoDubboServiceImpl implements DemoDubboService {
@Override
public String showMsg(String str) {
return \"Hello Dubbo \"+str;
}
}
再说一次这个是@Service是dubbo的注解
package com.lanh.springbootdubbo_provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootdubboProviderApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootdubboProviderApplication.class, args);
}
}
#指定服务的名称
dubbo:
application:
name: myProvider
registry:
address: 192.168.126.128:2181,192.168.126.128:2182,192.168.126.128:2183
protocol: zookeeper
timeout: 10000
#配置服务所使用的协议
protocol:
name: dubbo
port: 2002
scan:
base-packages: com.lanh.springbootdubbo_provider.service.impl
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd\">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.2.0.RELEASEversion>
<relativePath/>
parent>
<groupId>com.lanhgroupId>
<artifactId>springbootdubbo_consumerartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>springbootdubbo_consumername>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>com.lanhgroupId>
<artifactId>springbootdubbo_apiartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>ch.qos.logbackgroupId>
<artifactId>logback-classicartifactId>
<version>1.2.3version>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
<version>2.7.6version>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-registry-zookeeperartifactId>
<version>2.7.6version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<exclusions>
<exclusion>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
package com.lanh.springbootdubbo_consumer.service;
public interface DemoService {
String getMsg(String str);
}
package com.lanh.springbootdubbo_consumer.service.impl;
import com.lanh.dubbo.service.DemoDubboService;
import com.lanh.springbootdubbo_consumer.service.DemoService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;
/**
* @Author Lanh
**/
@Service
public class DemoServiceImpl implements DemoService {
@Reference
private DemoDubboService demoDubboService;
@Override
public String getMsg(String str) {
return this.demoDubboService.showMsg(str);
}
}
再说一次:这个@Service是spring中的注解,用@Reference而不用@Autowire
package com.lanh.springbootdubbo_consumer.controller;
import com.lanh.springbootdubbo_consumer.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author Lanh
**/
@RestController
public class DemoController {
@Autowired
private DemoService demoService;
@RequestMapping(\"/getMsg\")
public String getMsg(String str){
return this.demoService.getMsg(str);
}
}
package com.lanh.springbootdubbo_consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootdubboConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootdubboConsumerApplication.class, args);
}
}
#指定服务的名称
dubbo:
application:
name: myConsumer
registry:
address: 192.168.126.128:2181,192.168.126.128:2182,192.168.126.128:2183
protocol: zookeeper
timeout: 10000
#配置服务所使用的协议
protocol:
name: dubbo
至此结束,欢迎各位小伙伴讨论