I. Project structure

Project Structure: | - spring - the boot - dubbo - demo project (parent) | -- - spring - the boot - dubbo - base (foundation) | - spring - the boot - dubbo - consumer | - (consumer) Spring-boot-dubbo-provider (producer)Copy the code

SpringBoot version: 2.2.0

Dubbo version 2.7.0

Nacos version: 1.1.4

2. Start the Nacos registry

IO/zh-CN /docs/…

The default account password is nacos

3. Construction project

Maven dependencies for Consumer and Provider are as follows:

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <! -- Base dependency -->
        <dependency>
            <groupId>com.sans</groupId>
            <artifactId>spring-boot-dubbo-base</artifactId>
            <version>0.0.1 - the SNAPSHOT</version>
        </dependency>
        <! -- Dubbo dependency -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.7.0</version>
        </dependency>
        <! -- Nacos dependency -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-registry-nacos</artifactId>
            <version>2.7.1</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
            <version>1.0.0</version>
        </dependency>
</dependencies>
Copy the code

The Consumer configuration is as follows:

# config port
server:
  port: 8862
dubbo:
  Configure service information
  application:
    name: dubbo-consumer
    Port conflicts may occur on the same machine
    qos-enable: false
    qos-accept-foreign-ip: false
  Configure the registry
  registry:
    address: Nacos: / / 127.0.0.1:8848
  # set timeout
  consumer:
    timeout: 4000
spring:
  main:
    # Resolve Bean duplication
    allow-bean-definition-overriding: true
Copy the code

The Provider configuration is as follows:

# config port
server:
  port: 8861
dubbo:
  Configure service information
  application:
    name: dubbo-provider
    Port conflicts may occur on the same machine
    qos-enable: false
    qos-accept-foreign-ip: false
  Configure the registry
  registry:
    address: Nacos: / / 127.0.0.1:8848
  # Set up agreement - The Agreement is passively accepted by the consumer designated by the provider
  protocol:
    name: dubbo
    port: 20880
spring:
  main:
    # Resolve Bean duplication
    allow-bean-definition-overriding: true
Copy the code

4.Base project preparation

Write a DTO

/** * RPC interface DTO * Note that the serialization interface is implemented *@Author Sans
 * @CreateTime2019/11/6 23:04 * /
@Data
public class ProviderTestDTO implements Serializable {
    // ID
    private int id;
    / / name
    private String name;
    / / serial number
    private Integer number;
}
Copy the code

Write the Serivce

/** * RPC interface *@Author Sans
 * @CreateTime2019/11/6 23:03 * /
public interface IProviderService {
    List<ProviderTestDTO> queryList(a);
}
Copy the code

Write the return result class

/** * Returns the result class * here the builder pattern is used * Advantages: 1. Private constructor access range is small 2. Parameters can be flexibly set for easy management *@Author Sans
 * @CreateTime2019/11/7 18:59 * /
@Getter
public class ResultVO<T> implements Serializable {

    /** * return code */
    private int code;
    /** * Returns a message */
    private String message;
    /** * returns data */
    private T data;

    /** Privatize constructor **/
    private ResultVO(a) {}
    private ResultVO(ResultVO<T> resultVO) {
        this.code = resultVO.code;
        this.message = resultVO.message;
        this.data = resultVO.data;
    }

    /** * Build */
    public static class Builder<T>{
        private ResultVO<T> resultVO;
        public Builder(a) {
            resultVO = new ResultVO<>();
        }
        public Builder code(int code){
            resultVO.code = code;
            return this;
        }
        public Builder message(String message){
            resultVO.message = message;
            return this;
        }
        public Builder data(T data){
            resultVO.data = data;
            return this;
        }
        public ResultVO<T> build(a){
            return newResultVO<>(resultVO); }}}Copy the code

Five.Provider project compilation

Don’t forget to annotate the startup class with @enabledubbo

@EnableDubbo // Enable annotation support for Dubbo
@SpringBootApplication
public class ProviderApplication {
    public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); }}Copy the code

Implement the IProviderService interface. Note that Serivce references dubbo’s package

/** * Producer Dubbo interface implementation *@Author Sans
 * @CreateTime2019/11/6 23:01 * /
@Service
public class ProviderServiceImpl implements IProviderService {
    @Override
    public List<ProviderTestDTO> queryList(a) {
        // Initialize the data
        ProviderTestDTO testDTO1 = new ProviderTestDTO();
        testDTO1.setId(1);
        testDTO1.setName("Students");
        testDTO1.setNumber(100);
        ProviderTestDTO testDTO2 = new ProviderTestDTO();
        testDTO2.setId(2);
        testDTO2.setName("Teacher");
        testDTO2.setNumber(101);
        // Assemble data
        List<ProviderTestDTO> list = new ArrayList<>();
        list.add(testDTO1);
        list.add(testDTO2);
        returnlist; }}Copy the code

Vi.Consumer Project preparation

The @enabledubbo annotation is the same as the Provider project startup class

@EnableDubbo // Enable annotation support for Dubbo
@SpringBootApplication
public class ConsumerApplication {
    public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); }}Copy the code

Writing test interfaces

/** * Consumer test interface *@Author Sans
 * @CreateTime2019/11/6 23:09 * /
@RestController
@RequestMapping("/consumer")
public class ConsumerController {
    // Dubbo remote call annotation
    @Reference
    private IProviderService providerService;
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public ResultVO getList(a){
        // Remote call
        List<ProviderTestDTO> providerTestDTOList = providerService.queryList();
        return new ResultVO.Builder<>().code(200).message("success").data(providerTestDTOList).build(); }}Copy the code

7. Test

Start the Provider and Consumer projects, at which point Nacos will have corresponding services

Eight. Project source code

Yards cloud: gitee.com/liselotte/s…

GitHub:github.com/xuyulong201…

Thank you for reading this article. If you like it, please click “like” and give more stars. Please give your valuable opinions on the shortcomings of this article.