1. 依赖工具
- SpringBoot2.2.1
- Dubbo2.7.4.1
- DubboAdmin(20191208最新版)
2. Springboot-Dubbo-Server
- 主要的依赖
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.codingvip</groupId>
<artifactId>sdu-dubbo-client-boot</artifactId>
<version>1.0-SNAPSHOT</version>
<name>sdu-dubbo-client-boot</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.codingvip</groupId>
<artifactId>stu-dubbo-client</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.4.1</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-zookeeper</artifactId>
<version>2.7.4.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 1、接口实现Demo
@Service
@Component
public class UserServiceImpl implements UserService {
@Override
public String getName(int id) {
return null;
}
@Override
public User getUserById(int id) {
User user = new User();
user.setUserName("coding:Demo:"
user.setId(id);
return user;
}
@Override
public User getUser(UserParam param) {
return null;
}
}
- 2、启动类
@EnableDubbo
@SpringBootApplication
public class SduDubboServerBootApplication {
public static void main(String[] args) {
SpringApplication.run(SduDubboServerBootApplication.class, args);
}
}
- 3、application.properties
dubbo.application.name=simple
dubbo.registry.address=zookeeper://localhost:2181
dubbo.protocol.name=dubbo
dubbo.protocol.port=-1
- 4、启动结查如下:
2019-12-08 22:31:53.878 INFO 73677 --- [localhost:2181)] org.apache.zookeeper.ClientCnxn : Opening socket connection to server localhost/0:0:0:0:0:0:0:1:2181. Will not attempt to authenticate using SASL (unknown error)
2019-12-08 22:31:53.878 INFO 73677 --- [ main] o.a.c.f.imps.CuratorFrameworkImpl : Default schema
2019-12-08 22:31:53.907 INFO 73677 --- [localhost:2181)] org.apache.zookeeper.ClientCnxn : Socket connection established to localhost/0:0:0:0:0:0:0:1:2181, initiating session
2019-12-08 22:31:53.922 INFO 73677 --- [localhost:2181)] org.apache.zookeeper.ClientCnxn : Session establishment complete on server localhost/0:0:0:0:0:0:0:1:2181, sessionid = 0x1000156f6df0014, negotiated timeout = 40000
2019-12-08 22:31:53.930 INFO 73677 --- [ain-EventThread] o.a.c.f.state.ConnectionStateManager : State change: CONNECTED
2019-12-08 22:31:54.483 INFO 73677 --- [ main] c.c.s.SduDubboServerBootApplication : Started SduDubboServerBootApplication in 3.565 seconds (JVM running for 4.918)
2019-12-08 22:31:54.487 INFO 73677 --- [pool-1-thread-1] .b.c.e.AwaitingNonWebApplicationListener : [Dubbo] Current Spring Boot Application is await...
3. Springboot-Dubbo-Client
- 主要的依赖
与server端的依赖何持一致即可:
- 主要的依赖
- 启动类
@EnableDubbo
@SpringBootApplication
public class SduDubboClientBootApplication {
@Reference
UserService userService;
public static void main(String[] args) { SpringApplication.run(SduDubboClientBootApplication.class, args).close();
System.out.println("调用完成");
}
@Bean
public ApplicationRunner getBean() {
return args -> {
System.out.println(userService.getUserById(123));
};
}
}
- application.properties
测试过程中发现服务总是无法调通。调整了一下dubbo.reference.timeout即可;
- application.properties
dubbo.application.name=youngMan
dubbo.registry.address=zookeeper://localhost:2181
dubbo.reference.timeout=5000
- 4.启动结查如下:
User{id=11, userName='coding:Demo:', age=0, sex='null'}
2019-12-08 22:34:55.429 INFO 74177 --- [pool-1-thread-1] .b.c.e.AwaitingNonWebApplicationListener : [Dubbo] Current Spring Boot Application is await...
2019-12-08 22:34:55.430 INFO 74177 --- [ main] .b.c.e.AwaitingNonWebApplicationListener : [Dubbo] Current Spring Boot Application is about to shutdown...
2019-12-08 22:34:55.438 INFO 74177 --- [tor-Framework-0] o.a.c.f.imps.CuratorFrameworkImpl : backgroundOperationsLoop exiting
2019-12-08 22:34:55.444 INFO 74177 --- [ain-EventThread] org.apache.zookeeper.ClientCnxn : EventThread shut down for session: 0x1000156f6df0016
2019-12-08 22:34:55.444 INFO 74177 --- [ main] org.apache.zookeeper.ZooKeeper : Session: 0x1000156f6df0016 closed
2019-12-08 22:34:55.452 INFO 74177 --- [ main] f.a.ReferenceAnnotationBeanPostProcessor : org.apache.dubbo.common.bytecode.proxy0@6ada9c0c was destroying!
2019-12-08 22:34:55.452 INFO 74177 --- [ main] f.a.ReferenceAnnotationBeanPostProcessor : class org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor was destroying!
调用完成
断开与目标 VM 的连接,地址:'127.0.0.1:62058', transport: 'socket'
4. Dubbo-admin
- 下载最新版的dubbo-admin:
git clone https://github.com/apache/dubbo-admin
- 启动dubbo-admin-server
注意为避免端口冲突,修改一下
/resource/application.properties中
server.port=8888
- 启动dubbo-admin-server
2019-12-08 21:55:06.917 INFO 65028 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8888 (http) with context path ''
2019-12-08 22:16:21.561 INFO 65028 --- [nio-8888-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2019-12-08 22:16:21.581 INFO 65028 --- [nio-8888-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 20 ms
- 启动dubbo-admin-ui
此步比较麻烦一些:
- 启动dubbo-admin-ui
- 修改config/index.js:
proxyTable: {
'/': {
target: 'http://localhost:8888/',
changeOrigin: true,
pathRewrite: {
'^/': '/'
}
}
},
- node安装:
在dubbo-admin-ui目录下执行如下命令:
npm install --registry=https://registry.npm.taobao.org
待执行完成后运行:
npm run dev
结果如下:
DONE Compiled successfully in 18504ms 10:15:55 PM
Your application is running here: http://localhost:8081