dubbo入门示例很简单,服务端在数据库中查询user数据,消费端会调用服务端的服务获得用户数据。示例demo下载地址:https://download.csdn.net/download/zc_ad/10775010,项目的sql目录下有所需实验的sql文件。
代码目录解结构:
接口定义:在venue-dubbbo-api子项目下:
public interface UserService {
Response queryUserByCode(String userCode);
}
venue-dubbbo-manager子项目:
服务端maven依赖:
<?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>venue-dubbo</artifactId>
<groupId>com.xichuan.dubbo</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>venue-dubbbo-manager</artifactId>
<dependencies>
<dependency>
<groupId>com.xichuan.dubbo</groupId>
<artifactId>venue-dubbbo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
服务端配置:
注意的配置是服务端端口,zookeeper服务的地址,以及协议、和提供服务所在的包
server:
port: 2000
## Dubbo 服务提供者配置
spring:
profiles:
active: dev
dubbo:
application:
name: manager
server: true
registry:
id: xichuan
address: zookeeper://127.0.0.1:2181
protocol:
name: dubbo
port: 20881
scan:
base=packages: com.xichuan.dubbo.service
---
spring:
profiles: dev
master_key: ikldnm4e1rVZKfAV1JPC
datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/xichuan?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
# 连接池的配置信息
# 初始化大小,最小等待连接数量,最大等待连接数量,最大连接数
initialSize: 1
minIdle: 1
maxActive: 20
# 配置获取连接等待超时的时间
maxWait: 60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
timeBetweenEvictionRunsMillis: 60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: true
testOnReturn: false
# 打开PSCache,并且指定每个连接上PSCache的大小
poolPreparedStatements: false
maxPoolPreparedStatementPerConnectionSize: 20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
filters: stat,wall,log4j
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# 合并多个DruidDataSource的监控数据
#spring.datasource.useGlobalDataSourceStat=true
jpa:
database : MYSQL
show-sql : true
hibernate:
ddl-auto: update
properties:
hibernate.format_sql: true
hibernate.naming.physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
hibernate.cache.use_second_level_cache: false
hibernate.search.default.directory_provider: filesystem
hibernate.search.default.indexBase: ./indexes
open-in-view: true
druid:
allow:
ip: 127.0.0.1
login:
user_name: root
password: root
在启动类上添加@EnableDubboConfiguration注解
@SpringBootApplication
@EnableDubboConfiguration
public class ManagerApplication {
public static void main(String[] args) {
SpringApplication.run(ManagerApplication.class);
}
}
提供服务的实现代码,只需要使用@Service注解即可,需要参数是版本号和接口类
import com.alibaba.dubbo.config.annotation.Service;
import com.xichuan.dubbo.common.response.ExtraResponse;
import com.xichuan.dubbo.common.response.Response;
import com.xichuan.dubbo.repository.UserRepository;
import com.xichuan.dubbo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* Created by XiChuan on 2018-08-14.
*/
@Service(version = "1.0.0",interfaceClass = UserService.class)
@Component
public class UserServiceImpl implements UserService {
@Autowired
UserRepository userRepository;
@Override
public Response queryUserByCode(String userCode) {
return new ExtraResponse(userRepository.queryByUserCodeAndStatus(userCode,1));
}
}
venue-dubbbo-api子项目:
消费端maven依赖:
<?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>venue-dubbo</artifactId>
<groupId>com.xichuan.dubbo</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>venue-dubbbo-consumer</artifactId>
<dependencies>
<dependency>
<groupId>com.xichuan.dubbo</groupId>
<artifactId>venue-dubbbo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<!--swagger2-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<!--springfox-swagger-ui http://localhost:8010/swagger-ui.html-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
消费端配置文件,只需要太那几zookeeper地址即可。
server:
port: 2001
## Dubbo 服务提供者配置
spring:
profiles:
active: dev
dubbo:
application:
name: customer
registry:
id: xichuan
address: zookeeper://127.0.0.1:2181
---
spring:
profiles: dev
master_key: ikldnm4e1rVZKfAV1JPC
swagger:
enable: true
info:
version: 0.1
title: 兮川的接口
description: 对工作流操作的一系列接口
user_name: XiChuan
url:
email:
消费端启动类中加上@EnableDubboConfiguration注解
@SpringBootApplication
@EnableDubboConfiguration
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class);
}
}
使用服务端提供的服务获取数据,只需要使用@Reference注解,并加上版本号即可
import com.alibaba.dubbo.config.annotation.Reference;
import com.xichuan.dubbo.common.response.Response;
import com.xichuan.dubbo.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by XiChuan on 2018-08-14.
*/
@RestController
@RequestMapping("/dubbo/test")
@Api(tags = "对dubbo进行测试")
public class UserController {
@Reference(version = "1.0.0")
private UserService userService;
@GetMapping("/{user_code}")
@ApiOperation("通过user_code获得用户信息")
public Response sayHello(@PathVariable("user_code") String userCode) {
return userService.queryUserByCode(userCode);
}
}
上述代码只是主要代码,关于swagger2的配置,与数据库的配置,还有基本类的封装详细请下载代码细看。
运行:必须按照下面数据运行
1.运行zookeeper
2.运行ManagerApplication
3.运行ConsumerApplication
4.在浏览器中输入:http://localhost:2001/swagger-ui.html,输入code=2001,显示该用户的详细信息
服务端日志:
消费端日志:
原文链接:https://blog.csdn.net/zc_ad/article/details/83898168