SpringBoot2.x整合Dubbo(直连模式)
SpringBoot 是一种快速开发框架,帮助我们快速整合第三方框架(Maven方式继承)、完全采用注解化(使用注解方式启动SpringMvc),简化XML 内置Http服务(tomcat、jetty)最终以java应用启动;
dubbot 是RPC远程调用技术中的一种框架,解决SOA架构服务于服务之间通讯的框架;
dubbo 原理: https://blog.csdn.net/qq_33101675/article/details/78701305
目录结如下:
springboot-inteface 接口模块: JAR
package com.ysl.inteface;
public interface IndexService { 定义接口
public String index(); 测试方法
}
<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.ysl</groupId>
<artifactId>springboot-dubbo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>springboot-inteface</artifactId>
</project>
springboot-provider 接口实现模块: JAR 生产者
package com.ysl.service.impl;
import org.springframework.stereotype.Service;
import com.ysl.inteface.IndexService;
@Service("indexService") 注意service注解所属包 indexService 装配名字
public class IndexServiceImpl implements IndexService {
@Override
public String index() {
// TODO Auto-generated method stub
return "SUCCESS...";
}
}
<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.ysl</groupId>
<artifactId>springboot-dubbo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>springboot-provider</artifactId>
<dependencies>
<dependency>
<groupId>com.ysl</groupId>
<artifactId>springboot-inteface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
application.properties
# Spring boot application
spring.application.name = dubbo-provider-demo
# Spring boot port
server.port = 9090
customer-provider-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">
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="customer-provider" />
<!-- dubbo间的直连 -->
<dubbo:registry address="N/A" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" host="127.0.0.1" accesslog="true" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.ysl.inteface.IndexService" ref="indexService" version="1.0.0" />
</beans>
springboot-sustomer 视图模块: JAR 消费者
package com.ysl.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ysl.inteface.IndexService;
@RestController
public class IndexController {
@Autowired
private IndexService indexService;
@GetMapping("/index")
public void index() {
System.out.println(indexService.index());
}
}
<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.ysl</groupId>
<artifactId>springboot-dubbo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>springboot-sustomer</artifactId>
<dependencies>
<dependency>
<groupId>com.ysl</groupId>
<artifactId>springboot-inteface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
application.properties
# Spring boot application crm-customer-consumer
spring.application.name = dubbo-consumer-demo
server.port = 8080
font-consumer-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">
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="front" />
<!-- dubbo间的直连 -->
<dubbo:registry address="N/A" />
<!-- 生成远程服务代理 -->
<dubbo:reference id="indexService"
interface="com.ysl.inteface.IndexService" url="dubbo://127.0.0.1:20880"
timeout="600000" version="1.0.0" />
<!--关闭所有消费者启动时检查 -->
<dubbo:consumer check="false" />
</beans>
springboot-provider 模块启动类加上 加载dubbo配置
@ImportResource(locations="classpath:customer-provider-dubbo.xml")
springboot-sustomer 模块启动类加上 加载dubbo配置
@ImportResource(locations="classpath:font-consumer-dubbo.xml")
父工程pom:
<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.ysl</groupId>
<artifactId>springboot-dubbo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>springboot-dubbo</name>
<modules>
<module>springboot-inteface</module>
<module>springboot-sustomer</module>
<module>springboot-provider</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<!-- Spring Boot Dubbo 依赖 -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
</dependencies>
<!-- 解决依赖问题 -->
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- java编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
收工~~~
springboot-dubbo 代码地址:
https://github.com/SK-II/springboot-dubbo