This is based on Dubbo + RPC remote invocation framework demo Zookeeper, I hope the reader can read this article would be able to build a simple framework. I will continue to follow updates related articles micro Services Architecture (Spring Boot, Spring Cloud), distributed architecture (Dobbo + Zookeeper), big data architecture and source code analysis or the like, are interested can look at.
Dubbo Alibaba open source is a high performance excellent service framework, so that the application can realize output and input functions and services through the RPC performance, it can be seamlessly integrated with the Spring Framework.
Dubbo is a high performance, lightweight open-source Java RPC framework, which provides three core capabilities: an interface for remote method invocation, intelligent fault tolerance and load balancing, and automatic registration and service discovery.
- provider: expose service provider service
- consumer: call the remote service consumer services
- registry: registered in the service registry found
- monitor: Statistical monitoring service call center number and time of call
- container: container service runs
A, dubbo-provider (service provider)
1、Java Bean
Why implement Serializable? When we need the status information of the object is transmitted over the network, or status information of the object need to be persistent, we need to serialize objects to use in the future.
Lombok is used, it is by way of annotation, the compiler automatically generates attribute constructors, getter / setter, equals, hashcode , toString method.
@Data
public class UserInfo implements Serializable {
private String account;
private String password;
}
2、UserService
Service provider service exposed, will be registered to the zookeeper.
public interface UserService {
// 定义用户登录的api
UserInfo login(UserInfo user);
}
3、UserServiceImpl
Service corresponding to the service provider implementation class exposed.
@Component
@Service(interfaceClass = UserService.class)
public class UserServiceImpl implements UserService {
public UserInfo login(UserInfo user) {
UserInfo reUser = new UserInfo();
reUser.setAccount("登录的账号为:"+user.getAccount());
reUser.setPassword("登录的密码为:"+user.getPassword());
return reUser;
}
}
4、DubboProviderApplication
@SpringBootApplication
@EnableDubboConfiguration // 启用dubbo自动配置
public class DubboProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DubboProviderApplication.class, args);
}
}
5, pom.xml
<?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>
<groupId>com.boot.dubbo.demo</groupId>
<artifactId>dubbo-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>dubbo-provider</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--Dubbo依赖-->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!--自动生成getter,setter,equals,hashCode和toString等等-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
<scope>provided</scope>
</dependency>
<!--Zookeeper客户端-->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<!--Zookeeper依赖,排除log4j避免依赖冲突-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.10</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
6, application.yml
spring:
dubbo:
application:
name: dubbo-provider
protocol:
name: dubbo
port: 20880
registry:
address: zookeeper://127.0.0.1:2181
Two, dubbo-consumer (service consumer)
1、UserController
@RestController
public class UserController {
@Reference // 引用dubbo服务器提供服务器接口
private UserService userService;
@GetMapping("/login")
public UserInfo login(UserInfo userInfo) {
return userService.login(userInfo);
}
}
2、DubboConsumerApplication
@SpringBootApplication
@EnableDubboConfiguration // 启用dubbo自动配置
public class DubboConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(DubboConsumerApplication.class, args);
}
}
3, pom.xml
<?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>
<groupId>com.boot.dubbo.demo</groupId>
<artifactId>dubbo-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>dubbo-consumer</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--依赖dubbo-provider服务提供者-->
<dependency>
<groupId>com.boot.dubbo.demo</groupId>
<artifactId>dubbo-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!--Dubbo依赖-->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!--Zookeeper客户端-->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<!--Zookeeper依赖,排除log4j避免依赖冲突-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.10</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4, application.yml
spring:
dubbo:
application:
name: dubbo-consumer
protocol:
name: dubbo
port: 20880
registry:
address: zookeeper://127.0.0.1:2181
server:
port: 8081
Three, Zookeeper installation configuration
"What Zookeeper that? "
" Zookeeper installation configuration "
" Zookeeper clustered environment to build. "
Fourth, the project start
V. View Zookeeper registration of service
Sixth, through the browser sends a request (dubbo-provider services registered on the Zookeeper, dubbo-consumer Consumers can call a registered service).
This, rpc call service through the remote! Interested can go to learn next.
Want demo source code can leave e-mail address.