current position:Home>Dubbo | Dubbo quick start notes - environment and configuration

Dubbo | Dubbo quick start notes - environment and configuration

2022-01-27 04:02:15 Polychlorinated cyclohexane


Preface

More basic dubbo Learning notes , Some references are as follows :

Silicon Valley Dubbo course (dubbo A classic )

Dubbo Official website

Dubbo 2.x file


1. Dubbo Relevant concepts

1.1 How automatic service discovery works

Dubbo  Automatic service discovery capability based on consumer side


2. Startup file

This refers to the use of zookeeper As a registry ; And start the relevant dubbo monitor ;

zookeeper and dubbo monitor .png

2.1 zookeeper-3.4.11\bin\zkServer.cmd

Can be in zookeeper-3.4.11\conf Configure under the path zookeeper The port number of the client and data data :

 To configure zookeeper The port number of the client is the same as data data

stay zookeeper-3.4.11\bin Run in directory cmd start-up zookeeper service :

zookeeper service

2.2 zookeeper-3.4.11\bin\zkCli.cmd

stay zookeeper-3.4.11\bin Run in directory cmd start-up zookeeper client :

zookeeper client

2.3 java -jar dubbo-admin-0.0.1-SNAPSHOT.jar

Can be in incubator-dubbo-ops-master\dubbo-admin\src\main\resources Modify... Under the path application.properties Modification in file DubboWeb Interface port :

 modify dubbo port

Open the command line in the project root path , perform mvn clean package Type the project into jar package , Re execution java -jar dubbo-admin-0.0.1-SNAPSHOT.jar The command runs Dubbo;

Dubbo Of jar package
Dubbo Of Web Interface

2.4 dubbo-monitor-simple-2.0.0\assembly.bin\start.bat

Can be in dubbo-monitor-simple-2.0.0\conf The next path dubbo.properties File configuration monitoring page port :

 Configure the monitoring page port

stay dubbo-monitor-simple-2.0.0\assembly.bin Double click the path to run start.bat file

dubbo Monitoring page for .png

dubbo Monitoring page for Web Interface


3. Modify project documents

You need to make some changes to the project file ; In order to open dubbo Related functions ;

3.1 stay pom.xml Import related dependencies into the file

<!--  introduce dubbo -->
<!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>dubbo</artifactId>
	<version>2.6.2</version>
</dependency>
<!--  The registry uses zookeeper, Introduce operation zookeeper The client side of  -->
<dependency>
	<groupId>org.apache.curator</groupId>
	<artifactId>curator-framework</artifactId>
	<version>2.12.0</version>
</dependency>

3.2 stay application.properties file / src/main/resources/provider.xml Configuration in file dubbo

3.2.1 Provider

<?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://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

	<!-- 1、 Specify the current service / Name of application ( The same service name is the same , Don't have the same name as other services ) -->
	<dubbo:application name="user-service-provider"></dubbo:application>
	
	<!-- 2、 Specify the location of the registry  -->
	<!-- <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry> -->
	<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry>
	
	<!-- 3、 Specify communication rules (name Specify the communication protocol ? Communication port ) -->
	<dubbo:protocol name="dubbo" port="20882"></dubbo:protocol>
	
	<!-- 4、 Exposure Services  ref: Point to the real implementation object of the service  -->
	<dubbo:service interface="com.atguigu.gmall.service.UserService" ref="userServiceImpl01" timeout="1000" version="1.0.0">
		<dubbo:method name="getUserAddressList" timeout="1000"></dubbo:method>
	</dubbo:service>
	
	<!-- Set the rules of service provider  -->
	<dubbo:provider timeout="1000"></dubbo:provider>
	
	
	<!--  Implementation of services  -->
	<bean id="userServiceImpl01" class="com.atguigu.gmall.service.impl.UserServiceImpl"></bean>
	
	
	<dubbo:service interface="com.atguigu.gmall.service.UserService" ref="userServiceImpl02" timeout="1000" version="2.0.0">
		<dubbo:method name="getUserAddressList" timeout="1000"></dubbo:method>
	</dubbo:service>
	<bean id="userServiceImpl02" class="com.atguigu.gmall.service.impl.UserServiceImpl2"></bean>
	
	<!--  Connect the monitoring center  -->
	<dubbo:monitor protocol="registry"></dubbo:monitor>
	
</beans>

3.2.2 consumer

<?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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <!-- Enable package scanning -->
	<context:component-scan base-package="com.atguigu.gmall.service.impl"></context:component-scan>


	<dubbo:application name="order-service-consumer"></dubbo:application>
	
	<dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry>
	
	<!--  Configure local stub -->
	
	<!-- Declare the interface of the remote service that needs to be called ; Generate remote service proxy  -->
	<!-- 1)、 Precision first  ( Method level first , The interface level is second , Global configuration again ) 2)、 Consumer set priority ( If the level is the same , Then the consumer has priority , The provider is next to ) -->
	<!-- timeout="0"  The default is 1000ms-->
	<!-- retries="": Retry count , Does not include the first call ,0 Means not to try again -->
	<!--  idempotent ( Set number of retries )【 Inquire about 、 Delete 、 modify 】、 Non idempotent ( Can't set retry count )【 newly added 】 -->
	<dubbo:reference interface="com.atguigu.gmall.service.UserService" id="userService" timeout="5000" retries="3" version="*">
		<!-- <dubbo:method name="getUserAddressList" timeout="1000"></dubbo:method> -->
	</dubbo:reference>
		
	<!--  Configure the current consumer's uniform rules : All services are not checked  -->
	<dubbo:consumer check="false" timeout="5000"></dubbo:consumer>

	<dubbo:monitor protocol="registry"></dubbo:monitor>
	<!-- <dubbo:monitor address="127.0.0.1:7070"></dubbo:monitor> -->
	
</beans>

It's going on SpringBoot Integrate Dubbo when , The above information can be configured into application.properties In the document ; For details, please see 《4.8 SpringBoot And Dubbo Three ways to integrate 》

3.3 Use the annotation to turn on the function

annotation Location function
@EnableDubbo Turn on dubbo Related functions Main startup class
@Service Exposure Services Service providers provider
@Reference The remote invocation Serving consumers consumer

4. The configuration file

Dubbo Of XML Schema Configuration reference manual

dubbo:consumer To configure

4.1 Priority of configuration file

Dubbo Priority of configuration file

  • JVM start-up -D Parameter priority ;
  • XML second ;
  • Properties Last , Equivalent to the default value ;

4.2 Coverage relationships with different granularity configurations

 Coverage relationships with different granularity configurations

  • The higher the priority is ;
  • Precision first ( Method level first , The interface level is second , Global configuration again );
  • Consumer set priority ( If the level is the same , Then the consumer has priority , The provider is next to );

4.3 Turn off the startup check check

The provider has three configurations for closing and starting the check registry :

  • Spring The configuration file
<!--  Turn off the startup check of a service  ( No provider reported a mistake ) -->
<dubbo:reference interface="com.foo.BarService" check="false" />
	
<!--  Turn off start-up checks for all services  ( No provider reported a mistake ) -->
<dubbo:consumer check="false" />

<!--  Turn off check when registry starts  ( Failed to sign up for subscription ) -->
<dubbo:registry check="false" />
  • dubbo.properties
dubbo.reference.com.foo.BarService.check=false
dubbo.reference.check=false
dubbo.consumer.check=false
dubbo.registry.check=false
  • adopt -D Parameters
java -Ddubbo.reference.com.foo.BarService.check=false
java -Ddubbo.reference.check=false
java -Ddubbo.consumer.check=false 
java -Ddubbo.registry.check=false

The consumer has a configuration to turn off and start checking the service provider :

<dubbo:reference check="false"/>

4.4 Overtime timeout

Provider :

<!--  Configure timeout  -->
<dubbo:service interface="com.atguigu.gmall.service.UserService" ref="userServiceImpl01" timeout="1000"">
	<dubbo:method name="getUserAddressList" timeout="1000"></dubbo:method>
</dubbo:service>

consumer :

<!--  Configure timeout  -->
<dubbo:reference interface="com.foo.BarService" id="BarService" timeout="5000">
</dubbo:reference>

<!--  You can also configure timeout for specific methods ( hypothesis BarService Are there in testMethod Method ) -->
<dubbo:reference interface="com.foo.BarService" id="BarService" timeout="5000">
	<dubbo:method name="testMethod" timeout="2000"></dubbo:method>
</dubbo:reference>

It can reflect :

  • Precision first ( Method level first , The interface level is second , Global configuration again );
  • Consumer set priority ( If the level is the same , Then the consumer has priority , The provider is next to );

4.5 Retry count retries

consumer :

<!-- retries="": Retry count , Does not include the first call ,0 Means not to try again -->
<!--  idempotent ( Set number of retries )【 Inquire about 、 Delete 、 modify 】、 Non idempotent ( Can't set retry count )【 newly added 】 -->
<dubbo:reference interface="com.foo.BarService" id="BarService" timeout="5000" retries="3" >
</dubbo:reference>

4.6 Many versions version

Provider :

<!-- 1.0.0 edition -->
<dubbo:service interface="com.atguigu.gmall.service.UserService" ref="userServiceImpl01" timeout="1000" version="1.0.0">
	<dubbo:method name="getUserAddressList" timeout="1000"></dubbo:method>
</dubbo:service>
<!--  Implementation of services  -->
<bean id="userServiceImpl01" class="com.atguigu.gmall.service.impl.UserServiceImpl"></bean>

<!-- 2.0.0 edition -->
<dubbo:service interface="com.atguigu.gmall.service.UserService" ref="userServiceImpl02" timeout="1000" version="2.0.0">
	<dubbo:method name="getUserAddressList" timeout="1000"></dubbo:method>
</dubbo:service>
<bean id="userServiceImpl02" class="com.atguigu.gmall.service.impl.UserServiceImpl2"></bean>

consumer :

<!-- version Specify which version to use ,* Indicates the random version -->
<dubbo:reference interface="com.atguigu.gmall.service.UserService" id="userService" timeout="5000" retries="3" version="*">
	<!-- <dubbo:method name="getUserAddressList" timeout="1000"></dubbo:method> -->
</dubbo:reference>

4.7 Local stub stub

 Local stub
Provider :

public class UserServiceStub implements UserService {
    
	private final UserService userService;
	
	/** *  What's coming in is userService Remote proxy object  * @param userService */
	public UserServiceStub(UserService userService) {
    
		super();
		this.userService = userService;
	}
	
	@Override
	public List<UserAddress> getUserAddressList(String userId) {
    
		System.out.println("UserServiceStub.....");
		if(!StringUtils.isEmpty(userId)) {
    
			return userService.getUserAddressList(userId);
		}
		return null;
	}
}

consumer :

<!--  Configure local stub -->
<dubbo:reference interface="com.atguigu.gmall.service.UserService" id="userService" timeout="5000" retries="3" version="*" stub="com.*.UserServiceStub">
</dubbo:reference>

4.8 SpringBoot And Dubbo Three ways to integrate

  1. Import dubbo-starter; stay application.properties Configuration properties ; Use @Service Exposure Services ; Use @Reference Reference service ;
  2. Retain dubbo xml The configuration file ; Import dubbo-starter; Use @ImportResource Import dubbo The configuration file of ;
    • On the main startup class @ImportResource(locations="classpath:provider.xml");
  3. Using annotations API The way ; Manually create each component into the container , Give Way dubbo To scan other components ; Use @Service Exposure Services ;
    • On the main boot class :@EnableDubbo(scanBasePackages="com.atguigu.gmall");
    • stay config The following configuration is performed under the package :
@Configuration
public class MyDubboConfig {
    
	
	@Bean
	public ApplicationConfig applicationConfig() {
    
		ApplicationConfig applicationConfig = new ApplicationConfig();
		applicationConfig.setName("boot-user-service-provider");
		return applicationConfig;
	}
	
	//<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry>
	@Bean
	public RegistryConfig registryConfig() {
    
		RegistryConfig registryConfig = new RegistryConfig();
		registryConfig.setProtocol("zookeeper");
		registryConfig.setAddress("127.0.0.1:2181");
		return registryConfig;
	}
	
	//<dubbo:protocol name="dubbo" port="20882"></dubbo:protocol>
	@Bean
	public ProtocolConfig protocolConfig() {
    
		ProtocolConfig protocolConfig = new ProtocolConfig();
		protocolConfig.setName("dubbo");
		protocolConfig.setPort(20882);
		return protocolConfig;
	}
	
	/** *<dubbo:service interface="com.atguigu.gmall.service.UserService" ref="userServiceImpl01" timeout="1000" version="1.0.0"> <dubbo:method name="getUserAddressList" timeout="1000"></dubbo:method> </dubbo:service> */
	@Bean
	public ServiceConfig<UserService> userServiceConfig(UserService userService){
    
		ServiceConfig<UserService> serviceConfig = new ServiceConfig<>();
		serviceConfig.setInterface(UserService.class);
		serviceConfig.setRef(userService);
		serviceConfig.setVersion("1.0.0");
		
		// Configure each method Information about 
		MethodConfig methodConfig = new MethodConfig();
		methodConfig.setName("getUserAddressList");
		methodConfig.setTimeout(1000);
		
		// take method The settings of are related to service Configuration in progress 
		List<MethodConfig> methods = new ArrayList<>();
		methods.add(methodConfig);
		serviceConfig.setMethods(methods);
		
		//ProviderConfig
		//MonitorConfig
		return serviceConfig;
	}

}


Last

New people make , If there is a mistake , Welcome to point out , Be deeply grateful !
Welcome to the official account , Will share something more everyday !
If you want to reprint , Please mark the source !

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

Random recommended