current position:Home>Detailed explanation of using Dubbo in spring boot

Detailed explanation of using Dubbo in spring boot

2022-01-27 00:54:05 Java Yumeng

Dubbo It's alibaba SOA The core framework of service-oriented governance , Every day for 2,000+ Service offerings 3,000,000,000+ Secondary traffic support , And is widely used in Alibaba Group's member sites .Dubbo Is a distributed services framework , Committed to providing high performance and transparency RPC Remote service invocation scenarios , as well as SOA Service governance solution .

Dubbo brief introduction

Dubbo What is it? ?

Dubbo Is a distributed services framework , Committed to providing high performance and transparency RPC Remote service invocation scenarios , as well as SOA Service governance solution . To put it simply ,dubbo It's a service framework , If there are no distributed requirements , You don't need to use it , Only when distributed , Only then dubbo Requirements for such a distributed service framework , And it's essentially a service invocation thing , It is simply a distributed framework for remote service invocation

Its core part includes :

1. Telematics : Provides for a variety of long connection based NIO Framework Abstract encapsulation , Including multiple thread models , serialize , as well as “ request - Respond to ” Mode of information exchange .

2. Cluster fault tolerance : Provides transparent remote procedure calls based on interface methods , Including multi protocol support , And soft load balancing , Failure tolerance , Address routing , Dynamic configuration and other cluster support .

3. Auto discovery : Based on registry directory service , Enable service consumers to dynamically search for service providers , Make the address transparent , Enable service providers to smoothly add or reduce machines .

Dubbo What can be done ?

1. Transparent remote method calls , Call a remote method just like calling a local method , Simple configuration , There is no API invasion .

2. Soft load balancing and fault tolerance mechanism , It can be replaced in the intranet F5 Wait for hardware load balancer , cost reduction , Reduce the single point .

3. Service automatic registration and discovery , It's no longer necessary to write down the service provider address , The registry queries the... Of the service provider based on the interface name IP Address , And can smoothly add or remove service providers .

Dubbo The architecture of

Spring Boot  Use in  Dubbo  Detailed explanation

Architecture diagram

Node role description

node Role description
Provider Service providers that expose services
Consumer The service consumer that invokes the remote service
Registry Service registry with discovery registry
Monitor The monitoring center that counts the invocation times and invocation times of the service
Container Service run container

Dubbo Provide three key functions , Including interface based remote calls , Fault tolerance and load balancing, and automatic service registration and discovery

Call relation description

1. The service container is responsible for starting , load , Run the service provider .

2. The service provider at startup , Register your services with the registry .

3. Service consumers at startup , Subscribe to the registry for the services you need .

4. The registry returns a list of service provider addresses to the consumer , If there are changes , The registry pushes the change data to the consumer based on the long connection .

5. Serving consumers , From the provider address list , Based on soft load balancing algorithm , Select a provider to invoke , If the call fails , Select another call .

6. Service consumers and providers , Accumulates the number of calls and the call time in memory , Send statistics to the monitoring center every minute on a regular basis .

java Developers can see it .

For working 1 Year to 2 Students in . Students in this part of the time , Have been to Java With a deeper understanding of .

For working 2 Year to 3 Some of my classmates felt that they were very strong at this time , So I couldn't help but start to relax .

Have a job 3 Year to 4 Students in this stage of the year , It's hard to improve , And this stage of learning is often more diversified .

Have a job 4 Year to 5 The students of the year have gone through the experience of the previous year , I believe you have your own opinions in the field you are studying , This is the time , Technically you should have run into a bottleneck .

You can add the learning group number :521479582 JAVA Advanced development Let's exchange and study together 、 Help you improve yourself , Picture bottleneck , Keep up with the times .

Dubbo characteristic

Dubbo Architecture has the following characteristics , Connectivity 、 Robustness, 、 Scalability 、 And upgrading to the future architecture

Connectivity

  • The registry is responsible for registering and finding service addresses , Equivalent to directory service , Service providers and consumers interact with the registry only at startup , The registry does not forward requests , Less pressure

  • The monitoring center is responsible for counting the number of service calls , Call time, etc , The statistics will be sent to the monitoring center server once a minute after the memory summary , And show it in a report

  • A service provider registers its services with the registry , And report the call time to the monitoring center , This time does not include network overhead

  • The service consumer obtains the service provider address list from the registration center , And call the provider directly according to the load algorithm , At the same time, report the call time to the monitoring center , This time includes network overhead

  • Registry Center , Service providers , There are long connections among service consumers , Except for the monitoring center

  • The registry senses the presence of service providers through long connections , Service provider down , The registration center will immediately push the event to inform the consumer

  • Registration center and monitoring center are all down , Does not affect already running providers and consumers , The consumer caches the provider list locally

  • Registration center and monitoring center are optional , Service consumers can connect directly with service providers

Healthy sex

  • Downtime of the monitoring center does not affect the use of , Just lost part of the sampling data

  • After the database goes down , Registry can still provide service list query through cache , But can't sign up for a new service

  • Registry peer cluster , After any one goes down , Will automatically switch to another

  • When the registration center is down , Service providers and service consumers can still communicate through the local cache

  • Service provider stateless , After any one goes down , Not affecting use

  • After all the service providers are down , Service consumer apps will not be available , And reconnects indefinitely waiting for the service provider to recover

Scalability

  • The registry is a peer cluster , The machine deployment instance can be added dynamically , All clients will automatically discover new registries

  • Service provider stateless , The machine deployment instance can be added dynamically , The registry will push new service provider information to consumers

Escalating

When the scale of service cluster is further expanded , drive IT The governance structure is further upgraded , Need to implement dynamic deployment , Carry out flow calculation , The existing distributed service architecture will not bring resistance . Here's a possible future architecture :

Spring Boot  Use in  Dubbo  Detailed explanation

A possible architecture for the future

Node role description

node Role description
Deployer Automatically deploy local agents for services
Repository The warehouse is used to store service application release packages
Scheduler Dispatching center automatically increases or decreases service providers based on access pressure
Admin Unified management console
Registry Service registry with discovery registry
Monitor The monitoring center that counts the invocation times and invocation times of the service

Quick start

Dubbo Adopt all Spring Configuration mode , Transparent access application , There is nothing for the app API invasion , Just use Spring load Dubbo Can be configured ,Dubbo be based on Spring Of Schema Extension to load .

Environmental installation

Choose one of them

CentOs7.3 build ZooKeeper-3.4.9 Standalone service

CentOs7.3 build ZooKeeper-3.4.9 Cluster The cluster service

Github Code

I've put the code in Github , Import ymq-dubbo-spring-boot  project

github github.com/souyunku/ym…

Maven rely on

Add... To the project  dubbo  rely on

<dependency>
 <groupId>com.alibaba</groupId>
 <artifactId>dubbo</artifactId>
 <version>2.5.6</version></dependency>

Define the service interface

project :ymq-dubbo-api

public interface DemoService { String sayHello(String name);
}

service provider

project :ymq-dubbo-provider, Implement the interface at the service provider

@Service("demoService")public class DemoServiceImpl implements DemoService { @Override
 public String sayHello(String name) {
 System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress()); return "Hello " + name + ", response form provider: " + RpcContext.getContext().getLocalAddress();
 }
}

load dubbo To configure

@[email protected]("classpath:dubbo.properties")@ImportResource({"classpath:dubbo/*.xml"})
public class PropertiesConfig {
}

Add exposed service configuration at the provider : <dubbo:service>

dubbo-provider.xml

<!--  Declare the service interface that needs to be exposed  --><dubbo:service interface="io.ymq.dubbo.api.DemoService" ref="demoService"/>

Service consumer

project :ymq-dubbo-consumer , Remote method of consumption

@Service("consumerDemoService")
public class ConsumerDemoService { @Autowired
 private DemoService demoService; public void sayHello(String name) {
 String hello = demoService.sayHello(name); //  Execute the consumption remote method 
 System.out.println(hello); //  Show call results 
 }
}

load dubbo To configure

@[email protected]("classpath:dubbo.properties")@ImportResource({"classpath:dubbo/*.xml"})
public class PropertiesConfig {
}

Add reference service configuration to the consumer : <dubbo:reference>

dubbo-consumer.xml

<!--  Add reference remote service configuration   Can be with local bean The use of demoService --><dubbo:reference id="demoService" check="false" interface="io.ymq.dubbo.api.DemoService"/>

Remote services Dubbo To configure

project :ymq-dubbo-provider ,ymq-dubbo-consumer  Same configuration

dubbo.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
 <!--  Provider application information , Used to calculate dependencies  -->
 <dubbo:application name="${spring.application.name}" />
 <!--  Use multicast Broadcast registry exposes service address  -->
 <dubbo:registry protocol="zookeeper" address="${zookeeper.connect}" file="${dubbo.cache}"/>
 <!--  use dubbo The agreement 20880 Port exposure service  -->
 <dubbo:protocol name="dubbo" port="${dubbo.protocol.port}" threadpool="${dubbo.protocol.threadpool}" threads="${dubbo.protocol.threads}"/>
 <!--  The default value of the provider , When ProtocolConfig and ServiceConfig When a property is not configured , Use this default value , Optional .-->
 <dubbo:provider connections="${dubbo.provider.connections}" timeout="${dubbo.provider.timeout}" retries="${dubbo.provider.retries}" version="${dubbo.provider.version}" />
 <!--  Default configuration of the consumer , When ReferenceConfig When a property is not configured , Use this default value , Optional .-->
 <dubbo:consumer version="${dubbo.provider.version}" />
 <!--  Configuration of Monitoring Center , It is used to configure the information related to connecting the monitoring center , Optional .-->
 <dubbo:monitor protocol="registry"/></beans>

dubbo.properties

#########################################################
# dubbo config
# Expose service ports 
dubbo.protocol.port=20880
# Provider timeout 
dubbo.provider.timeout=10000
# Provider version 
dubbo.provider.version=1.0
# Indicates that the service uses a unique five bar company 
dubbo.provider.connections=5
#  Fixed size thread pool , A thread is created at startup , Don't shut down , Has been held .( default )
dubbo.protocol.threadpool=fixed
#  Number of threads 
dubbo.protocol.threads=500
# Configure the number of retries , It's best to retry only for reading , Write operations may cause multiple writes   Default retries="0"
dubbo.provider.retries=0
# dubbo Cache file 
dubbo.cache=/data/dubbo/cache/ymq-dubbo-provider
########################################################## zookeeper configzookeeper.connect=127.0.0.1:2181

test Dubbo

  1. The interface needs to be packaged separately , Share... Between service provider and consumer

  2. Hide the implementation from the service consumer

  3. You can also use IoC Inject

start-up ZooKeeper

Start the service

/opt/zookeeper-3.4.9/bin/zkServer.sh start

Start provider service

package io.ymq.dubbo.provider.run;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;/**
*  describe : Start provider service 
*
* @author yanpenglei
* @create 2017-10-27 11:49
**/@[email protected](value = {"io.ymq.dubbo"})
public class Startup { public static void main(String[] args) {
 SpringApplication.run(Startup.class, args);
 }
}

Test consumer remote services

package io.ymq.dubbo.test;import io.ymq.dubbo.consumer.run.Startup;import io.ymq.dubbo.consumer.service.ConsumerDemoService;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;/**
*  describe :  Test consumer remote services 
*
* @author yanpenglei
* @create 2017-10-27 14:15
**/@RunWith(SpringRunner.class)
@SpringBootTest(classes = Startup.class)
public class ConsumerTest {
 @Autowired
 private ConsumerDemoService consumerDemoService;
 @Test
 public void sayHello(){ consumerDemoService.sayHello("Peng Lei");
 }
}

Respond to :

[15:54:00] Hello Peng Lei, request from consumer: /10.4.82.6:63993

I've put the code in Github , Import ymq-dubbo-spring-boot  project

github github.com/souyunku/ym…

copyright notice
author[Java Yumeng],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201270054038819.html

Random recommended