Springboot
整合Apache Dubbo
一、 通过Docker
安装Zookeeper
docker-compose
的yaml
文件
version: '3'
services:
zookeeper:
image: zookeeper:3.6.1
container_name: zookeeper
restart: always
ports:
- 2181:2181
volumes:
- ./zookeeper/data:/data
- ./zookeeper/log:/datalog
networks:
- zk-esnet
networks:
zk-esnet:
driver: bridge
启动执行:
docker-compose up -d # 后台启动
二、整合springboot
和Apache Dubbo
2.1、创建一个maven
项目
项目结果如图:
2.2、引入依赖
root
模块的pom
文件
<?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>
<!--添加SpringBoot parent支持-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
</parent>
<groupId>com.code</groupId>
<artifactId>code-one</artifactId>
<packaging>pom</packaging>
<version>1.0.0</version>
<!--添加两个子模块-->
<modules>
<module>dubbo-provider</module>
<module>dubbo-consumer</module>
</modules>
<dependencies>
<!--添加SpringBoot测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-dependencies-zookeeper -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>2.7.8</version>
<type>pom</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
服务提供者模块的pom
文件
<?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>code-one</artifactId>
<groupId>com.code</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-provider</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies>
</project>
服务消费者模块的pom
文件
<?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>code-one</artifactId>
<groupId>com.code</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-consumer</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.code</groupId>
<artifactId>dubbo-provider</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies>
</project>
2.3、添加服务提供者相关代码
添加一个实体类:(必须实现序列化接口)
package com.dubbo.entity;
import java.io.Serializable;
/**
* @author 墨龙吟之天问
*/
public class User implements Serializable {
private static final long serialVersionUID = 565515108558415476L;
private Long id;
private String username;
private String password;
private Integer age;
// ... set / get ...
@Override
public String toString() {
return "User[" +
"id:" + id +
", username:'" + username + '\'' +
", password:'" + password + '\'' +
", age:" + age +
']';
}
}
添加service
接口和实现
import com.dubbo.entity.User;
import java.util.List;
/**
* @author 墨龙吟之天问
*/
public interface UserService {
/**
* 查询列表
* @return
*/
List<User> queryLists();
}
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import com.dubbo.entity.User;
import com.dubbo.service.UserService;
import java.util.ArrayList;
import java.util.List;
/**
* UserService的实现类
* @author 墨龙吟之天问
*/
@DubboService(version = "${dubbo.service.version}")
public class UserServiceImpl implements UserService {
@Override
public List<User> queryLists() {
// 构造一手假数据
List<User> users = new ArrayList<>();
for (int i = 0; i < 10; i++) {
User user = new User();
user.setAge(i * 10);
user.setId((long) i);
user.setPassword(RandomStringUtils.randomAlphanumeric(10));
user.setUsername(RandomStringUtils.randomAlphabetic(5));
users.add(user);
}
return users;
}
}
添加启动类和配置文件
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author 墨龙吟之天问
*/
@SpringBootApplication
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class);
}
}
server:
port: 7777
spring:
application:
name: dubbo-provider-service
dubbo:
service:
version: 1.0.0
application:
name: dubbo-provider-service
scan:
base-packages: com.dubbo.service.impl
# 注册中心的地址
registry:
address: zookeeper://192.168.31.25:2181
protocol:
name: dubbo
port: 20880
2.4、添加服务消费者者相关代码
编写一个controller
来调用远程service
:
import com.dubbo.entity.User;
import com.dubbo.service.UserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author 墨龙吟之天问
*/
@RestController
public class IndexController {
@DubboReference(version = "${dubbo.service.version}", loadbalance = "roundrobin")
private UserService userService;
@GetMapping("/user")
public List<User> index() {
return userService.queryLists();
}
}
编写配置文件将服务提供者的服务注册到zookeeper
中,然后写一个配置文件
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author 墨龙吟之天问
*/
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class);
}
}
server:
port: 7778
spring:
application:
name: dubbo-consumer-service
dubbo:
service:
version: 1.0.0
application:
name: dubbo-consumer-service
registry:
address: zookeeper://192.168.31.25:2181
2.5、启动服务
将IDEA的仪表板(dashboard
)启动,这个可以方便我们去统一管理服务。
访问接口:localhost:7778/user
[
{"id":0,"username":"oBhxn","password":"5VIogUrCG3","age":0}, {"id":1,"username":"DqYti","password":"XDShtZrszU","age":10}, {"id":2,"username":"FvRTn","password":"S7hug0oRWz","age":20}, {"id":3,"username":"BMVRN","password":"apwhk8llZw","age":30}, {"id":4,"username":"vHnWF","password":"14fp8mWbs8","age":40}, {"id":5,"username":"SNHkl","password":"hA5J0509YY","age":50}, {"id":6,"username":"yOAtC","password":"NQo50bo9mq","age":60}, {"id":7,"username":"QrgpW","password":"S49VOeOrk3","age":70}, {"id":8,"username":"zJSXK","password":"rR31TAuXJA","age":80}, {"id":9,"username":"TwQMM","password":"3Y3c0JlcpJ","age":90}
]
三、zookeeper
和dubbo
一些工具
3.1、zookeeper
监控工具:ZooInspector
下载地址:https://issues.apache.org/jira/secure/attachment/12436620/ZooInspector.zip
解压之后的build
目录下有一个start.bat
的脚本,双击启动。
连接之后就可以看到我们注册的服务了
这个版本比较旧,新版本的下载地址就在后面连接里面。
链接:https://pan.baidu.com/s/1jk5a1Wv_PPkQaJ8rmZERog
提取码:kqh4
3.2、Dubbo
监控工具dubbo-admin
这个工具我们依然在docker
中启动,我们在上面那个docker-compose.yml
文件中增加一个apache-dubbo-admin
的容器。
version: '3'
services:
zookeeper:
image: zookeeper:3.6.1
container_name: zookeeper
restart: always
ports:
- 2181:2181
volumes:
- ./zookeeper/data:/data
- ./zookeeper/log:/datalog
networks:
- zk-esnet
dubbo-admin:
container_name: dubbo-admin
image: apache/dubbo-admin
links:
- zookeeper:zookeeper
depends_on:
- zookeeper
ports:
- "8080:8080"
environment:
- admin.registry.address=zookeeper://zookeeper:2181
- admin.config-center=zookeeper://zookeeper:2181
- admin.metadata-report.address=zookeeper://zookeeper:2181
restart: always
networks:
- zk-net
networks:
zk-esnet:
driver: bridge
启动之后访问:IP:8080
就可以进入管理页面