spring boot configuration dubbo(properties)

spring boot and dubbo configuration (properties)

dubbo and zookeeper work together, but the configuration between them is not mentioned here.

1. There are two ways to configure spring boot and dubbo:

1) spring boot configures dubbo in its own profile application.properties. (This article focuses on this)
2) spring boot combines traditional spring dubbo configuration file xml form. (See next article)

2. This article focuses on the steps to configure dubbo in your own profile:

1)dubbo interface publisher configuration

1. First, you need to introduce the following dependencies:

 <!--  Spring Boot Dubbo rely on-->
<dependency>
  <groupId>io.dubbo.springboot</groupId>
  <artifactId>spring-boot-starter-dubbo</artifactId>
  <version>1.0.0</version>
</dependency>

2. Configure the following in the application.properties file:

#Alias of the dubbo provider, just an identity
spring.dubbo.application.name=provider(Typically write your own project name)
#zk address
spring.dubbo.registry.address=zookeeper://192.168.1.160:2181
#dubbo protocol
spring.dubbo.protocol.name=dubbo
#duboo port number
spring.dubbo.protocol.port=20880
#This is the package location of the interface you want to publish to dubbo
spring.dubbo.scan=test.spring.dubboService

3. Create a dubbo interface and its implementation class to be published as follows:

package test.spring.dubboService;
public interface TestDubboService {
    public String getName(String name);         
}
package test.spring.dubboService.impl;

import com.alibaba.dubbo.config.annotation.Service;
import test.spring.dubboService.TestDubboService;

@Service
public class TestDubboServiceImpl implements TestDubboService {
    @Override
    public String getName(String name) {
        // TODO Auto-generated method stub
        return "Full name:"+name;
    }
}

Nothing is added to the interface, but add a comment @Service to its implementation class. Note that the comment is com.alibaba.dubbo.config.annotation.Service If errors are introduced, publishing will not succeed.

4. If the published interface needs a version number, it can be written as @Service(version="1.0.0.1")

The specific package location interface is shown in the following figure:

As I said in previous articles, the spring boot Default scan starts with the boot class and then goes down to the lower packages. If the callee has not initialized when calling, it will be impossible to find it. You should understand why the interface in the above diagram is placed above the implementation class.

The publisher that configures dubbo is done, running the startup class directly to see the publication of the dubbo interface. The following:

2)dubbo interface consumer configuration

1. First, still introduce the dependency of spring boot:

 <!--  Spring Boot Dubbo rely on-->
<dependency>
  <groupId>io.dubbo.springboot</groupId>
  <artifactId>spring-boot-starter-dubbo</artifactId>
  <version>1.0.0</version>
</dependency>

2. Fill in the application.properties file with some dubbo configuration:

spring.dubbo.application.name=consumer-1(Typically write your own project name)
#zk address
spring.dubbo.registry.address=zookeeper://192.168.1.160:2181
#dubbo protocol
spring.dubbo.protocol.name=dubbo
#duboo port number
spring.dubbo.protocol.port=20880
#This is the package location of the dubbo interface to scan for
spring.dubbo.scan=test.spring.dubboService

3. Since the consumer and publisher are not in the same project, the published interface TestDubboService needs to be copied to the consumer in the test.spring.dubboService package above, and the package structure must be the same as that published.

?? Note the above, what I'm talking about here is replication, which isn't exactly what we do in project development, like this.
?? We have built three projects in our project (springboot-facade, springboot-rest, springboot-service). Springboot-facade contains the structure of the interface, that is, service and bean, springboot-rest project and springboot-service project refer to springboot-facade project respectively. In the future, even if someone calls our interface, simply introduce the springboot-facade package. Configure dubbo and you're ready.
Of course, even remote calls like web services require that the service structure be generated through wsdl on the consumer side.
If a single project needs to be copied, please see the following figure specifically, mainly to remind that the copied interface must be in the same path as the published one:

4. Finally, where I called it, I was the interface introduced in TestController, where I needed to add a comment, alibaba's @Reference. Similarly, if you want to add a version number, you can add version, which corresponds to the version number of the publishing interface. For example: @Reference(version="1.0.0.1"), as follows:

package test.spring.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.dubbo.config.annotation.Reference;
import test.spring.dubboService.TestDubboService;


@RestController
@RequestMapping("/springboot")
public class TestController {
    @Reference
    TestDubboService testDubboService;
    @RequestMapping(value="abc/akf",method=RequestMethod.GET)
    public String abc(String name){  
        return testDubboService.getName(name);
    }
}

5. Start the dubbo publisher project and the caller project in turn. Access to the above controller, if this runs without any surprise, a null pointer exception will be reported. The published interface cannot be reached by the caller, so modify it as follows.

The modified project structure is as follows:

That is, you need to place the controller one level below dubboService, where I am a null pointer exception that resolves by adjusting the package structure, even if you add the @Lazy Delay Load comment to the introduction.

6. Start the project again, access the controller, and you will have access to the successful:

Next article on spring boot and traditional dubbo xml configuration

Keywords: Java Dubbo Spring Boot Back-end

Added by CGameProgrammer on Wed, 24 Nov 2021 20:26:36 +0200