springboot combined with dubbo

springboot combined with dubbo

Dubbo combined with spring needs to be configured in the configuration file. When combined with springboot, you can use the application.yml configuration file or application.properties. I prefer to use springboot in combination.
Not to mention so much, what you get from the code is the deepest.
Create a maven project

Create a springboot provider module

step0: After creation, introduce jar package dependency in pom.xml

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!--web项目-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--dubbo ali apache-->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.7.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.curator/curator-recipes -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>2.7.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.3</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.itzz</groupId>
            <artifactId>dubbo02-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

Step1 Change the suffix of application.properties file to .yml suffix, the configuration in the configuration file

dubbo:
  application:
    name: dubbo02-provider
  registry:
    address: zookeeper://localhost:2181
  protocol:
    port: 20880
    name: dubbo
  scan:
    base-packages: com.itzz.provider.service.Impl
#自定义当前应用版本号
application:
  version: 1.0.0
#修改web项目启动默认的端口号
server:
  port: 8081

Create the relevant package structure, and then create the relevant functions in the Impl package of the service


import com.itzz.provider.service.UserService;
import org.apache.dubbo.config.annotation.Service;

@Service(version = "${application.version}")
public class UserServiceImpl implements UserService {
    
    
    @Override
    public String getName(int id) {
    
    
        System.out.println("------1------");
        if (id == 1){
    
    
            return "张三";
        }else if (id == 2){
    
    
            return "李四";
        }
        return "傻蛋";
    }
}

@Service(version = "${application.version}") refers to the version number in the configuration file. If the version is upgraded in the future, you can't change it one by one, so add relevant notes. My package structure looks like this:
Insert picture description here

Create an api module (module of maven structure)

Create the same package structure as the provider's module in the api module


public interface UserService {
    
    
    public String getName(int id);
}

Package structure
Insert picture description here

Create a consumer spring boot module

step0: jar package of pom.xml

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.7.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>2.7.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.itzz</groupId>
            <artifactId>dubbo02-api</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

Be sure to introduce the api dependency, otherwise an error will be reported, saying that the suffix of the configuration file automatically generated by springboot can not be found in the UserService interface
step1: or change it to the .yml suffix. It is the same as the configuration information used when combining spring.

dubbo:
  application:
    name: dubbo02-consumer
  registry:
    address: zookeeper://localhost:2181
  scan:
    base-packages: com.itzz.provider.service.Impl
  #自定义当前应用版本号
application:
  version: 1.0.0
  #修改web项目启动默认的端口号
server:
  port: 8082

step2: Create a web layer, and the web directly calls the methods defined in the interface.

import com.itzz.provider.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    
    
    @Reference(version = "${application.version}")
    private UserService userService;
    @RequestMapping("getName")
    public String getName(Integer id){
    
    
        return userService.getName(id);
    }
}

The package structure is as follows:
Insert picture description here
the project is finished here, first start the main class of the provider, and then start the main class of the consumer. The rest is to test in the management center.

Test in the control center

Enter http://localhost:7001/ in the web page to enter the management center (of course, the premise is that your management center is turned on). Adding a provider is the same as the above function, making a provider cluster.

Then see if both have been registered in the monitoring center

Access in the URL of the web page through the consumer's port, as shown below:Insert picture description here

Weights

Weight: I set a weight of 300 on the second provided implementation class, so the chance of access will be more than that of the first provider.
Insert picture description here
Disabling means prohibiting use, and this provider is no longer used.
Insert picture description here
Displayed in the service center

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

Load balancing strategy

Dubbo's load balancing strategy is random access. Because Dubbo's default load balancing strategy is random, in addition to random polling (two providers come next to each other), and minimal concurrency (like where there are fewer people in the queue).

Random strategy

Random only when the weights are the same
Insert picture description here
Insert picture description here
Insert picture description here

Polling strategy

Insert picture description here

The number of visits to the provider is the same.
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_39095899/article/details/107425777