可以转载,但请在开头鲜明描述转载来源
多模块项目结构:父工程springbootparent2 ,子模块 dubboapi , dubboa , dubbob
dubboa 和 dubbob 一样都 既是dubbo提供者又是消费者 , 他们的关系如下:
(注意:下面程序运行前,请先确认zookeeper安装且运行,dubbo-admin安装且运行,若没安装看
https://blog.csdn.net/a704397849/article/details/91904085 )
项目整体结构图如下:
dubboapi 是dubbo公共服务接口api ,有2个接口服务
AService
package com.example.dubboapi.service;
public interface AService {
public String sayA(String s);
}
BService
package com.example.dubboapi.service;
public interface BService {
public String sayB(String s);
}
springboot 多模块创建 具体过程不再赘述了,看我之前的springboot dubbo 多模块文章
https://blog.csdn.net/a704397849/article/details/94185615
子模块 dubboa结构
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>
<parent>
<groupId>com.example</groupId>
<artifactId>springbootparent2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>dubboa</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dubboa</name>
<description>Demo project for Spring Boot</description>
<packaging>jar</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>dubboapi</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- dubbo依赖 -->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.14</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
<scope>compile</scope>
</dependency>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
UserController
package com.example.dubboa.controller;
import com.example.dubboapi.service.BService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @author
*/
@Controller
public class UserController {
//reference 在xml配置,这里需要用注解Autowired
@Autowired
public BService bService;
@RequestMapping("/test")
@ResponseBody
public String test(){
String s = bService.sayB("hhhhhhhhhhh");
return s;
}
}
AServiceImpl
package com.example.dubboa.service;
import com.example.dubboapi.service.AService;
//提供者提供的接口服务 service 采用xml配置
//@Service(interfaceClass = Xxx.class)
//@Component
public class AServiceImpl implements AService {
@Override
public String sayA(String s) {
return "sayA:" + s;
}
}
dubbo.xml (提供者消费者都在一个配置xml文件里,强调:配置文件中 port ,消费者 ,提供者的顺序不要变)
<?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">
<!--注意:port ,消费者 ,提供者的顺序不要变-->
<dubbo:registry address="zookeeper://127.0.0.1:2181" timeout="60000"/>
<!-- port值-1 表示让dubbo自行找一个可用的port -->
<dubbo:protocol port="-1" name="dubbo"/>
<dubbo:application name="dubbo"/>
<!--消费者-->
<!--关闭服务消费方所有服务的启动检查。dubbo缺省会在启动时检查依赖的服务是否可用,不可用时会抛出异常,阻止Spring初始化完成。url="dubbo//172.16.1.112:20880"-->
<dubbo:consumer check="false" />
<!--reference 采用xml配置实现,在代码中获取远程服务需要加注解@Autowired-->
<dubbo:reference id="bService" check="false" interface="com.example.dubboapi.service.BService"/>
<dubbo:annotation package="com.example.dubboa.*"/>
<!--提供者-->
<dubbo:service protocol="dubbo" ref="aService" interface="com.example.dubboapi.service.AService"/>
<bean id="aService" class="com.example.dubboa.service.AServiceImpl"/>
</beans>
application.properties
server.port=8884
启动类 DubboaApplication
package com.example.dubboa;
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
@EnableDubboConfiguration
@ImportResource(locations = {"classpath:dubbo.xml"})
@ComponentScan(basePackages = "com.example.dubboa.*")
@SpringBootApplication
public class DubboaApplication {
public static void main(String[] args) {
SpringApplication.run(DubboaApplication.class, args);
}
}
启动 dubboa
子模块 dubbob结构
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>
<parent>
<groupId>com.example</groupId>
<artifactId>springbootparent2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>dubbob</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dubbob</name>
<description>Demo project for Spring Boot</description>
<packaging>jar</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>dubboapi</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- dubbo依赖 -->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.14</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
<scope>compile</scope>
</dependency>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
UserController
package com.example.dubbob.controller;
import com.example.dubboapi.service.AService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @author
*/
@Controller
public class UserController {
//reference 在xml配置,这里需要用注解Autowired
@Autowired
public AService aService;
@RequestMapping("/test")
@ResponseBody
public String test(){
String s = aService.sayA("sssssssss");
return s;
}
}
BServiceImpl
package com.example.dubbob.service;
import com.example.dubboapi.service.BService;
//提供者提供的接口服务 service 采用xml配置
//@Service(interfaceClass = Xxx.class)
//@Component
public class BServiceImpl implements BService {
@Override
public String sayB(String s) {
return "sayB:" + s;
}
}
dubbo.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">
<!--注意:port ,消费者 ,提供者的顺序不要变-->
<dubbo:registry address="zookeeper://127.0.0.1:2181" timeout="60000"/>
<!-- port值-1 表示让dubbo自行找一个可用的port -->
<dubbo:protocol port="-1" name="dubbo"/>
<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<dubbo:application name="dubbo"/>
<!--关闭服务消费方所有服务的启动检查。dubbo缺省会在启动时检查依赖的服务是否可用,不可用时会抛出异常,阻止Spring初始化完成。url="dubbo//172.16.1.112:20880"-->
<dubbo:consumer check="false" />
<!--reference 采用xml配置实现,在代码中获取远程服务需要加注解@Autowired-->
<dubbo:reference id="aService" check="false" interface="com.example.dubboapi.service.AService"/>
<dubbo:annotation package="com.example.dubbob.*"/>
<!--提供者-->
<dubbo:service protocol="dubbo" ref="bService" interface="com.example.dubboapi.service.BService"/>
<bean id="bService" class="com.example.dubbob.service.BServiceImpl"/>
</beans>
application.properties
server.port=9994
启动类 DubbobApplication
package com.example.dubbob;
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
@EnableDubboConfiguration
@ImportResource(locations = {"classpath:dubbo.xml"})
@ComponentScan(basePackages = "com.example.dubbob.*")
@SpringBootApplication
public class DubbobApplication {
public static void main(String[] args) {
SpringApplication.run(DubbobApplication.class, args);
}
}
启动 dubbob
dubbo-amdin观察 和 web测试
具体提供者、消费者信息,这里就不再点进去看了
测试,浏览器输入 http://127.0.0.1:8884/test
测试,浏览器输入 http://127.0.0.1:9994/test
到此,springboot dubbo多模块,且子模块既有提供者又有消费者 的例子已经介绍完。
源码
链接:https://pan.baidu.com/s/1uHpzsJm9fbNfR0GsG8IQig
提取码:hy13
注意:运行的时候 请先确认安装运行了zookeeper 和 dubbo-admin ,如果没有安装可以看下我之前的文章 https://blog.csdn.net/a704397849/article/details/91904085
最后
springboot dubbo 使用了一段时间,有了一些心得体会。
1 dubbo 尽量用xml配置,不要使用注解(@Reference , @Service) 。简单的使用dubbo应用,注解容易实现,但复杂一点注解实现的问题就源源不断。
2 dubbo 提供者、消费者在同一个xml配置文件中有先后顺序的(上面pom.xml有提到),不要颠倒!
要想dubbo得心应手,dubbo源码势必要看。