如何上手使用duboo-springcloud
dubbo-springcloud
直连模式
依赖引入
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring-boot.version>2.1.1.RELEASE</spring-boot.version>
<dubbo.version>2.7.1</dubbo.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Apache Dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-bom</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
</dependency>
<!-- Dubbo Spring Boot Starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<dependency>
<groupId>com.ognice</groupId>
<artifactId>dubbo-provider-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
接口定义
@Data
@Accessors(chain = true)
public class User implements Serializable {
private String username;
private String password;
private Integer age;
private String idCard;
}
public interface UserService {
public User login(String username, String password);
}
提供者实现
import org.apache.dubbo.config.annotation.Service;
@Service
public class UserServiceImpl implements UserService {
@Override
public User login(String username, String password) {
// TODO
return new User().setUsername("dubbo");
}
}
// 启动
@SpringBootApplication
public class ProviderApp {
public static void main(String[] args) {
SpringApplication.run(ProviderApp.class, args);
}
}
提供者应用配置
# Base packages to scan Dubbo Component: @org.apache.dubbo.config.annotation.Service
dubbo.scan.base-packages=com.ognice
# Dubbo Protocol
dubbo.protocol.name=dubbo
dubbo.protocol.port=12345
## Dubbo Registry
dubbo.registry.address=N/A
消费者应用
@SpringBootApplication
@Slf4j
public class ConsumerApp {
public static void main(String[] args) {
SpringApplication.run(ConsumerApp.class, args);
}
// 通过服务地址直连
@Reference(url = "dubbo://127.0.0.1:12345")
UserService userService;
@Bean
public ApplicationRunner runner() {
return args -> {
log.info("user {}", userService.login("xxx", "yyy"));
};
}
}
消费日志:
2019-07-07 15:27:17.325 INFO 8960 --- [ main] com.ognice.ConsumerApp : Started ConsumerApp in 2.21 seconds (JVM running for 2.746)
2019-07-07 15:27:17.413 INFO 8960 --- [main] com.ognice.ConsumerApp : user User(username=dubbo, password=***, age=18, idCard=40000XXXXXX)
Zookeeper注册中心
本地启动zk服务,默认启动端口2181
依赖引入
<dependencyManagement>
<dependencies>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Apache Dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-bom</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- Spring Boot dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
</dependency>
<!-- Dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<dependency>
<groupId>com.ognice</groupId>
<artifactId>dubbo-provider-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- Zookeeper dependencies -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
</dependency>
</dependencies>
提供者配置
server.port=0
# Spring boot application
spring.application.name=dubbo-provider-zookeeper
# Base packages to scan Dubbo Component: @org.apache.dubbo.config.annotation.Service
dubbo.scan.base-packages=com.ognice.service
# Dubbo Application
## The default value of dubbo.application.name is ${spring.application.name}
## dubbo.application.name=${spring.application.name}
embedded.zookeeper.port=2181
# Dubbo Protocol
dubbo.protocol.name=dubbo
## Random port
dubbo.protocol.port=-1
## Dubbo Registry
dubbo.registry.address=zookeeper://127.0.0.1:${embedded.zookeeper.port}
dubbo.registry.file=${user.home}/dubbo-cache/${spring.application.name}/dubbo.cache
提供者实现与直连模式一致
消费者配置
server.port=0
# Spring boot application
spring.application.name=dubbo-consumer-zookeeper
# Base packages to scan Dubbo Component: @org.apache.dubbo.config.annotation.Service
dubbo.scan.base-packages=com.ognice
# Dubbo Application
## The default value of dubbo.application.name is ${spring.application.name}
## dubbo.application.name=${spring.application.name}
embedded.zookeeper.port=2181
# Dubbo Protocol
dubbo.protocol.name=dubbo
## Random port
dubbo.protocol.port=-1
## Dubbo Registry
dubbo.registry.address=zookeeper://127.0.0.1:${embedded.zookeeper.port}
dubbo.registry.file=${user.home}/dubbo-cache/${spring.application.name}/dubbo.cache
消费者调用
@SpringBootApplication
@Slf4j
public class ZkConsumerApp {
public static void main(String[] args) {
SpringApplication.run(ZkConsumerApp.class, args);
}
@Reference
UserService userService;
@Bean
public ApplicationRunner runner() {
return args -> {
log.info("user {}", userService.login("xxx", "yyy"));
};
}
}
消费日志
2019-07-07 15:35:45.347 INFO 11668 --- [main] com.ognice.ZkConsumerApp : Started ZkConsumerApp in 5.395 seconds (JVM running for 6.053)
2019-07-07 15:35:46.938 INFO 11668 --- [main] com.ognice.ZkConsumerApp : user User(username=dubbo-zk, password=***, age=18, idCard=40000XXXXXX)
Nacos注册中心
本地启动nacos服务端
默认启动端口8848
依赖引入
<properties>
<nacos.version>1.0.0</nacos.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Apache Dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-bom</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- Spring Boot dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
</dependency>
<!-- Dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<dependency>
<groupId>com.ognice</groupId>
<artifactId>dubbo-provider-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- Dubbo Registry Nacos -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>${nacos.version}</version>
</dependency>
</dependencies>
提供者配置
server.port=0
# Spring boot application
spring.application.name=dubbo-provider-nacos
# Base packages to scan Dubbo Component: @org.apache.dubbo.config.annotation.Service
dubbo.scan.base-packages=com.ognice
# Dubbo Application
## The default value of dubbo.application.name is ${spring.application.name}
# Dubbo Protocol
dubbo.protocol.name=dubbo
## Random port
dubbo.protocol.port=-1
## Dubbo Registry
nacos.server-address = 127.0.0.1
nacos.port = 8848
dubbo.registry.address=nacos://${nacos.server-address}:${nacos.port}
提供者实现UserService接口,与直连模式一致
消费者配置
server.port=0
# Spring boot application
spring.application.name=dubbo-consumer-nacos
# Base packages to scan Dubbo Component: @org.apache.dubbo.config.annotation.Service
dubbo.scan.base-packages=com.ognice
# Dubbo Application
## The default value of dubbo.application.name is ${spring.application.name}
# Dubbo Protocol
dubbo.protocol.name=dubbo
## Random port
dubbo.protocol.port=-1
## Dubbo Registry
nacos.server-address = 127.0.0.1
nacos.port = 8848
dubbo.registry.address=nacos://${nacos.server-address}:${nacos.port}
消费端调用
@SpringBootApplication
@Slf4j
public class NacosConsumerApp {
public static void main(String[] args) {
SpringApplication.run(NacosConsumerApp.class, args);
}
@Reference
UserService userService;
@Bean
public ApplicationRunner runner() {
return args -> {
log.info("user {}", userService.login("xxx", "yyy"));
};
}
}
调用日志
2019-07-07 15:46:34.501 INFO 13352 --- [main] com.ognice.NacosConsumerApp : Started NacosConsumerApp in 3.804 seconds (JVM running for 4.352)
2019-07-07 15:46:34.559 INFO 13352 --- [main] com.ognice.NacosConsumerApp : user User(username=dubbo-nacos, password=***, age=18, idCard=40000XXXXXX)
网友评论