Skip to content

Commit

Permalink
SpringBoot2.0 整合 Dubbo框架 ,实现RPC服务远程调用
Browse files Browse the repository at this point in the history
  • Loading branch information
cicadasmile committed Jul 7, 2019
1 parent e428fe1 commit 97d8869
Show file tree
Hide file tree
Showing 19 changed files with 476 additions and 1 deletion.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
<a href="https://mp.weixin.qq.com/s?__biz=MzU4Njg0MzYwNw==&mid=2247483774&idx=1&sn=e5c2eed4a989bedd5c3b108c7dc18c37&chksm=fdf455c6ca83dcd0bccf71aa07639b8ef514e3b6e6d85c6aeb4bceae30d58f021c9d7ba8590b&token=261735462&lang=zh_CN#rd">
5、SpringBoot2.0 整合 QuartJob ,实现定时器实时管理</a><br/>

<a href="">
<a href="https://mp.weixin.qq.com/s?__biz=MzU4Njg0MzYwNw==&mid=2247483886&idx=1&sn=2158323687acc7713d5d45c21c9558ee&chksm=fdf45556ca83dc409f1c7ce282b4297a0bed9df594796fd921c719f0c77896a183098cb43159&token=872954754&lang=zh_CN#rd">
6、SpringBoot2.0 整合 Redis集群 ,实现消息队列场景</a><br/>

<a href="">
7、SpringBoot2.0 整合 Dubbo框架 ,实现RPC服务远程调用</a><br/>

持续更新中...

## 项目简介
Expand Down
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
<module>ware-swagger-two</module>
<!-- RedisCluster 集群 -->
<module>ware-redis-cluster</module>
<!-- Dubbo 框架 -->
<module>ware-dubbo-parent</module>
</modules>

<!-- 依赖版本管理 -->
Expand Down
15 changes: 15 additions & 0 deletions ware-dubbo-parent/block-dubbo-common/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?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">
<parent>
<artifactId>ware-dubbo-parent</artifactId>
<groupId>com.dubbo.parent</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.dubbo.common</groupId>
<artifactId>block-dubbo-common</artifactId>
<packaging>jar</packaging>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.boot.common;

public interface DubboService {
String getInfo (String param) ;

UserEntity getUserInfo (UserEntity userEntity) ;

String timeOut (Integer time) ;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.boot.common;

import java.io.Serializable;

public class UserEntity implements Serializable {
private Integer id ;
private String userName ;

@Override
public String toString() {
return "UserEntity{id=" + id +", userName='" + userName + "}";
}
public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.boot.common;

public interface VersionService {
String getVersion () ;
}
45 changes: 45 additions & 0 deletions ware-dubbo-parent/block-dubbo-consume/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?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">
<parent>
<artifactId>ware-dubbo-parent</artifactId>
<groupId>com.dubbo.parent</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.dubbo.consume</groupId>
<artifactId>block-dubbo-consume</artifactId>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>com.dubbo.common</groupId>
<artifactId>block-dubbo-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>

<!-- 项目构建 -->
<build>
<finalName>${project.artifactId}</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.boot.consume;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@EnableDubbo
@SpringBootApplication
public class ConsumeApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumeApplication.class,args) ;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.boot.consume.controller;

import com.boot.common.UserEntity;
import com.boot.consume.service.ConsumeService;
import com.boot.consume.service.VersionServiceImpl;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/*
http://localhost:7008/getParam
响应参数:[Hello,Cicada]
http://localhost:7008/getUserInfo
响应参数:{"id":1,"userName":"知了"}
*/
@RestController
public class DubboController {

@Resource
private ConsumeService consumeService ;
@Resource
private VersionServiceImpl versionService1 ;
@Resource
private VersionServiceImpl versionService2 ;

@RequestMapping("/getParam")
public String getParam (){
return consumeService.getInfo("知了...") ;
}

@RequestMapping("/getUserInfo")
public UserEntity getUserInfo (){
UserEntity userEntity = new UserEntity() ;
userEntity.setId(1);
userEntity.setUserName("知了");
return consumeService.getUserInfo(userEntity) ;
}

/**
* 抛出超时异常
* com.alibaba.dubbo.remoting.TimeoutException
*/
@RequestMapping("/timeOut")
public String timeOut (){
return consumeService.timeOut(2000) ;
}

/**
* 测试接口版本
* 启动日志
* <dubbo:reference object="com.alibaba.dubbo.common.bytecode.proxy1@3ad65"
* singleton="true"
* interface="com.boot.common.VersionService"
* uniqueServiceName="com.boot.common.VersionService:1.0.0"
* generic="false" version="1.0.0"
* id="com.boot.common.VersionService" /> has been built.
*/
@RequestMapping("/getVersion1")
public String getVersion1 (){
return versionService1.getVersion() ;
}

@RequestMapping("/getVersion2")
public String getVersion2 (){
return versionService2.version2() ;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.boot.consume.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.boot.common.DubboService;
import com.boot.common.UserEntity;
import org.springframework.stereotype.Service;

@Service
public class ConsumeService implements DubboService {

@Reference
private DubboService dubboService ;

@Override
public String getInfo(String param) {
return dubboService.getInfo(param);
}

@Override
public UserEntity getUserInfo(UserEntity userEntity) {
return dubboService.getUserInfo(userEntity);
}

@Override
public String timeOut(Integer time) {
return dubboService.timeOut(time);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.boot.consume.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.boot.common.VersionService;
import org.springframework.stereotype.Service;

@Service
public class VersionServiceImpl implements VersionService {

@Reference(version = "1.0.0")
private VersionService versionService1 ;

@Reference(version = "2.0.0")
private VersionService versionService2 ;

@Override
public String getVersion() {
return versionService1.getVersion();
}

public String version2 (){
return versionService2.getVersion() ;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
server:
tomcat:
uri-encoding: UTF-8
max-threads: 1000
min-spare-threads: 30
port: 7008
connection-timeout: 5000ms

spring:
application:
name: block-dubbo-consume
# Dubbo 配置文件
dubbo:
application:
name: block-dubbo-consume
registry:
address: 127.0.0.1:2181
protocol: zookeeper
# 基于服务消费方配置超时
consumer:
timeout: 1000
45 changes: 45 additions & 0 deletions ware-dubbo-parent/block-dubbo-provider/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?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">
<parent>
<artifactId>ware-dubbo-parent</artifactId>
<groupId>com.dubbo.parent</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.dubbo.provider</groupId>
<artifactId>block-dubbo-provider</artifactId>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>com.dubbo.common</groupId>
<artifactId>block-dubbo-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>

<!-- 项目构建 -->
<build>
<finalName>${project.artifactId}</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.boot.consume;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/*
[zk: localhost:2181(CONNECTED) 0] ls /
[dubbo, zookeeper]
[zk: localhost:2181(CONNECTED) 1] ls /dubbo
[com.boot.common.DubboService]
[zk: localhost:2181(CONNECTED) 2]
*/
@EnableDubbo
@SpringBootApplication
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class,args) ;
}
}
Loading

0 comments on commit 97d8869

Please sign in to comment.