forked from zhoustone/middle-ware-parent
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SpringBoot2.0 整合 Dubbo框架 ,实现RPC服务远程调用
- Loading branch information
cicadasmile
committed
Jul 7, 2019
1 parent
e428fe1
commit 97d8869
Showing
19 changed files
with
476 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
9 changes: 9 additions & 0 deletions
9
ware-dubbo-parent/block-dubbo-common/src/main/java/com/boot/common/DubboService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) ; | ||
} |
28 changes: 28 additions & 0 deletions
28
ware-dubbo-parent/block-dubbo-common/src/main/java/com/boot/common/UserEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
ware-dubbo-parent/block-dubbo-common/src/main/java/com/boot/common/VersionService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.boot.common; | ||
|
||
public interface VersionService { | ||
String getVersion () ; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
13 changes: 13 additions & 0 deletions
13
ware-dubbo-parent/block-dubbo-consume/src/main/java/com/boot/consume/ConsumeApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) ; | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
...parent/block-dubbo-consume/src/main/java/com/boot/consume/controller/DubboController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() ; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...bbo-parent/block-dubbo-consume/src/main/java/com/boot/consume/service/ConsumeService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...parent/block-dubbo-consume/src/main/java/com/boot/consume/service/VersionServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() ; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
ware-dubbo-parent/block-dubbo-consume/src/main/resources/application.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
19 changes: 19 additions & 0 deletions
19
...dubbo-parent/block-dubbo-provider/src/main/java/com/boot/consume/ProviderApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) ; | ||
} | ||
} |
Oops, something went wrong.