Summary:
Node Role description
node | The role that |
---|---|
Provider | The service provider that exposes the service |
Consumer | Service consumer that invokes the remote service |
Registry | A registry for service registration and discovery |
Monitor | A monitoring center that collects statistics on service invocation times and invocation time |
Container | The container in which the service runs |
Call Relationship Description
- The Service Container is responsible for starting, loading, and running the service provider.
- When a service Provider is started, it registers its services with Registry.
- Service consumers subscribe to the Registry for the services they require at startup time.
- The Registry returns a list of service providers’ addresses to consumers, and if there are changes, the Registry pushes change data to consumers based on long connections.
- The service consumer selects one provider from the provider address list based on the soft load balancing algorithm, and selects another one if the invocation fails.
- Service consumers and providers count the number and time of calls in memory, and send the statistics to Monitor every minute.
The project build
The development environment mainly involves the following aspects:
- Spring Boot
- JDK 8
- Dubbo 2.7.1
- Zookeeper
See github’s Dubbo module at github.com/UniqueDong/…
Dubbo API
Define service interfaces, jar them for consumers to rely on, and implement them. The project only has interface definitions and Model objects. @Data is a feature provided by the Lombok open source library for easy development.
- Model object definition:
@Data
public class User implements Serializable {
private Long id;
private String username;
}
Copy the code
- Provider interface definitions:
public interface UserProvider {
List<User> listUser(a);
}
Copy the code
Provider Service Provider
- Pom depends on:
Dubo-api is the jar for spring-boot-starter. Dubo-spring-boot-starter is the jar for dubo-dependencies -zookeeper.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-logging</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>zero.springboot.study</groupId>
<artifactId>dubbo-api</artifactId>
<version>1.0.0 - the SNAPSHOT</version>
</dependency>
<! --dubbo start-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.1</version>
</dependency>
<! -- Zookeeper dependencies -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>2.7.1</version>
<type>pom</type>
<exclusions>
<exclusion>
<artifactId>log4j</artifactId>
<groupId>log4j</groupId>
</exclusion>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<! --dubbo end-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.57</version>
</dependency>
</dependencies>
Copy the code
- The yamL configuration file defines:
spring:
application:
name: dubbo-provider
# Custom configuration
embedded:
zookeeper:
# ZooKeeper service connection port
port: 2181
# dubbo configuration
dubbo:
Registry configuration
registry:
id: dubbo-provider
address: Zookeeper: / / 127.0.0.1: ${embedded. Zookeeper. Port}
group: local
application:
name: dubbo-provider
id: dubbo-provider
logger: slf4j
qosEnable: true
qosPort: 22224
qosAcceptForeignIp: false
# Dubbo protocol configuration
protocol:
# -1 indicates that a random unused port is used
port: - 1
name: dubbo
scan:
Dubbo service provider implementation class package
base-packages: com.zero.provider.impl
Copy the code
-
Implement the interface defined by the API
Note that @service is Dubbo, do not import Spring.
import com.google.common.collect.Lists;
import com.zero.api.model.User;
import com.zero.api.provider.UserProvider;
import org.apache.dubbo.config.annotation.Service;
import java.util.List;
@Service(interfaceClass = UserProvider.class)
public class UserProviderImpl implements UserProvider {
@Override
public List<User> listUser(a) {
User user = new User();
user.setId(1L);
user.setUsername("Dragon");
returnLists.newArrayList(user); }}Copy the code
Consumer
- Pom definition:
We rely on spring-boot-starter-Web to provide the HTTP REST interface to the front-end calls. At the same time, the RPC call service provider is implemented internally through Dubbo.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>zero.springboot.study</groupId>
<artifactId>dubbo-api</artifactId>
<version>1.0.0 - the SNAPSHOT</version>
</dependency>
<! --dubbo start-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.1</version>
</dependency>
<! -- Zookeeper dependencies -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>2.7.1</version>
<type>pom</type>
<exclusions>
<exclusion>
<artifactId>log4j</artifactId>
<groupId>log4j</groupId>
</exclusion>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<! --dubbo end-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
Copy the code
- Yaml definition:
server:
# WebDay menopause port
port: 9005
spring:
application:
name: dubbo-comsumer
# Custom configuration
embedded:
zookeeper:
port: 2181
# dubbo configuration
dubbo:
registry:
id: dubbo-comsumer
address: Zookeeper: / / 127.0.0.1: ${embedded. Zookeeper. Port}
group: local
application:
name: dubbo-comsumer
id: dubbo-comsumer
logger: slf4j
qosEnable: false
qosPort: 22223
qosAcceptForeignIp: false
protocol:
port: - 1
name: dubbo
Check whether the service provider is valid
consumer:
check: false
Copy the code
- The service consumer invokes the service producer
import com.zero.api.model.User;
import com.zero.api.provider.UserProvider;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
@Reference
private UserProvider userProvider;
public List<User> listUser(a) {
returnuserProvider.listUser(); }}Copy the code
- We provide this to the front end call through a RESTfull interface.
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService userService;
@GetMapping
public Object listUser(a) {
List<User> list = userService.listUser();
returnlist; }}Copy the code
conclusion
For details on protocols, registries, multiple registries, and timeout configurations, you can view the official documents dubbo.apache.org/zh-cn/docs/…
Welcome to discuss the correction, email: [email protected]. Liking and following wechat official accounts are our driving force.
Scan _ Search joint propagation style – wechat standard green version. PNG