preface

Apache Dubbo (incubating) | ˈ d ʌ b goes ʊ | is a high-performance, lightweight open source Java RPC framework, it provides three main core competencies: an interface of remote method invocation (rmi), intelligent fault tolerance and load balancing, and automatic registration and discovery of services.

1, features,

High-performance RPC calls for interface proxy

Providing high performance proxy-based remote invocation capabilities, services are interface-grained, shielding developers from remote invocation low-level details.

Intelligent load Balancing

Multiple load balancing policies are built in to intelligently sense the health status of downstream nodes, significantly reducing call delay and improving system throughput.

Automatic service registration and discovery

Supports a variety of registry services, real-time awareness of service instances online and offline.

Highly scalable

Following the design principle of microkernel + plug-in, all core capabilities such as Protocol, Transport, Serialization are designed as extension points, and the built-in implementation and third-party implementation are treated equally.

Run-time traffic scheduling

Built-in routing policies, such as conditions and scripts, can be configured with different routing rules to easily implement grayscale advertising and give priority to the same machine room.

Visual service governance and o&M

Provides various service governance and O&M tools: Query service metadata, service health status, and call statistics, deliver routing policies, and adjust configuration parameters in real time.

2. Node role

role instructions
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 Service run container

Let’s take another look at the architecture diagram provided by the official website:

For more Dubbo architecture, usage, etc., please visit the website: http://dubbo.apache.org/zh-cn/index.html

Integration with Spring

Dubbo uses a full Spring configuration mode to transparently access applications without any API intrusion. You only need to load Dubbo’s configuration with Spring. Dubbo is loaded based on Spring’s Schema extension.

So, if we want to use Dubbo, just introduce Maven coordinates. Dubbo already includes Spring, Zookeeper, Netty, and more.

<dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.2</version> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId> zkClient </artifactId> <version>0.10</version> </dependency>Copy the code

First, we need to define the interface and implementation class, in this case we will also make an interface to operate on user information.

1, interfaces,

package com.viewscenes.netsupervisor.service;
import java.util.List;
import com.viewscenes.netsupervisor.entity.InfoUser;

public interface InfoUserService {
	
	void insertInfoUser(InfoUser user);
	InfoUser getUserById(String id);	
	void deleteUserById(String id);
	List<InfoUser> getAllUser();
}
Copy the code
package com.viewscenes.netsupervisor.service.impl;

public class InfoUserServiceImpl implements InfoUserService{

	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	private Map<String,InfoUser> userMap = new HashMap<String, InfoUser>();
	
	public void insertInfoUser(InfoUser user) {
		logger.info("Add user info... {}",JSONObject.toJSONString(user));
		userMap.put(user.getId(), user);
	}
	public List<InfoUser> getAllUser(){
		List<InfoUser> infoUserList = new ArrayList<InfoUser>();
		for(Map.Entry<String, InfoUser> entry:userMap.entrySet()) {
			infoUserList.add(entry.getValue());
		}
		System.out.println("Get all user data :"+infoUserList.size());
		returninfoUserList; }}Copy the code

2, the Provider

Let’s look at the producer-side configuration file first.

<? xml version="1.0" encoding="UTF-8"? > <beans xmlns="http://www.springframework.org/schema/beans"
		    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		    xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
		    xsi:schemaLocation="Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> <! Dubbo: Application name= <dubbo:application name="dubbo_producer1"/ > <! -- Use the ZooKeeper registry to expose the service address --> <dubbo:registry address=Zookeeper: / / 192.168.139.129:2181? client=zkclient"/ > <! -- Expose service on port 20880 with dubbo --> <dubbo:protocol name="dubbo" port="20880"/ > <! -- Spring Bean --> < Bean id="infoUserService" class="com.viewscenes.netsupervisor.service.impl.InfoUserServiceImpl"/ > <! <dubbo:service interface="com.viewscenes.netsupervisor.service.InfoUserService" ref="infoUserService" /> 
</beans>
Copy the code

3, Consumer

<? xml version="1.0" encoding="UTF-8"? > <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> <! Dubbo: Application name= <dubbo: Application name="dubbo_consumer"/ > <! -- Used to configure connection registry information --> <dubbo: Registry protocol="zookeeper" address="192.168.139.129:2181" client="zkclient"/ > <! <dubbo:reference id="infoUserService" check="false" interface="com.viewscenes.netsupervisor.service.InfoUserService"/>
</beans>
Copy the code

4, start,

Once configured, we can start the producer side and the consumer side separately in the Spring project. Of course, remember to start the ZooKeeper server first.

public class Provider1 {
	public static void main(String[] args) throws IOException {
		ClassPathXmlApplicationContext applicationContext = new
			ClassPathXmlApplicationContext(new String[]{"classpath:dubbo_provider1.xml"}); applicationContext.start(); System.in.read(); }}Copy the code

We then create a user on the consumer side and constantly ask the user to query the method.

public class Consumer1 {	
	@SuppressWarnings("resource")
	public static void main(String[] args) {
		
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext
                (new String[]{"classpath:dubbo_consumer1.xml"});
        context.start();
        
        InfoUserService userService = (InfoUserService) context.getBean("infoUserService");
        
        InfoUser infoUser = new InfoUser();
        infoUser.setId(UUID.randomUUID().toString());
        infoUser.setName("Jack");
        infoUser.setAddress("BeiJing");
        userService.insertInfoUser(infoUser);
        
        while(true) {
        	List<InfoUser> userList = userService.getAllUser();
        	System.out.println("Query user information return data :"+JSONObject.toJSONString(userList)); try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }}}}Copy the code

With the above code, the consumer side can continuously fetch data from the producer side. This is the most basic application of the Dubbo framework, ok? Simple. Come and have a try