dubbo 之 入門示例(springboot)
阿新 • • 發佈:2018-11-29
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,顯示該使用者的詳細資訊
服務端日誌:
消費端日誌: