Spring boot integrates Dubbo practice, super practical!

Thank you for your reference- http://bjbsair.com/2020-04-01/tech-info/18390.html

1. Choose ZooKeeper as the registration center

Using docker to install ZooKeeper in linux Environment

//Pull zookeeper image  
docker pull zookeeper  
  
//Start zookeeper  
docker run -d -p 2181:2181 -v /mysoft/zookeeper/data/:/data/ --name=zookeeper --privileged zookeeper

Use the zookeeper dev zooinspector client connection tool to view

2. Create Maven project unified declaration interface, extract all interfaces to separate projects, and introduce them to other projects through maven

pom.xml

<?xml version="1.0" encoding="UTF-8"?>  
<project xmlns="http://maven.apache.org/POM/4.0.0"  
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
    <modelVersion>4.0.0</modelVersion>  
  
    <groupId>com.cott.gmail</groupId>  
    <artifactId>api-interface</artifactId>  
    <version>1.0-SNAPSHOT</version>  
  
</project>

UserAddress.java

package com.cott.gmail.bean;  
  
import java.io.Serializable;  
  
public class UserAddress implements Serializable {  
    private Integer id;  
    private String userAddress;  
    private String userId;  
  
    public Integer getId() {  
        return id;  
    }  
  
    public void setId(Integer id) {  
        this.id = id;  
    }  
  
    public String getUserAddress() {  
        return userAddress;  
    }  
  
    public void setUserAddress(String userAddress) {  
        this.userAddress = userAddress;  
    }  
  
    public String getUserId() {  
        return userId;  
    }  
  
    public void setUserId(String userId) {  
        this.userId = userId;  
    }  
}

OrderService.java

package com.cott.gmail.service;  
  
import com.cott.gmail.bean.UserAddress;  
  
import java.util.List;  
  
public interface OrderService {  
  
    List<UserAddress> initOrder(String id);  
}

UserService.java

package com.cott.gmail.service;  
  
import com.cott.gmail.bean.UserAddress;  
  
import java.util.List;  
  
public interface UserService {  
    List<UserAddress> getAddress(String userId);  
}

3. Create producer SpringBoot project

pom.xml introduces the above interface project and dubbo dependency

<?xml version="1.0" encoding="UTF-8"?>  
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">  
    <modelVersion>4.0.0</modelVersion>  
    <parent>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-parent</artifactId>  
        <version>2.2.4.RELEASE</version>  
        <relativePath/> <!-- lookup parent from repository -->  
    </parent>  
    <groupId>com.cott.gmail</groupId>  
    <artifactId>boot-user-service-provider</artifactId>  
    <version>0.0.1-SNAPSHOT</version>  
    <name>boot-user-service-provider</name>  
    <description>Demo project for Spring Boot</description>  
  
    <properties>  
        <java.version>1.8</java.version>  
    </properties>  
  
    <dependencies>  
        <dependency>  
            <groupId>com.cott.gmail</groupId>  
            <artifactId>api-interface</artifactId>  
            <version>1.0-SNAPSHOT</version>  
        </dependency>  
  
        <dependency>  
            <groupId>com.alibaba.boot</groupId>  
            <artifactId>dubbo-spring-boot-starter</artifactId>  
            <version>0.2.0</version>  
        </dependency>  
  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter</artifactId>  
        </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>  
    </dependencies>  
  
    <build>  
        <plugins>  
            <plugin>  
                <groupId>org.springframework.boot</groupId>  
                <artifactId>spring-boot-maven-plugin</artifactId>  
            </plugin>  
        </plugins>  
    </build>  
  
</project>

To implement the UserService interface, the producer of dubbo needs to use @ Service annotation. In order to distinguish it from the @ Service annotation of spring, the @ Component annotation is used here. More articles on springboot: springboot content aggregation

UserServiceImpl.java

package com.cott.gmail.bootuserserviceprovider.service.impl;  
  
import com.alibaba.dubbo.config.annotation.Service;  
import com.cott.gmail.bean.UserAddress;  
import com.cott.gmail.service.UserService;  
import org.springframework.stereotype.Component;  
  
import java.util.Arrays;  
import java.util.List;  
  
@Service  
@Component  
public class UserServiceImpl implements UserService {  
  
    @Override  
    public List<UserAddress> getAddress(String uesrId) {  
        UserAddress userAddress1 = new UserAddress();  
        userAddress1.setId(1);  
        userAddress1.setUserAddress("1");  
        userAddress1.setUserId("1");  
        UserAddress userAddress2 = new UserAddress();  
        userAddress2.setId(2);  
        userAddress2.setUserAddress("2");  
        userAddress2.setUserId("2");  
  
        return Arrays.asList(userAddress1, userAddress2);  
    }  
}

Enable Dubbo annotation with @ EnableDubbo on the main method

BootUserServiceProviderApplication.java

package com.cott.gmail.bootuserserviceprovider;  
  
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;  
import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.SpringBootApplication;  
  
@EnableDubbo  
@SpringBootApplication  
public class BootUserServiceProviderApplication {  
  
    public static void main(String[] args) {  
        SpringApplication.run(BootUserServiceProviderApplication.class, args);  
    }  
  
}

Profile specifies application name, registry, service protocol and port number

application.yml

dubbo:  
  application:  
    name: boot-user-service-provider  
  registry:  
    address: 192.168.200.128:2181  
    protocol: zookeeper  
  protocol:  
    name: dubbo  
    port: 20880

Finally, start the program and check that the service has been registered in Dubbo admin.

4. Create a consumer spring boot project

pom.xml introduces the above interface project and dubbo dependency

<?xml version="1.0" encoding="UTF-8"?>  
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">  
    <modelVersion>4.0.0</modelVersion>  
    <parent>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-parent</artifactId>  
        <version>2.2.4.RELEASE</version>  
        <relativePath/> <!-- lookup parent from repository -->  
    </parent>  
    <groupId>com.cott.gmail</groupId>  
    <artifactId>boot-order-service-consumer</artifactId>  
    <version>0.0.1-SNAPSHOT</version>  
    <name>boot-order-service-consumer</name>  
    <description>Demo project for Spring Boot</description>  
  
    <properties>  
        <java.version>1.8</java.version>  
    </properties>  
  
    <dependencies>  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter</artifactId>  
        </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>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-web</artifactId>  
        </dependency>  
        <dependency>  
            <groupId>com.cott.gmail</groupId>  
            <artifactId>api-interface</artifactId>  
            <version>1.0-SNAPSHOT</version>  
            <scope>compile</scope>  
        </dependency>  
        <dependency>  
            <groupId>com.alibaba.boot</groupId>  
            <artifactId>dubbo-spring-boot-starter</artifactId>  
            <version>0.2.0</version>  
        </dependency>  
    </dependencies>  
  
    <build>  
        <plugins>  
            <plugin>  
                <groupId>org.springframework.boot</groupId>  
                <artifactId>spring-boot-maven-plugin</artifactId>  
            </plugin>  
        </plugins>  
    </build>  
  
</project>

Implement the orderservice interface, where @ Service is spring, and use @ Reference annotation to declare that UserService is injected through remote call

OrderServiceImpl.java

package com.cott.gmail.bootorderserviceconsumer.service.impl;  
  
import com.alibaba.dubbo.config.annotation.Reference;  
import com.cott.gmail.bean.UserAddress;  
import com.cott.gmail.service.OrderService;  
import com.cott.gmail.service.UserService;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.stereotype.Service;  
  
import java.util.List;  
  
@Service  
public class OrderServiceImpl implements OrderService {  
  
    @Reference  
    UserService userService;  
  
    @Override  
    public List<UserAddress> initOrder(String id) {  
        System.out.println("id= " + id);  
        List<UserAddress> list = userService.getAddress("1");  
        for (UserAddress user : list  
        ) {  
            System.out.println(user.getUserAddress());  
        }  
        return list;  
    }  
}

OrderController.java

package com.cott.gmail.bootorderserviceconsumer.controller;  
  
import com.cott.gmail.bean.UserAddress;  
import com.cott.gmail.service.OrderService;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.stereotype.Controller;  
import org.springframework.web.bind.annotation.GetMapping;  
import org.springframework.web.bind.annotation.RequestParam;  
import org.springframework.web.bind.annotation.ResponseBody;  
  
import java.util.List;  
  
@Controller  
public class OrderController {  
  
    @Autowired  
    OrderService orderService;  
  
    @ResponseBody  
    @GetMapping("/initOrder")  
    public List<UserAddress> initOrder(@RequestParam(name = "id") String id) {  
        return orderService.initOrder(id);  
    }  
}

@EnableDubbo enable Dubbo annotation

BootOrderServiceConsumerApplication.java

package com.cott.gmail.bootorderserviceconsumer;  
  
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;  
import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.SpringBootApplication;  
  
@EnableDubbo  
@SpringBootApplication  
public class BootOrderServiceConsumerApplication {  
  
    public static void main(String[] args) {  
        SpringApplication.run(BootOrderServiceConsumerApplication.class, args);  
    }  
  
}

The service name, registry, and tomcat external port number are declared in the configuration file

application.yml

dubbo:  
  application:  
    name: order-service-provider  
  registry:  
    address: 192.168.200.128:2181  
    protocol: zookeeper  
  
server:  
  port: 8081

So far, a simple dubbo project has been developed. Next, start the consumer project, enter the url in the browser, and get the return result

Thank you for your reference- http://bjbsair.com/2020-04-01/tech-info/18390.html

1. Choose ZooKeeper as the registration center

Using docker to install ZooKeeper in linux Environment

//Pull zookeeper image  
docker pull zookeeper  
  
//Start zookeeper  
docker run -d -p 2181:2181 -v /mysoft/zookeeper/data/:/data/ --name=zookeeper --privileged zookeeper

Use the zookeeper dev zooinspector client connection tool to view

2. Create Maven project unified declaration interface, extract all interfaces to separate projects, and introduce them to other projects through maven

pom.xml

<?xml version="1.0" encoding="UTF-8"?>  
<project xmlns="http://maven.apache.org/POM/4.0.0"  
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
    <modelVersion>4.0.0</modelVersion>  
  
    <groupId>com.cott.gmail</groupId>  
    <artifactId>api-interface</artifactId>  
    <version>1.0-SNAPSHOT</version>  
  
</project>

UserAddress.java

package com.cott.gmail.bean;  
  
import java.io.Serializable;  
  
public class UserAddress implements Serializable {  
    private Integer id;  
    private String userAddress;  
    private String userId;  
  
    public Integer getId() {  
        return id;  
    }  
  
    public void setId(Integer id) {  
        this.id = id;  
    }  
  
    public String getUserAddress() {  
        return userAddress;  
    }  
  
    public void setUserAddress(String userAddress) {  
        this.userAddress = userAddress;  
    }  
  
    public String getUserId() {  
        return userId;  
    }  
  
    public void setUserId(String userId) {  
        this.userId = userId;  
    }  
}

OrderService.java

package com.cott.gmail.service;  
  
import com.cott.gmail.bean.UserAddress;  
  
import java.util.List;  
  
public interface OrderService {  
  
    List<UserAddress> initOrder(String id);  
}

UserService.java

package com.cott.gmail.service;  
  
import com.cott.gmail.bean.UserAddress;  
  
import java.util.List;  
  
public interface UserService {  
    List<UserAddress> getAddress(String userId);  
}

3. Create producer SpringBoot project

pom.xml introduces the above interface project and dubbo dependency

<?xml version="1.0" encoding="UTF-8"?>  
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">  
    <modelVersion>4.0.0</modelVersion>  
    <parent>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-parent</artifactId>  
        <version>2.2.4.RELEASE</version>  
        <relativePath/> <!-- lookup parent from repository -->  
    </parent>  
    <groupId>com.cott.gmail</groupId>  
    <artifactId>boot-user-service-provider</artifactId>  
    <version>0.0.1-SNAPSHOT</version>  
    <name>boot-user-service-provider</name>  
    <description>Demo project for Spring Boot</description>  
  
    <properties>  
        <java.version>1.8</java.version>  
    </properties>  
  
    <dependencies>  
        <dependency>  
            <groupId>com.cott.gmail</groupId>  
            <artifactId>api-interface</artifactId>  
            <version>1.0-SNAPSHOT</version>  
        </dependency>  
  
        <dependency>  
            <groupId>com.alibaba.boot</groupId>  
            <artifactId>dubbo-spring-boot-starter</artifactId>  
            <version>0.2.0</version>  
        </dependency>  
  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter</artifactId>  
        </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>  
    </dependencies>  
  
    <build>  
        <plugins>  
            <plugin>  
                <groupId>org.springframework.boot</groupId>  
                <artifactId>spring-boot-maven-plugin</artifactId>  
            </plugin>  
        </plugins>  
    </build>  
  
</project>

To implement the UserService interface, the producer of dubbo needs to use @ Service annotation. In order to distinguish it from the @ Service annotation of spring, the @ Component annotation is used here. More articles on springboot: springboot content aggregation

UserServiceImpl.java

package com.cott.gmail.bootuserserviceprovider.service.impl;  
  
import com.alibaba.dubbo.config.annotation.Service;  
import com.cott.gmail.bean.UserAddress;  
import com.cott.gmail.service.UserService;  
import org.springframework.stereotype.Component;  
  
import java.util.Arrays;  
import java.util.List;  
  
@Service  
@Component  
public class UserServiceImpl implements UserService {  
  
    @Override  
    public List<UserAddress> getAddress(String uesrId) {  
        UserAddress userAddress1 = new UserAddress();  
        userAddress1.setId(1);  
        userAddress1.setUserAddress("1");  
        userAddress1.setUserId("1");  
        UserAddress userAddress2 = new UserAddress();  
        userAddress2.setId(2);  
        userAddress2.setUserAddress("2");  
        userAddress2.setUserId("2");  
  
        return Arrays.asList(userAddress1, userAddress2);  
    }  
}

Enable Dubbo annotation with @ EnableDubbo on the main method

BootUserServiceProviderApplication.java

package com.cott.gmail.bootuserserviceprovider;  
  
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;  
import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.SpringBootApplication;  
  
@EnableDubbo  
@SpringBootApplication  
public class BootUserServiceProviderApplication {  
  
    public static void main(String[] args) {  
        SpringApplication.run(BootUserServiceProviderApplication.class, args);  
    }  
  
}

Profile specifies application name, registry, service protocol and port number

application.yml

dubbo:  
  application:  
    name: boot-user-service-provider  
  registry:  
    address: 192.168.200.128:2181  
    protocol: zookeeper  
  protocol:  
    name: dubbo  
    port: 20880

Finally, start the program and check that the service has been registered in Dubbo admin.

4. Create a consumer spring boot project

pom.xml introduces the above interface project and dubbo dependency

<?xml version="1.0" encoding="UTF-8"?>  
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">  
    <modelVersion>4.0.0</modelVersion>  
    <parent>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-parent</artifactId>  
        <version>2.2.4.RELEASE</version>  
        <relativePath/> <!-- lookup parent from repository -->  
    </parent>  
    <groupId>com.cott.gmail</groupId>  
    <artifactId>boot-order-service-consumer</artifactId>  
    <version>0.0.1-SNAPSHOT</version>  
    <name>boot-order-service-consumer</name>  
    <description>Demo project for Spring Boot</description>  
  
    <properties>  
        <java.version>1.8</java.version>  
    </properties>  
  
    <dependencies>  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter</artifactId>  
        </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>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-web</artifactId>  
        </dependency>  
        <dependency>  
            <groupId>com.cott.gmail</groupId>  
            <artifactId>api-interface</artifactId>  
            <version>1.0-SNAPSHOT</version>  
            <scope>compile</scope>  
        </dependency>  
        <dependency>  
            <groupId>com.alibaba.boot</groupId>  
            <artifactId>dubbo-spring-boot-starter</artifactId>  
            <version>0.2.0</version>  
        </dependency>  
    </dependencies>  
  
    <build>  
        <plugins>  
            <plugin>  
                <groupId>org.springframework.boot</groupId>  
                <artifactId>spring-boot-maven-plugin</artifactId>  
            </plugin>  
        </plugins>  
    </build>  
  
</project>

Implement the orderservice interface, where @ Service is spring, and use @ Reference annotation to declare that UserService is injected through remote call

OrderServiceImpl.java

package com.cott.gmail.bootorderserviceconsumer.service.impl;  
  
import com.alibaba.dubbo.config.annotation.Reference;  
import com.cott.gmail.bean.UserAddress;  
import com.cott.gmail.service.OrderService;  
import com.cott.gmail.service.UserService;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.stereotype.Service;  
  
import java.util.List;  
  
@Service  
public class OrderServiceImpl implements OrderService {  
  
    @Reference  
    UserService userService;  
  
    @Override  
    public List<UserAddress> initOrder(String id) {  
        System.out.println("id= " + id);  
        List<UserAddress> list = userService.getAddress("1");  
        for (UserAddress user : list  
        ) {  
            System.out.println(user.getUserAddress());  
        }  
        return list;  
    }  
}

OrderController.java

package com.cott.gmail.bootorderserviceconsumer.controller;  
  
import com.cott.gmail.bean.UserAddress;  
import com.cott.gmail.service.OrderService;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.stereotype.Controller;  
import org.springframework.web.bind.annotation.GetMapping;  
import org.springframework.web.bind.annotation.RequestParam;  
import org.springframework.web.bind.annotation.ResponseBody;  
  
import java.util.List;  
  
@Controller  
public class OrderController {  
  
    @Autowired  
    OrderService orderService;  
  
    @ResponseBody  
    @GetMapping("/initOrder")  
    public List<UserAddress> initOrder(@RequestParam(name = "id") String id) {  
        return orderService.initOrder(id);  
    }  
}

@EnableDubbo enable Dubbo annotation

BootOrderServiceConsumerApplication.java

package com.cott.gmail.bootorderserviceconsumer;  
  
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;  
import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.SpringBootApplication;  
  
@EnableDubbo  
@SpringBootApplication  
public class BootOrderServiceConsumerApplication {  
  
    public static void main(String[] args) {  
        SpringApplication.run(BootOrderServiceConsumerApplication.class, args);  
    }  
  
}

The service name, registry, and tomcat external port number are declared in the configuration file

application.yml

dubbo:  
  application:  
    name: order-service-provider  
  registry:  
    address: 192.168.200.128:2181  
    protocol: zookeeper  
  
server:  
  port: 8081

So far, a simple dubbo project has been developed. Next, start the consumer project, enter the url in the browser, and get the return result

Thank you for your reference- http://bjbsair.com/2020-04-01/tech-info/18390.html

1. Choose ZooKeeper as the registration center

Using docker to install ZooKeeper in linux Environment

//Pull zookeeper image  
docker pull zookeeper  
  
//Start zookeeper  
docker run -d -p 2181:2181 -v /mysoft/zookeeper/data/:/data/ --name=zookeeper --privileged zookeeper

Use the zookeeper dev zooinspector client connection tool to view

2. Create Maven project unified declaration interface, extract all interfaces to separate projects, and introduce them to other projects through maven

pom.xml

<?xml version="1.0" encoding="UTF-8"?>  
<project xmlns="http://maven.apache.org/POM/4.0.0"  
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
    <modelVersion>4.0.0</modelVersion>  
  
    <groupId>com.cott.gmail</groupId>  
    <artifactId>api-interface</artifactId>  
    <version>1.0-SNAPSHOT</version>  
  
</project>

UserAddress.java

package com.cott.gmail.bean;  
  
import java.io.Serializable;  
  
public class UserAddress implements Serializable {  
    private Integer id;  
    private String userAddress;  
    private String userId;  
  
    public Integer getId() {  
        return id;  
    }  
  
    public void setId(Integer id) {  
        this.id = id;  
    }  
  
    public String getUserAddress() {  
        return userAddress;  
    }  
  
    public void setUserAddress(String userAddress) {  
        this.userAddress = userAddress;  
    }  
  
    public String getUserId() {  
        return userId;  
    }  
  
    public void setUserId(String userId) {  
        this.userId = userId;  
    }  
}

OrderService.java

package com.cott.gmail.service;  
  
import com.cott.gmail.bean.UserAddress;  
  
import java.util.List;  
  
public interface OrderService {  
  
    List<UserAddress> initOrder(String id);  
}

UserService.java

package com.cott.gmail.service;  
  
import com.cott.gmail.bean.UserAddress;  
  
import java.util.List;  
  
public interface UserService {  
    List<UserAddress> getAddress(String userId);  
}

3. Create producer SpringBoot project

pom.xml introduces the above interface project and dubbo dependency

<?xml version="1.0" encoding="UTF-8"?>  
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">  
    <modelVersion>4.0.0</modelVersion>  
    <parent>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-parent</artifactId>  
        <version>2.2.4.RELEASE</version>  
        <relativePath/> <!-- lookup parent from repository -->  
    </parent>  
    <groupId>com.cott.gmail</groupId>  
    <artifactId>boot-user-service-provider</artifactId>  
    <version>0.0.1-SNAPSHOT</version>  
    <name>boot-user-service-provider</name>  
    <description>Demo project for Spring Boot</description>  
  
    <properties>  
        <java.version>1.8</java.version>  
    </properties>  
  
    <dependencies>  
        <dependency>  
            <groupId>com.cott.gmail</groupId>  
            <artifactId>api-interface</artifactId>  
            <version>1.0-SNAPSHOT</version>  
        </dependency>  
  
        <dependency>  
            <groupId>com.alibaba.boot</groupId>  
            <artifactId>dubbo-spring-boot-starter</artifactId>  
            <version>0.2.0</version>  
        </dependency>  
  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter</artifactId>  
        </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>  
    </dependencies>  
  
    <build>  
        <plugins>  
            <plugin>  
                <groupId>org.springframework.boot</groupId>  
                <artifactId>spring-boot-maven-plugin</artifactId>  
            </plugin>  
        </plugins>  
    </build>  
  
</project>

To implement the UserService interface, the producer of dubbo needs to use @ Service annotation. In order to distinguish it from the @ Service annotation of spring, the @ Component annotation is used here. More articles on springboot: springboot content aggregation

UserServiceImpl.java

package com.cott.gmail.bootuserserviceprovider.service.impl;  
  
import com.alibaba.dubbo.config.annotation.Service;  
import com.cott.gmail.bean.UserAddress;  
import com.cott.gmail.service.UserService;  
import org.springframework.stereotype.Component;  
  
import java.util.Arrays;  
import java.util.List;  
  
@Service  
@Component  
public class UserServiceImpl implements UserService {  
  
    @Override  
    public List<UserAddress> getAddress(String uesrId) {  
        UserAddress userAddress1 = new UserAddress();  
        userAddress1.setId(1);  
        userAddress1.setUserAddress("1");  
        userAddress1.setUserId("1");  
        UserAddress userAddress2 = new UserAddress();  
        userAddress2.setId(2);  
        userAddress2.setUserAddress("2");  
        userAddress2.setUserId("2");  
  
        return Arrays.asList(userAddress1, userAddress2);  
    }  
}

Enable Dubbo annotation with @ EnableDubbo on the main method

BootUserServiceProviderApplication.java

package com.cott.gmail.bootuserserviceprovider;  
  
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;  
import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.SpringBootApplication;  
  
@EnableDubbo  
@SpringBootApplication  
public class BootUserServiceProviderApplication {  
  
    public static void main(String[] args) {  
        SpringApplication.run(BootUserServiceProviderApplication.class, args);  
    }  
  
}

Profile specifies application name, registry, service protocol and port number

application.yml

dubbo:  
  application:  
    name: boot-user-service-provider  
  registry:  
    address: 192.168.200.128:2181  
    protocol: zookeeper  
  protocol:  
    name: dubbo  
    port: 20880

Finally, start the program and check that the service has been registered in Dubbo admin.

4. Create a consumer spring boot project

pom.xml introduces the above interface project and dubbo dependency

<?xml version="1.0" encoding="UTF-8"?>  
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">  
    <modelVersion>4.0.0</modelVersion>  
    <parent>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-parent</artifactId>  
        <version>2.2.4.RELEASE</version>  
        <relativePath/> <!-- lookup parent from repository -->  
    </parent>  
    <groupId>com.cott.gmail</groupId>  
    <artifactId>boot-order-service-consumer</artifactId>  
    <version>0.0.1-SNAPSHOT</version>  
    <name>boot-order-service-consumer</name>  
    <description>Demo project for Spring Boot</description>  
  
    <properties>  
        <java.version>1.8</java.version>  
    </properties>  
  
    <dependencies>  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter</artifactId>  
        </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>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-web</artifactId>  
        </dependency>  
        <dependency>  
            <groupId>com.cott.gmail</groupId>  
            <artifactId>api-interface</artifactId>  
            <version>1.0-SNAPSHOT</version>  
            <scope>compile</scope>  
        </dependency>  
        <dependency>  
            <groupId>com.alibaba.boot</groupId>  
            <artifactId>dubbo-spring-boot-starter</artifactId>  
            <version>0.2.0</version>  
        </dependency>  
    </dependencies>  
  
    <build>  
        <plugins>  
            <plugin>  
                <groupId>org.springframework.boot</groupId>  
                <artifactId>spring-boot-maven-plugin</artifactId>  
            </plugin>  
        </plugins>  
    </build>  
  
</project>

Implement the orderservice interface, where @ Service is spring, and use @ Reference annotation to declare that UserService is injected through remote call

OrderServiceImpl.java

package com.cott.gmail.bootorderserviceconsumer.service.impl;  
  
import com.alibaba.dubbo.config.annotation.Reference;  
import com.cott.gmail.bean.UserAddress;  
import com.cott.gmail.service.OrderService;  
import com.cott.gmail.service.UserService;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.stereotype.Service;  
  
import java.util.List;  
  
@Service  
public class OrderServiceImpl implements OrderService {  
  
    @Reference  
    UserService userService;  
  
    @Override  
    public List<UserAddress> initOrder(String id) {  
        System.out.println("id= " + id);  
        List<UserAddress> list = userService.getAddress("1");  
        for (UserAddress user : list  
        ) {  
            System.out.println(user.getUserAddress());  
        }  
        return list;  
    }  
}

OrderController.java

package com.cott.gmail.bootorderserviceconsumer.controller;  
  
import com.cott.gmail.bean.UserAddress;  
import com.cott.gmail.service.OrderService;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.stereotype.Controller;  
import org.springframework.web.bind.annotation.GetMapping;  
import org.springframework.web.bind.annotation.RequestParam;  
import org.springframework.web.bind.annotation.ResponseBody;  
  
import java.util.List;  
  
@Controller  
public class OrderController {  
  
    @Autowired  
    OrderService orderService;  
  
    @ResponseBody  
    @GetMapping("/initOrder")  
    public List<UserAddress> initOrder(@RequestParam(name = "id") String id) {  
        return orderService.initOrder(id);  
    }  
}

@EnableDubbo enable Dubbo annotation

BootOrderServiceConsumerApplication.java

package com.cott.gmail.bootorderserviceconsumer;  
  
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;  
import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.SpringBootApplication;  
  
@EnableDubbo  
@SpringBootApplication  
public class BootOrderServiceConsumerApplication {  
  
    public static void main(String[] args) {  
        SpringApplication.run(BootOrderServiceConsumerApplication.class, args);  
    }  
  
}

The service name, registry, and tomcat external port number are declared in the configuration file

application.yml

dubbo:  
  application:  
    name: order-service-provider  
  registry:  
    address: 192.168.200.128:2181  
    protocol: zookeeper  
  
server:  
  port: 8081

So far, a simple dubbo project has been developed. Next, start the consumer project, enter the url in the browser, and get the return result

Thank you for your reference- http://bjbsair.com/2020-04-01/tech-info/18390.html

1. Choose ZooKeeper as the registration center

Using docker to install ZooKeeper in linux Environment

//Pull zookeeper image  
docker pull zookeeper  
  
//Start zookeeper  
docker run -d -p 2181:2181 -v /mysoft/zookeeper/data/:/data/ --name=zookeeper --privileged zookeeper

Use the zookeeper dev zooinspector client connection tool to view

2. Create Maven project unified declaration interface, extract all interfaces to separate projects, and introduce them to other projects through maven

pom.xml

<?xml version="1.0" encoding="UTF-8"?>  
<project xmlns="http://maven.apache.org/POM/4.0.0"  
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
    <modelVersion>4.0.0</modelVersion>  
  
    <groupId>com.cott.gmail</groupId>  
    <artifactId>api-interface</artifactId>  
    <version>1.0-SNAPSHOT</version>  
  
</project>

UserAddress.java

package com.cott.gmail.bean;  
  
import java.io.Serializable;  
  
public class UserAddress implements Serializable {  
    private Integer id;  
    private String userAddress;  
    private String userId;  
  
    public Integer getId() {  
        return id;  
    }  
  
    public void setId(Integer id) {  
        this.id = id;  
    }  
  
    public String getUserAddress() {  
        return userAddress;  
    }  
  
    public void setUserAddress(String userAddress) {  
        this.userAddress = userAddress;  
    }  
  
    public String getUserId() {  
        return userId;  
    }  
  
    public void setUserId(String userId) {  
        this.userId = userId;  
    }  
}

OrderService.java

package com.cott.gmail.service;  
  
import com.cott.gmail.bean.UserAddress;  
  
import java.util.List;  
  
public interface OrderService {  
  
    List<UserAddress> initOrder(String id);  
}

UserService.java

package com.cott.gmail.service;  
  
import com.cott.gmail.bean.UserAddress;  
  
import java.util.List;  
  
public interface UserService {  
    List<UserAddress> getAddress(String userId);  
}

3. Create producer SpringBoot project

pom.xml introduces the above interface project and dubbo dependency

<?xml version="1.0" encoding="UTF-8"?>  
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">  
    <modelVersion>4.0.0</modelVersion>  
    <parent>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-parent</artifactId>  
        <version>2.2.4.RELEASE</version>  
        <relativePath/> <!-- lookup parent from repository -->  
    </parent>  
    <groupId>com.cott.gmail</groupId>  
    <artifactId>boot-user-service-provider</artifactId>  
    <version>0.0.1-SNAPSHOT</version>  
    <name>boot-user-service-provider</name>  
    <description>Demo project for Spring Boot</description>  
  
    <properties>  
        <java.version>1.8</java.version>  
    </properties>  
  
    <dependencies>  
        <dependency>  
            <groupId>com.cott.gmail</groupId>  
            <artifactId>api-interface</artifactId>  
            <version>1.0-SNAPSHOT</version>  
        </dependency>  
  
        <dependency>  
            <groupId>com.alibaba.boot</groupId>  
            <artifactId>dubbo-spring-boot-starter</artifactId>  
            <version>0.2.0</version>  
        </dependency>  
  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter</artifactId>  
        </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>  
    </dependencies>  
  
    <build>  
        <plugins>  
            <plugin>  
                <groupId>org.springframework.boot</groupId>  
                <artifactId>spring-boot-maven-plugin</artifactId>  
            </plugin>  
        </plugins>  
    </build>  
  
</project>

To implement the UserService interface, the producer of dubbo needs to use @ Service annotation. In order to distinguish it from the @ Service annotation of spring, the @ Component annotation is used here. More articles on springboot: springboot content aggregation

UserServiceImpl.java

package com.cott.gmail.bootuserserviceprovider.service.impl;  
  
import com.alibaba.dubbo.config.annotation.Service;  
import com.cott.gmail.bean.UserAddress;  
import com.cott.gmail.service.UserService;  
import org.springframework.stereotype.Component;  
  
import java.util.Arrays;  
import java.util.List;  
  
@Service  
@Component  
public class UserServiceImpl implements UserService {  
  
    @Override  
    public List<UserAddress> getAddress(String uesrId) {  
        UserAddress userAddress1 = new UserAddress();  
        userAddress1.setId(1);  
        userAddress1.setUserAddress("1");  
        userAddress1.setUserId("1");  
        UserAddress userAddress2 = new UserAddress();  
        userAddress2.setId(2);  
        userAddress2.setUserAddress("2");  
        userAddress2.setUserId("2");  
  
        return Arrays.asList(userAddress1, userAddress2);  
    }  
}

Enable Dubbo annotation with @ EnableDubbo on the main method

BootUserServiceProviderApplication.java

package com.cott.gmail.bootuserserviceprovider;  
  
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;  
import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.SpringBootApplication;  
  
@EnableDubbo  
@SpringBootApplication  
public class BootUserServiceProviderApplication {  
  
    public static void main(String[] args) {  
        SpringApplication.run(BootUserServiceProviderApplication.class, args);  
    }  
  
}

Profile specifies application name, registry, service protocol and port number

application.yml

dubbo:  
  application:  
    name: boot-user-service-provider  
  registry:  
    address: 192.168.200.128:2181  
    protocol: zookeeper  
  protocol:  
    name: dubbo  
    port: 20880

Finally, start the program and check that the service has been registered in Dubbo admin.

4. Create a consumer spring boot project

pom.xml introduces the above interface project and dubbo dependency

<?xml version="1.0" encoding="UTF-8"?>  
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">  
    <modelVersion>4.0.0</modelVersion>  
    <parent>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-parent</artifactId>  
        <version>2.2.4.RELEASE</version>  
        <relativePath/> <!-- lookup parent from repository -->  
    </parent>  
    <groupId>com.cott.gmail</groupId>  
    <artifactId>boot-order-service-consumer</artifactId>  
    <version>0.0.1-SNAPSHOT</version>  
    <name>boot-order-service-consumer</name>  
    <description>Demo project for Spring Boot</description>  
  
    <properties>  
        <java.version>1.8</java.version>  
    </properties>  
  
    <dependencies>  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter</artifactId>  
        </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>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-web</artifactId>  
        </dependency>  
        <dependency>  
            <groupId>com.cott.gmail</groupId>  
            <artifactId>api-interface</artifactId>  
            <version>1.0-SNAPSHOT</version>  
            <scope>compile</scope>  
        </dependency>  
        <dependency>  
            <groupId>com.alibaba.boot</groupId>  
            <artifactId>dubbo-spring-boot-starter</artifactId>  
            <version>0.2.0</version>  
        </dependency>  
    </dependencies>  
  
    <build>  
        <plugins>  
            <plugin>  
                <groupId>org.springframework.boot</groupId>  
                <artifactId>spring-boot-maven-plugin</artifactId>  
            </plugin>  
        </plugins>  
    </build>  
  
</project>

Implement the orderservice interface, where @ Service is spring, and use @ Reference annotation to declare that UserService is injected through remote call

OrderServiceImpl.java

package com.cott.gmail.bootorderserviceconsumer.service.impl;  
  
import com.alibaba.dubbo.config.annotation.Reference;  
import com.cott.gmail.bean.UserAddress;  
import com.cott.gmail.service.OrderService;  
import com.cott.gmail.service.UserService;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.stereotype.Service;  
  
import java.util.List;  
  
@Service  
public class OrderServiceImpl implements OrderService {  
  
    @Reference  
    UserService userService;  
  
    @Override  
    public List<UserAddress> initOrder(String id) {  
        System.out.println("id= " + id);  
        List<UserAddress> list = userService.getAddress("1");  
        for (UserAddress user : list  
        ) {  
            System.out.println(user.getUserAddress());  
        }  
        return list;  
    }  
}

OrderController.java

package com.cott.gmail.bootorderserviceconsumer.controller;  
  
import com.cott.gmail.bean.UserAddress;  
import com.cott.gmail.service.OrderService;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.stereotype.Controller;  
import org.springframework.web.bind.annotation.GetMapping;  
import org.springframework.web.bind.annotation.RequestParam;  
import org.springframework.web.bind.annotation.ResponseBody;  
  
import java.util.List;  
  
@Controller  
public class OrderController {  
  
    @Autowired  
    OrderService orderService;  
  
    @ResponseBody  
    @GetMapping("/initOrder")  
    public List<UserAddress> initOrder(@RequestParam(name = "id") String id) {  
        return orderService.initOrder(id);  
    }  
}

@EnableDubbo enable Dubbo annotation

BootOrderServiceConsumerApplication.java

package com.cott.gmail.bootorderserviceconsumer;  
  
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;  
import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.SpringBootApplication;  
  
@EnableDubbo  
@SpringBootApplication  
public class BootOrderServiceConsumerApplication {  
  
    public static void main(String[] args) {  
        SpringApplication.run(BootOrderServiceConsumerApplication.class, args);  
    }  
  
}

The service name, registry, and tomcat external port number are declared in the configuration file

application.yml

dubbo:  
  application:  
    name: order-service-provider  
  registry:  
    address: 192.168.200.128:2181  
    protocol: zookeeper  
  
server:  
  port: 8081

So far, a simple dubbo project has been developed. Next, start the consumer project, enter the url in the browser, and get the return result

Thank you for your reference- http://bjbsair.com/2020-04-01/tech-info/18390.html

1. Choose ZooKeeper as the registration center

Using docker to install ZooKeeper in linux Environment

//Pull zookeeper image  
docker pull zookeeper  
  
//Start zookeeper  
docker run -d -p 2181:2181 -v /mysoft/zookeeper/data/:/data/ --name=zookeeper --privileged zookeeper

Use the zookeeper dev zooinspector client connection tool to view

2. Create Maven project unified declaration interface, extract all interfaces to separate projects, and introduce them to other projects through maven

pom.xml

<?xml version="1.0" encoding="UTF-8"?>  
<project xmlns="http://maven.apache.org/POM/4.0.0"  
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
    <modelVersion>4.0.0</modelVersion>  
  
    <groupId>com.cott.gmail</groupId>  
    <artifactId>api-interface</artifactId>  
    <version>1.0-SNAPSHOT</version>  
  
</project>

UserAddress.java

package com.cott.gmail.bean;  
  
import java.io.Serializable;  
  
public class UserAddress implements Serializable {  
    private Integer id;  
    private String userAddress;  
    private String userId;  
  
    public Integer getId() {  
        return id;  
    }  
  
    public void setId(Integer id) {  
        this.id = id;  
    }  
  
    public String getUserAddress() {  
        return userAddress;  
    }  
  
    public void setUserAddress(String userAddress) {  
        this.userAddress = userAddress;  
    }  
  
    public String getUserId() {  
        return userId;  
    }  
  
    public void setUserId(String userId) {  
        this.userId = userId;  
    }  
}

OrderService.java

package com.cott.gmail.service;  
  
import com.cott.gmail.bean.UserAddress;  
  
import java.util.List;  
  
public interface OrderService {  
  
    List<UserAddress> initOrder(String id);  
}

UserService.java

package com.cott.gmail.service;  
  
import com.cott.gmail.bean.UserAddress;  
  
import java.util.List;  
  
public interface UserService {  
    List<UserAddress> getAddress(String userId);  
}

3. Create producer SpringBoot project

pom.xml introduces the above interface project and dubbo dependency

<?xml version="1.0" encoding="UTF-8"?>  
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">  
    <modelVersion>4.0.0</modelVersion>  
    <parent>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-parent</artifactId>  
        <version>2.2.4.RELEASE</version>  
        <relativePath/> <!-- lookup parent from repository -->  
    </parent>  
    <groupId>com.cott.gmail</groupId>  
    <artifactId>boot-user-service-provider</artifactId>  
    <version>0.0.1-SNAPSHOT</version>  
    <name>boot-user-service-provider</name>  
    <description>Demo project for Spring Boot</description>  
  
    <properties>  
        <java.version>1.8</java.version>  
    </properties>  
  
    <dependencies>  
        <dependency>  
            <groupId>com.cott.gmail</groupId>  
            <artifactId>api-interface</artifactId>  
            <version>1.0-SNAPSHOT</version>  
        </dependency>  
  
        <dependency>  
            <groupId>com.alibaba.boot</groupId>  
            <artifactId>dubbo-spring-boot-starter</artifactId>  
            <version>0.2.0</version>  
        </dependency>  
  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter</artifactId>  
        </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>  
    </dependencies>  
  
    <build>  
        <plugins>  
            <plugin>  
                <groupId>org.springframework.boot</groupId>  
                <artifactId>spring-boot-maven-plugin</artifactId>  
            </plugin>  
        </plugins>  
    </build>  
  
</project>

To implement the UserService interface, the producer of dubbo needs to use @ Service annotation. In order to distinguish it from the @ Service annotation of spring, the @ Component annotation is used here. More articles on springboot: springboot content aggregation

UserServiceImpl.java

package com.cott.gmail.bootuserserviceprovider.service.impl;  
  
import com.alibaba.dubbo.config.annotation.Service;  
import com.cott.gmail.bean.UserAddress;  
import com.cott.gmail.service.UserService;  
import org.springframework.stereotype.Component;  
  
import java.util.Arrays;  
import java.util.List;  
  
@Service  
@Component  
public class UserServiceImpl implements UserService {  
  
    @Override  
    public List<UserAddress> getAddress(String uesrId) {  
        UserAddress userAddress1 = new UserAddress();  
        userAddress1.setId(1);  
        userAddress1.setUserAddress("1");  
        userAddress1.setUserId("1");  
        UserAddress userAddress2 = new UserAddress();  
        userAddress2.setId(2);  
        userAddress2.setUserAddress("2");  
        userAddress2.setUserId("2");  
  
        return Arrays.asList(userAddress1, userAddress2);  
    }  
}

Enable Dubbo annotation with @ EnableDubbo on the main method

BootUserServiceProviderApplication.java

package com.cott.gmail.bootuserserviceprovider;  
  
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;  
import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.SpringBootApplication;  
  
@EnableDubbo  
@SpringBootApplication  
public class BootUserServiceProviderApplication {  
  
    public static void main(String[] args) {  
        SpringApplication.run(BootUserServiceProviderApplication.class, args);  
    }  
  
}

Profile specifies application name, registry, service protocol and port number

application.yml

dubbo:  
  application:  
    name: boot-user-service-provider  
  registry:  
    address: 192.168.200.128:2181  
    protocol: zookeeper  
  protocol:  
    name: dubbo  
    port: 20880

Finally, start the program and check that the service has been registered in Dubbo admin.

4. Create a consumer spring boot project

pom.xml introduces the above interface project and dubbo dependency

<?xml version="1.0" encoding="UTF-8"?>  
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">  
    <modelVersion>4.0.0</modelVersion>  
    <parent>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-parent</artifactId>  
        <version>2.2.4.RELEASE</version>  
        <relativePath/> <!-- lookup parent from repository -->  
    </parent>  
    <groupId>com.cott.gmail</groupId>  
    <artifactId>boot-order-service-consumer</artifactId>  
    <version>0.0.1-SNAPSHOT</version>  
    <name>boot-order-service-consumer</name>  
    <description>Demo project for Spring Boot</description>  
  
    <properties>  
        <java.version>1.8</java.version>  
    </properties>  
  
    <dependencies>  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter</artifactId>  
        </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>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-web</artifactId>  
        </dependency>  
        <dependency>  
            <groupId>com.cott.gmail</groupId>  
            <artifactId>api-interface</artifactId>  
            <version>1.0-SNAPSHOT</version>  
            <scope>compile</scope>  
        </dependency>  
        <dependency>  
            <groupId>com.alibaba.boot</groupId>  
            <artifactId>dubbo-spring-boot-starter</artifactId>  
            <version>0.2.0</version>  
        </dependency>  
    </dependencies>  
  
    <build>  
        <plugins>  
            <plugin>  
                <groupId>org.springframework.boot</groupId>  
                <artifactId>spring-boot-maven-plugin</artifactId>  
            </plugin>  
        </plugins>  
    </build>  
  
</project>

Implement the orderservice interface, where @ Service is spring, and use @ Reference annotation to declare that UserService is injected through remote call

OrderServiceImpl.java

package com.cott.gmail.bootorderserviceconsumer.service.impl;  
  
import com.alibaba.dubbo.config.annotation.Reference;  
import com.cott.gmail.bean.UserAddress;  
import com.cott.gmail.service.OrderService;  
import com.cott.gmail.service.UserService;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.stereotype.Service;  
  
import java.util.List;  
  
@Service  
public class OrderServiceImpl implements OrderService {  
  
    @Reference  
    UserService userService;  
  
    @Override  
    public List<UserAddress> initOrder(String id) {  
        System.out.println("id= " + id);  
        List<UserAddress> list = userService.getAddress("1");  
        for (UserAddress user : list  
        ) {  
            System.out.println(user.getUserAddress());  
        }  
        return list;  
    }  
}

OrderController.java

package com.cott.gmail.bootorderserviceconsumer.controller;  
  
import com.cott.gmail.bean.UserAddress;  
import com.cott.gmail.service.OrderService;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.stereotype.Controller;  
import org.springframework.web.bind.annotation.GetMapping;  
import org.springframework.web.bind.annotation.RequestParam;  
import org.springframework.web.bind.annotation.ResponseBody;  
  
import java.util.List;  
  
@Controller  
public class OrderController {  
  
    @Autowired  
    OrderService orderService;  
  
    @ResponseBody  
    @GetMapping("/initOrder")  
    public List<UserAddress> initOrder(@RequestParam(name = "id") String id) {  
        return orderService.initOrder(id);  
    }  
}

@EnableDubbo enable Dubbo annotation

BootOrderServiceConsumerApplication.java

package com.cott.gmail.bootorderserviceconsumer;  
  
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;  
import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.SpringBootApplication;  
  
@EnableDubbo  
@SpringBootApplication  
public class BootOrderServiceConsumerApplication {  
  
    public static void main(String[] args) {  
        SpringApplication.run(BootOrderServiceConsumerApplication.class, args);  
    }  
  
}

The service name, registry, and tomcat external port number are declared in the configuration file

application.yml

dubbo:  
  application:  
    name: order-service-provider  
  registry:  
    address: 192.168.200.128:2181  
    protocol: zookeeper  
  
server:  
  port: 8081

So far, a simple dubbo project has been developed. Next, start the consumer project, enter the url in the browser, and get the return result

Thank you for your reference- http://bjbsair.com/2020-04-01/tech-info/18390.html

1. Choose ZooKeeper as the registration center

Using docker to install ZooKeeper in linux Environment

//Pull zookeeper image  
docker pull zookeeper  
  
//Start zookeeper  
docker run -d -p 2181:2181 -v /mysoft/zookeeper/data/:/data/ --name=zookeeper --privileged zookeeper

Use the zookeeper dev zooinspector client connection tool to view

2. Create Maven project unified declaration interface, extract all interfaces to separate projects, and introduce them to other projects through maven

pom.xml

<?xml version="1.0" encoding="UTF-8"?>  
<project xmlns="http://maven.apache.org/POM/4.0.0"  
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
    <modelVersion>4.0.0</modelVersion>  
  
    <groupId>com.cott.gmail</groupId>  
    <artifactId>api-interface</artifactId>  
    <version>1.0-SNAPSHOT</version>  
  
</project>

UserAddress.java

package com.cott.gmail.bean;  
  
import java.io.Serializable;  
  
public class UserAddress implements Serializable {  
    private Integer id;  
    private String userAddress;  
    private String userId;  
  
    public Integer getId() {  
        return id;  
    }  
  
    public void setId(Integer id) {  
        this.id = id;  
    }  
  
    public String getUserAddress() {  
        return userAddress;  
    }  
  
    public void setUserAddress(String userAddress) {  
        this.userAddress = userAddress;  
    }  
  
    public String getUserId() {  
        return userId;  
    }  
  
    public void setUserId(String userId) {  
        this.userId = userId;  
    }  
}

OrderService.java

package com.cott.gmail.service;  
  
import com.cott.gmail.bean.UserAddress;  
  
import java.util.List;  
  
public interface OrderService {  
  
    List<UserAddress> initOrder(String id);  
}

UserService.java

package com.cott.gmail.service;  
  
import com.cott.gmail.bean.UserAddress;  
  
import java.util.List;  
  
public interface UserService {  
    List<UserAddress> getAddress(String userId);  
}

3. Create producer SpringBoot project

pom.xml introduces the above interface project and dubbo dependency

<?xml version="1.0" encoding="UTF-8"?>  
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">  
    <modelVersion>4.0.0</modelVersion>  
    <parent>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-parent</artifactId>  
        <version>2.2.4.RELEASE</version>  
        <relativePath/> <!-- lookup parent from repository -->  
    </parent>  
    <groupId>com.cott.gmail</groupId>  
    <artifactId>boot-user-service-provider</artifactId>  
    <version>0.0.1-SNAPSHOT</version>  
    <name>boot-user-service-provider</name>  
    <description>Demo project for Spring Boot</description>  
  
    <properties>  
        <java.version>1.8</java.version>  
    </properties>  
  
    <dependencies>  
        <dependency>  
            <groupId>com.cott.gmail</groupId>  
            <artifactId>api-interface</artifactId>  
            <version>1.0-SNAPSHOT</version>  
        </dependency>  
  
        <dependency>  
            <groupId>com.alibaba.boot</groupId>  
            <artifactId>dubbo-spring-boot-starter</artifactId>  
            <version>0.2.0</version>  
        </dependency>  
  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter</artifactId>  
        </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>  
    </dependencies>  
  
    <build>  
        <plugins>  
            <plugin>  
                <groupId>org.springframework.boot</groupId>  
                <artifactId>spring-boot-maven-plugin</artifactId>  
            </plugin>  
        </plugins>  
    </build>  
  
</project>

To implement the UserService interface, the producer of dubbo needs to use @ Service annotation. In order to distinguish it from the @ Service annotation of spring, the @ Component annotation is used here. More articles on springboot: springboot content aggregation

UserServiceImpl.java

package com.cott.gmail.bootuserserviceprovider.service.impl;  
  
import com.alibaba.dubbo.config.annotation.Service;  
import com.cott.gmail.bean.UserAddress;  
import com.cott.gmail.service.UserService;  
import org.springframework.stereotype.Component;  
  
import java.util.Arrays;  
import java.util.List;  
  
@Service  
@Component  
public class UserServiceImpl implements UserService {  
  
    @Override  
    public List<UserAddress> getAddress(String uesrId) {  
        UserAddress userAddress1 = new UserAddress();  
        userAddress1.setId(1);  
        userAddress1.setUserAddress("1");  
        userAddress1.setUserId("1");  
        UserAddress userAddress2 = new UserAddress();  
        userAddress2.setId(2);  
        userAddress2.setUserAddress("2");  
        userAddress2.setUserId("2");  
  
        return Arrays.asList(userAddress1, userAddress2);  
    }  
}

Enable Dubbo annotation with @ EnableDubbo on the main method

BootUserServiceProviderApplication.java

package com.cott.gmail.bootuserserviceprovider;  
  
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;  
import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.SpringBootApplication;  
  
@EnableDubbo  
@SpringBootApplication  
public class BootUserServiceProviderApplication {  
  
    public static void main(String[] args) {  
        SpringApplication.run(BootUserServiceProviderApplication.class, args);  
    }  
  
}

Profile specifies application name, registry, service protocol and port number

application.yml

dubbo:  
  application:  
    name: boot-user-service-provider  
  registry:  
    address: 192.168.200.128:2181  
    protocol: zookeeper  
  protocol:  
    name: dubbo  
    port: 20880

Finally, start the program and check that the service has been registered in Dubbo admin.

4. Create a consumer spring boot project

pom.xml introduces the above interface project and dubbo dependency

<?xml version="1.0" encoding="UTF-8"?>  
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">  
    <modelVersion>4.0.0</modelVersion>  
    <parent>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-parent</artifactId>  
        <version>2.2.4.RELEASE</version>  
        <relativePath/> <!-- lookup parent from repository -->  
    </parent>  
    <groupId>com.cott.gmail</groupId>  
    <artifactId>boot-order-service-consumer</artifactId>  
    <version>0.0.1-SNAPSHOT</version>  
    <name>boot-order-service-consumer</name>  
    <description>Demo project for Spring Boot</description>  
  
    <properties>  
        <java.version>1.8</java.version>  
    </properties>  
  
    <dependencies>  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter</artifactId>  
        </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>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-web</artifactId>  
        </dependency>  
        <dependency>  
            <groupId>com.cott.gmail</groupId>  
            <artifactId>api-interface</artifactId>  
            <version>1.0-SNAPSHOT</version>  
            <scope>compile</scope>  
        </dependency>  
        <dependency>  
            <groupId>com.alibaba.boot</groupId>  
            <artifactId>dubbo-spring-boot-starter</artifactId>  
            <version>0.2.0</version>  
        </dependency>  
    </dependencies>  
  
    <build>  
        <plugins>  
            <plugin>  
                <groupId>org.springframework.boot</groupId>  
                <artifactId>spring-boot-maven-plugin</artifactId>  
            </plugin>  
        </plugins>  
    </build>  
  
</project>

Implement the orderservice interface, where @ Service is spring, and use @ Reference annotation to declare that UserService is injected through remote call

OrderServiceImpl.java

package com.cott.gmail.bootorderserviceconsumer.service.impl;  
  
import com.alibaba.dubbo.config.annotation.Reference;  
import com.cott.gmail.bean.UserAddress;  
import com.cott.gmail.service.OrderService;  
import com.cott.gmail.service.UserService;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.stereotype.Service;  
  
import java.util.List;  
  
@Service  
public class OrderServiceImpl implements OrderService {  
  
    @Reference  
    UserService userService;  
  
    @Override  
    public List<UserAddress> initOrder(String id) {  
        System.out.println("id= " + id);  
        List<UserAddress> list = userService.getAddress("1");  
        for (UserAddress user : list  
        ) {  
            System.out.println(user.getUserAddress());  
        }  
        return list;  
    }  
}

OrderController.java

package com.cott.gmail.bootorderserviceconsumer.controller;  
  
import com.cott.gmail.bean.UserAddress;  
import com.cott.gmail.service.OrderService;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.stereotype.Controller;  
import org.springframework.web.bind.annotation.GetMapping;  
import org.springframework.web.bind.annotation.RequestParam;  
import org.springframework.web.bind.annotation.ResponseBody;  
  
import java.util.List;  
  
@Controller  
public class OrderController {  
  
    @Autowired  
    OrderService orderService;  
  
    @ResponseBody  
    @GetMapping("/initOrder")  
    public List<UserAddress> initOrder(@RequestParam(name = "id") String id) {  
        return orderService.initOrder(id);  
    }  
}

@EnableDubbo enable Dubbo annotation

BootOrderServiceConsumerApplication.java

package com.cott.gmail.bootorderserviceconsumer;  
  
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;  
import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.SpringBootApplication;  
  
@EnableDubbo  
@SpringBootApplication  
public class BootOrderServiceConsumerApplication {  
  
    public static void main(String[] args) {  
        SpringApplication.run(BootOrderServiceConsumerApplication.class, args);  
    }  
  
}

The service name, registry, and tomcat external port number are declared in the configuration file

application.yml

dubbo:  
  application:  
    name: order-service-provider  
  registry:  
    address: 192.168.200.128:2181  
    protocol: zookeeper  
  
server:  
  port: 8081

So far, a simple dubbo project has been developed. Next, start the consumer project, enter the url in the browser, and get the return result

Thank you for your reference- http://bjbsair.com/2020-04-01/tech-info/18390.html

1. Choose ZooKeeper as the registration center

Using docker to install ZooKeeper in linux Environment

//Pull zookeeper image  
docker pull zookeeper  
  
//Start zookeeper  
docker run -d -p 2181:2181 -v /mysoft/zookeeper/data/:/data/ --name=zookeeper --privileged zookeeper

Use the zookeeper dev zooinspector client connection tool to view

2. Create Maven project unified declaration interface, extract all interfaces to separate projects, and introduce them to other projects through maven

pom.xml

<?xml version="1.0" encoding="UTF-8"?>  
<project xmlns="http://maven.apache.org/POM/4.0.0"  
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
    <modelVersion>4.0.0</modelVersion>  
  
    <groupId>com.cott.gmail</groupId>  
    <artifactId>api-interface</artifactId>  
    <version>1.0-SNAPSHOT</version>  
  
</project>

UserAddress.java

package com.cott.gmail.bean;  
  
import java.io.Serializable;  
  
public class UserAddress implements Serializable {  
    private Integer id;  
    private String userAddress;  
    private String userId;  
  
    public Integer getId() {  
        return id;  
    }  
  
    public void setId(Integer id) {  
        this.id = id;  
    }  
  
    public String getUserAddress() {  
        return userAddress;  
    }  
  
    public void setUserAddress(String userAddress) {  
        this.userAddress = userAddress;  
    }  
  
    public String getUserId() {  
        return userId;  
    }  
  
    public void setUserId(String userId) {  
        this.userId = userId;  
    }  
}

OrderService.java

package com.cott.gmail.service;  
  
import com.cott.gmail.bean.UserAddress;  
  
import java.util.List;  
  
public interface OrderService {  
  
    List<UserAddress> initOrder(String id);  
}

UserService.java

package com.cott.gmail.service;  
  
import com.cott.gmail.bean.UserAddress;  
  
import java.util.List;  
  
public interface UserService {  
    List<UserAddress> getAddress(String userId);  
}

3. Create producer SpringBoot project

pom.xml introduces the above interface project and dubbo dependency

<?xml version="1.0" encoding="UTF-8"?>  
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">  
    <modelVersion>4.0.0</modelVersion>  
    <parent>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-parent</artifactId>  
        <version>2.2.4.RELEASE</version>  
        <relativePath/> <!-- lookup parent from repository -->  
    </parent>  
    <groupId>com.cott.gmail</groupId>  
    <artifactId>boot-user-service-provider</artifactId>  
    <version>0.0.1-SNAPSHOT</version>  
    <name>boot-user-service-provider</name>  
    <description>Demo project for Spring Boot</description>  
  
    <properties>  
        <java.version>1.8</java.version>  
    </properties>  
  
    <dependencies>  
        <dependency>  
            <groupId>com.cott.gmail</groupId>  
            <artifactId>api-interface</artifactId>  
            <version>1.0-SNAPSHOT</version>  
        </dependency>  
  
        <dependency>  
            <groupId>com.alibaba.boot</groupId>  
            <artifactId>dubbo-spring-boot-starter</artifactId>  
            <version>0.2.0</version>  
        </dependency>  
  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter</artifactId>  
        </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>  
    </dependencies>  
  
    <build>  
        <plugins>  
            <plugin>  
                <groupId>org.springframework.boot</groupId>  
                <artifactId>spring-boot-maven-plugin</artifactId>  
            </plugin>  
        </plugins>  
    </build>  
  
</project>

To implement the UserService interface, the producer of dubbo needs to use @ Service annotation. In order to distinguish it from the @ Service annotation of spring, the @ Component annotation is used here. More articles on springboot: springboot content aggregation

UserServiceImpl.java

package com.cott.gmail.bootuserserviceprovider.service.impl;  
  
import com.alibaba.dubbo.config.annotation.Service;  
import com.cott.gmail.bean.UserAddress;  
import com.cott.gmail.service.UserService;  
import org.springframework.stereotype.Component;  
  
import java.util.Arrays;  
import java.util.List;  
  
@Service  
@Component  
public class UserServiceImpl implements UserService {  
  
    @Override  
    public List<UserAddress> getAddress(String uesrId) {  
        UserAddress userAddress1 = new UserAddress();  
        userAddress1.setId(1);  
        userAddress1.setUserAddress("1");  
        userAddress1.setUserId("1");  
        UserAddress userAddress2 = new UserAddress();  
        userAddress2.setId(2);  
        userAddress2.setUserAddress("2");  
        userAddress2.setUserId("2");  
  
        return Arrays.asList(userAddress1, userAddress2);  
    }  
}

Enable Dubbo annotation with @ EnableDubbo on the main method

BootUserServiceProviderApplication.java

package com.cott.gmail.bootuserserviceprovider;  
  
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;  
import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.SpringBootApplication;  
  
@EnableDubbo  
@SpringBootApplication  
public class BootUserServiceProviderApplication {  
  
    public static void main(String[] args) {  
        SpringApplication.run(BootUserServiceProviderApplication.class, args);  
    }  
  
}

Profile specifies application name, registry, service protocol and port number

application.yml

dubbo:  
  application:  
    name: boot-user-service-provider  
  registry:  
    address: 192.168.200.128:2181  
    protocol: zookeeper  
  protocol:  
    name: dubbo  
    port: 20880

Finally, start the program and check that the service has been registered in Dubbo admin.

4. Create a consumer spring boot project

pom.xml introduces the above interface project and dubbo dependency

<?xml version="1.0" encoding="UTF-8"?>  
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">  
    <modelVersion>4.0.0</modelVersion>  
    <parent>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-parent</artifactId>  
        <version>2.2.4.RELEASE</version>  
        <relativePath/> <!-- lookup parent from repository -->  
    </parent>  
    <groupId>com.cott.gmail</groupId>  
    <artifactId>boot-order-service-consumer</artifactId>  
    <version>0.0.1-SNAPSHOT</version>  
    <name>boot-order-service-consumer</name>  
    <description>Demo project for Spring Boot</description>  
  
    <properties>  
        <java.version>1.8</java.version>  
    </properties>  
  
    <dependencies>  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter</artifactId>  
        </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>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-web</artifactId>  
        </dependency>  
        <dependency>  
            <groupId>com.cott.gmail</groupId>  
            <artifactId>api-interface</artifactId>  
            <version>1.0-SNAPSHOT</version>  
            <scope>compile</scope>  
        </dependency>  
        <dependency>  
            <groupId>com.alibaba.boot</groupId>  
            <artifactId>dubbo-spring-boot-starter</artifactId>  
            <version>0.2.0</version>  
        </dependency>  
    </dependencies>  
  
    <build>  
        <plugins>  
            <plugin>  
                <groupId>org.springframework.boot</groupId>  
                <artifactId>spring-boot-maven-plugin</artifactId>  
            </plugin>  
        </plugins>  
    </build>  
  
</project>

Implement the orderservice interface, where @ Service is spring, and use @ Reference annotation to declare that UserService is injected through remote call

OrderServiceImpl.java

package com.cott.gmail.bootorderserviceconsumer.service.impl;  
  
import com.alibaba.dubbo.config.annotation.Reference;  
import com.cott.gmail.bean.UserAddress;  
import com.cott.gmail.service.OrderService;  
import com.cott.gmail.service.UserService;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.stereotype.Service;  
  
import java.util.List;  
  
@Service  
public class OrderServiceImpl implements OrderService {  
  
    @Reference  
    UserService userService;  
  
    @Override  
    public List<UserAddress> initOrder(String id) {  
        System.out.println("id= " + id);  
        List<UserAddress> list = userService.getAddress("1");  
        for (UserAddress user : list  
        ) {  
            System.out.println(user.getUserAddress());  
        }  
        return list;  
    }  
}

OrderController.java

package com.cott.gmail.bootorderserviceconsumer.controller;  
  
import com.cott.gmail.bean.UserAddress;  
import com.cott.gmail.service.OrderService;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.stereotype.Controller;  
import org.springframework.web.bind.annotation.GetMapping;  
import org.springframework.web.bind.annotation.RequestParam;  
import org.springframework.web.bind.annotation.ResponseBody;  
  
import java.util.List;  
  
@Controller  
public class OrderController {  
  
    @Autowired  
    OrderService orderService;  
  
    @ResponseBody  
    @GetMapping("/initOrder")  
    public List<UserAddress> initOrder(@RequestParam(name = "id") String id) {  
        return orderService.initOrder(id);  
    }  
}

@EnableDubbo enable Dubbo annotation

BootOrderServiceConsumerApplication.java

package com.cott.gmail.bootorderserviceconsumer;  
  
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;  
import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.SpringBootApplication;  
  
@EnableDubbo  
@SpringBootApplication  
public class BootOrderServiceConsumerApplication {  
  
    public static void main(String[] args) {  
        SpringApplication.run(BootOrderServiceConsumerApplication.class, args);  
    }  
  
}

The service name, registry, and tomcat external port number are declared in the configuration file

application.yml

dubbo:  
  application:  
    name: order-service-provider  
  registry:  
    address: 192.168.200.128:2181  
    protocol: zookeeper  
  
server:  
  port: 8081

So far, a simple dubbo project has been developed. Next, start the consumer project, enter the url in the browser, and get the return result

Thank you for your reference- http://bjbsair.com/2020-04-01/tech-info/18390.html

1. Choose ZooKeeper as the registration center

Using docker to install ZooKeeper in linux Environment

//Pull zookeeper image  
docker pull zookeeper  
  
//Start zookeeper  
docker run -d -p 2181:2181 -v /mysoft/zookeeper/data/:/data/ --name=zookeeper --privileged zookeeper

Use the zookeeper dev zooinspector client connection tool to view

2. Create Maven project unified declaration interface, extract all interfaces to separate projects, and introduce them to other projects through maven

pom.xml

<?xml version="1.0" encoding="UTF-8"?>  
<project xmlns="http://maven.apache.org/POM/4.0.0"  
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
    <modelVersion>4.0.0</modelVersion>  
  
    <groupId>com.cott.gmail</groupId>  
    <artifactId>api-interface</artifactId>  
    <version>1.0-SNAPSHOT</version>  
  
</project>

UserAddress.java

package com.cott.gmail.bean;  
  
import java.io.Serializable;  
  
public class UserAddress implements Serializable {  
    private Integer id;  
    private String userAddress;  
    private String userId;  
  
    public Integer getId() {  
        return id;  
    }  
  
    public void setId(Integer id) {  
        this.id = id;  
    }  
  
    public String getUserAddress() {  
        return userAddress;  
    }  
  
    public void setUserAddress(String userAddress) {  
        this.userAddress = userAddress;  
    }  
  
    public String getUserId() {  
        return userId;  
    }  
  
    public void setUserId(String userId) {  
        this.userId = userId;  
    }  
}

OrderService.java

package com.cott.gmail.service;  
  
import com.cott.gmail.bean.UserAddress;  
  
import java.util.List;  
  
public interface OrderService {  
  
    List<UserAddress> initOrder(String id);  
}

UserService.java

package com.cott.gmail.service;  
  
import com.cott.gmail.bean.UserAddress;  
  
import java.util.List;  
  
public interface UserService {  
    List<UserAddress> getAddress(String userId);  
}

3. Create producer SpringBoot project

pom.xml introduces the above interface project and dubbo dependency

<?xml version="1.0" encoding="UTF-8"?>  
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">  
    <modelVersion>4.0.0</modelVersion>  
    <parent>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-parent</artifactId>  
        <version>2.2.4.RELEASE</version>  
        <relativePath/> <!-- lookup parent from repository -->  
    </parent>  
    <groupId>com.cott.gmail</groupId>  
    <artifactId>boot-user-service-provider</artifactId>  
    <version>0.0.1-SNAPSHOT</version>  
    <name>boot-user-service-provider</name>  
    <description>Demo project for Spring Boot</description>  
  
    <properties>  
        <java.version>1.8</java.version>  
    </properties>  
  
    <dependencies>  
        <dependency>  
            <groupId>com.cott.gmail</groupId>  
            <artifactId>api-interface</artifactId>  
            <version>1.0-SNAPSHOT</version>  
        </dependency>  
  
        <dependency>  
            <groupId>com.alibaba.boot</groupId>  
            <artifactId>dubbo-spring-boot-starter</artifactId>  
            <version>0.2.0</version>  
        </dependency>  
  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter</artifactId>  
        </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>  
    </dependencies>  
  
    <build>  
        <plugins>  
            <plugin>  
                <groupId>org.springframework.boot</groupId>  
                <artifactId>spring-boot-maven-plugin</artifactId>  
            </plugin>  
        </plugins>  
    </build>  
  
</project>

To implement the UserService interface, the producer of dubbo needs to use @ Service annotation. In order to distinguish it from the @ Service annotation of spring, the @ Component annotation is used here. More articles on springboot: springboot content aggregation

UserServiceImpl.java

package com.cott.gmail.bootuserserviceprovider.service.impl;  
  
import com.alibaba.dubbo.config.annotation.Service;  
import com.cott.gmail.bean.UserAddress;  
import com.cott.gmail.service.UserService;  
import org.springframework.stereotype.Component;  
  
import java.util.Arrays;  
import java.util.List;  
  
@Service  
@Component  
public class UserServiceImpl implements UserService {  
  
    @Override  
    public List<UserAddress> getAddress(String uesrId) {  
        UserAddress userAddress1 = new UserAddress();  
        userAddress1.setId(1);  
        userAddress1.setUserAddress("1");  
        userAddress1.setUserId("1");  
        UserAddress userAddress2 = new UserAddress();  
        userAddress2.setId(2);  
        userAddress2.setUserAddress("2");  
        userAddress2.setUserId("2");  
  
        return Arrays.asList(userAddress1, userAddress2);  
    }  
}

Enable Dubbo annotation with @ EnableDubbo on the main method

BootUserServiceProviderApplication.java

package com.cott.gmail.bootuserserviceprovider;  
  
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;  
import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.SpringBootApplication;  
  
@EnableDubbo  
@SpringBootApplication  
public class BootUserServiceProviderApplication {  
  
    public static void main(String[] args) {  
        SpringApplication.run(BootUserServiceProviderApplication.class, args);  
    }  
  
}

Profile specifies application name, registry, service protocol and port number

application.yml

dubbo:  
  application:  
    name: boot-user-service-provider  
  registry:  
    address: 192.168.200.128:2181  
    protocol: zookeeper  
  protocol:  
    name: dubbo  
    port: 20880

Finally, start the program and check that the service has been registered in Dubbo admin.

4. Create a consumer spring boot project

pom.xml introduces the above interface project and dubbo dependency

<?xml version="1.0" encoding="UTF-8"?>  
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">  
    <modelVersion>4.0.0</modelVersion>  
    <parent>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-parent</artifactId>  
        <version>2.2.4.RELEASE</version>  
        <relativePath/> <!-- lookup parent from repository -->  
    </parent>  
    <groupId>com.cott.gmail</groupId>  
    <artifactId>boot-order-service-consumer</artifactId>  
    <version>0.0.1-SNAPSHOT</version>  
    <name>boot-order-service-consumer</name>  
    <description>Demo project for Spring Boot</description>  
  
    <properties>  
        <java.version>1.8</java.version>  
    </properties>  
  
    <dependencies>  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter</artifactId>  
        </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>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-web</artifactId>  
        </dependency>  
        <dependency>  
            <groupId>com.cott.gmail</groupId>  
            <artifactId>api-interface</artifactId>  
            <version>1.0-SNAPSHOT</version>  
            <scope>compile</scope>  
        </dependency>  
        <dependency>  
            <groupId>com.alibaba.boot</groupId>  
            <artifactId>dubbo-spring-boot-starter</artifactId>  
            <version>0.2.0</version>  
        </dependency>  
    </dependencies>  
  
    <build>  
        <plugins>  
            <plugin>  
                <groupId>org.springframework.boot</groupId>  
                <artifactId>spring-boot-maven-plugin</artifactId>  
            </plugin>  
        </plugins>  
    </build>  
  
</project>

Implement the orderservice interface, where @ Service is spring, and use @ Reference annotation to declare that UserService is injected through remote call

OrderServiceImpl.java

package com.cott.gmail.bootorderserviceconsumer.service.impl;  
  
import com.alibaba.dubbo.config.annotation.Reference;  
import com.cott.gmail.bean.UserAddress;  
import com.cott.gmail.service.OrderService;  
import com.cott.gmail.service.UserService;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.stereotype.Service;  
  
import java.util.List;  
  
@Service  
public class OrderServiceImpl implements OrderService {  
  
    @Reference  
    UserService userService;  
  
    @Override  
    public List<UserAddress> initOrder(String id) {  
        System.out.println("id= " + id);  
        List<UserAddress> list = userService.getAddress("1");  
        for (UserAddress user : list  
        ) {  
            System.out.println(user.getUserAddress());  
        }  
        return list;  
    }  
}

OrderController.java

package com.cott.gmail.bootorderserviceconsumer.controller;  
  
import com.cott.gmail.bean.UserAddress;  
import com.cott.gmail.service.OrderService;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.stereotype.Controller;  
import org.springframework.web.bind.annotation.GetMapping;  
import org.springframework.web.bind.annotation.RequestParam;  
import org.springframework.web.bind.annotation.ResponseBody;  
  
import java.util.List;  
  
@Controller  
public class OrderController {  
  
    @Autowired  
    OrderService orderService;  
  
    @ResponseBody  
    @GetMapping("/initOrder")  
    public List<UserAddress> initOrder(@RequestParam(name = "id") String id) {  
        return orderService.initOrder(id);  
    }  
}

@EnableDubbo enable Dubbo annotation

BootOrderServiceConsumerApplication.java

package com.cott.gmail.bootorderserviceconsumer;  
  
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;  
import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.SpringBootApplication;  
  
@EnableDubbo  
@SpringBootApplication  
public class BootOrderServiceConsumerApplication {  
  
    public static void main(String[] args) {  
        SpringApplication.run(BootOrderServiceConsumerApplication.class, args);  
    }  
  
}

The service name, registry, and tomcat external port number are declared in the configuration file

application.yml

dubbo:  
  application:  
    name: order-service-provider  
  registry:  
    address: 192.168.200.128:2181  
    protocol: zookeeper  
  
server:  
  port: 8081

So far, a simple dubbo project has been developed. Next, start the consumer project, enter the url in the browser, and get the return result

Thank you for your reference- http://bjbsair.com/2020-04-01/tech-info/18390.html

1. Choose ZooKeeper as the registration center

Using docker to install ZooKeeper in linux Environment

//Pull zookeeper image  
docker pull zookeeper  
  
//Start zookeeper  
docker run -d -p 2181:2181 -v /mysoft/zookeeper/data/:/data/ --name=zookeeper --privileged zookeeper

Use the zookeeper dev zooinspector client connection tool to view

2. Create Maven project unified declaration interface, extract all interfaces to separate projects, and introduce them to other projects through maven

pom.xml

<?xml version="1.0" encoding="UTF-8"?>  
<project xmlns="http://maven.apache.org/POM/4.0.0"  
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
    <modelVersion>4.0.0</modelVersion>  
  
    <groupId>com.cott.gmail</groupId>  
    <artifactId>api-interface</artifactId>  
    <version>1.0-SNAPSHOT</version>  
  
</project>

UserAddress.java

package com.cott.gmail.bean;  
  
import java.io.Serializable;  
  
public class UserAddress implements Serializable {  
    private Integer id;  
    private String userAddress;  
    private String userId;  
  
    public Integer getId() {  
        return id;  
    }  
  
    public void setId(Integer id) {  
        this.id = id;  
    }  
  
    public String getUserAddress() {  
        return userAddress;  
    }  
  
    public void setUserAddress(String userAddress) {  
        this.userAddress = userAddress;  
    }  
  
    public String getUserId() {  
        return userId;  
    }  
  
    public void setUserId(String userId) {  
        this.userId = userId;  
    }  
}

OrderService.java

package com.cott.gmail.service;  
  
import com.cott.gmail.bean.UserAddress;  
  
import java.util.List;  
  
public interface OrderService {  
  
    List<UserAddress> initOrder(String id);  
}

UserService.java

package com.cott.gmail.service;  
  
import com.cott.gmail.bean.UserAddress;  
  
import java.util.List;  
  
public interface UserService {  
    List<UserAddress> getAddress(String userId);  
}

3. Create producer SpringBoot project

pom.xml introduces the above interface project and dubbo dependency

<?xml version="1.0" encoding="UTF-8"?>  
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">  
    <modelVersion>4.0.0</modelVersion>  
    <parent>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-parent</artifactId>  
        <version>2.2.4.RELEASE</version>  
        <relativePath/> <!-- lookup parent from repository -->  
    </parent>  
    <groupId>com.cott.gmail</groupId>  
    <artifactId>boot-user-service-provider</artifactId>  
    <version>0.0.1-SNAPSHOT</version>  
    <name>boot-user-service-provider</name>  
    <description>Demo project for Spring Boot</description>  
  
    <properties>  
        <java.version>1.8</java.version>  
    </properties>  
  
    <dependencies>  
        <dependency>  
            <groupId>com.cott.gmail</groupId>  
            <artifactId>api-interface</artifactId>  
            <version>1.0-SNAPSHOT</version>  
        </dependency>  
  
        <dependency>  
            <groupId>com.alibaba.boot</groupId>  
            <artifactId>dubbo-spring-boot-starter</artifactId>  
            <version>0.2.0</version>  
        </dependency>  
  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter</artifactId>  
        </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>  
    </dependencies>  
  
    <build>  
        <plugins>  
            <plugin>  
                <groupId>org.springframework.boot</groupId>  
                <artifactId>spring-boot-maven-plugin</artifactId>  
            </plugin>  
        </plugins>  
    </build>  
  
</project>

To implement the UserService interface, the producer of dubbo needs to use @ Service annotation. In order to distinguish it from the @ Service annotation of spring, the @ Component annotation is used here. More articles on springboot: springboot content aggregation

UserServiceImpl.java

package com.cott.gmail.bootuserserviceprovider.service.impl;  
  
import com.alibaba.dubbo.config.annotation.Service;  
import com.cott.gmail.bean.UserAddress;  
import com.cott.gmail.service.UserService;  
import org.springframework.stereotype.Component;  
  
import java.util.Arrays;  
import java.util.List;  
  
@Service  
@Component  
public class UserServiceImpl implements UserService {  
  
    @Override  
    public List<UserAddress> getAddress(String uesrId) {  
        UserAddress userAddress1 = new UserAddress();  
        userAddress1.setId(1);  
        userAddress1.setUserAddress("1");  
        userAddress1.setUserId("1");  
        UserAddress userAddress2 = new UserAddress();  
        userAddress2.setId(2);  
        userAddress2.setUserAddress("2");  
        userAddress2.setUserId("2");  
  
        return Arrays.asList(userAddress1, userAddress2);  
    }  
}

Enable Dubbo annotation with @ EnableDubbo on the main method

BootUserServiceProviderApplication.java

package com.cott.gmail.bootuserserviceprovider;  
  
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;  
import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.SpringBootApplication;  
  
@EnableDubbo  
@SpringBootApplication  
public class BootUserServiceProviderApplication {  
  
    public static void main(String[] args) {  
        SpringApplication.run(BootUserServiceProviderApplication.class, args);  
    }  
  
}

Profile specifies application name, registry, service protocol and port number

application.yml

dubbo:  
  application:  
    name: boot-user-service-provider  
  registry:  
    address: 192.168.200.128:2181  
    protocol: zookeeper  
  protocol:  
    name: dubbo  
    port: 20880

Finally, start the program and check that the service has been registered in Dubbo admin.

4. Create a consumer spring boot project

pom.xml introduces the above interface project and dubbo dependency

<?xml version="1.0" encoding="UTF-8"?>  
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">  
    <modelVersion>4.0.0</modelVersion>  
    <parent>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-parent</artifactId>  
        <version>2.2.4.RELEASE</version>  
        <relativePath/> <!-- lookup parent from repository -->  
    </parent>  
    <groupId>com.cott.gmail</groupId>  
    <artifactId>boot-order-service-consumer</artifactId>  
    <version>0.0.1-SNAPSHOT</version>  
    <name>boot-order-service-consumer</name>  
    <description>Demo project for Spring Boot</description>  
  
    <properties>  
        <java.version>1.8</java.version>  
    </properties>  
  
    <dependencies>  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter</artifactId>  
        </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>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-web</artifactId>  
        </dependency>  
        <dependency>  
            <groupId>com.cott.gmail</groupId>  
            <artifactId>api-interface</artifactId>  
            <version>1.0-SNAPSHOT</version>  
            <scope>compile</scope>  
        </dependency>  
        <dependency>  
            <groupId>com.alibaba.boot</groupId>  
            <artifactId>dubbo-spring-boot-starter</artifactId>  
            <version>0.2.0</version>  
        </dependency>  
    </dependencies>  
  
    <build>  
        <plugins>  
            <plugin>  
                <groupId>org.springframework.boot</groupId>  
                <artifactId>spring-boot-maven-plugin</artifactId>  
            </plugin>  
        </plugins>  
    </build>  
  
</project>

Implement the orderservice interface, where @ Service is spring, and use @ Reference annotation to declare that UserService is injected through remote call

OrderServiceImpl.java

package com.cott.gmail.bootorderserviceconsumer.service.impl;  
  
import com.alibaba.dubbo.config.annotation.Reference;  
import com.cott.gmail.bean.UserAddress;  
import com.cott.gmail.service.OrderService;  
import com.cott.gmail.service.UserService;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.stereotype.Service;  
  
import java.util.List;  
  
@Service  
public class OrderServiceImpl implements OrderService {  
  
    @Reference  
    UserService userService;  
  
    @Override  
    public List<UserAddress> initOrder(String id) {  
        System.out.println("id= " + id);  
        List<UserAddress> list = userService.getAddress("1");  
        for (UserAddress user : list  
        ) {  
            System.out.println(user.getUserAddress());  
        }  
        return list;  
    }  
}

OrderController.java

package com.cott.gmail.bootorderserviceconsumer.controller;  
  
import com.cott.gmail.bean.UserAddress;  
import com.cott.gmail.service.OrderService;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.stereotype.Controller;  
import org.springframework.web.bind.annotation.GetMapping;  
import org.springframework.web.bind.annotation.RequestParam;  
import org.springframework.web.bind.annotation.ResponseBody;  
  
import java.util.List;  
  
@Controller  
public class OrderController {  
  
    @Autowired  
    OrderService orderService;  
  
    @ResponseBody  
    @GetMapping("/initOrder")  
    public List<UserAddress> initOrder(@RequestParam(name = "id") String id) {  
        return orderService.initOrder(id);  
    }  
}

@EnableDubbo enable Dubbo annotation

BootOrderServiceConsumerApplication.java

package com.cott.gmail.bootorderserviceconsumer;  
  
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;  
import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.SpringBootApplication;  
  
@EnableDubbo  
@SpringBootApplication  
public class BootOrderServiceConsumerApplication {  
  
    public static void main(String[] args) {  
        SpringApplication.run(BootOrderServiceConsumerApplication.class, args);  
    }  
  
}

The service name, registry, and tomcat external port number are declared in the configuration file

application.yml

dubbo:  
  application:  
    name: order-service-provider  
  registry:  
    address: 192.168.200.128:2181  
    protocol: zookeeper  
  
server:  
  port: 8081

So far, a simple dubbo project has been developed. Next, start the consumer project, enter the url in the browser, and get the return result

Thank you for your reference- http://bjbsair.com/2020-04-01/tech-info/18390.html

1. Choose ZooKeeper as the registration center

Using docker to install ZooKeeper in linux Environment

//Pull zookeeper image  
docker pull zookeeper  
  
//Start zookeeper  
docker run -d -p 2181:2181 -v /mysoft/zookeeper/data/:/data/ --name=zookeeper --privileged zookeeper

Use the zookeeper dev zooinspector client connection tool to view

2. Create Maven project unified declaration interface, extract all interfaces to separate projects, and introduce them to other projects through maven

pom.xml

<?xml version="1.0" encoding="UTF-8"?>  
<project xmlns="http://maven.apache.org/POM/4.0.0"  
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
    <modelVersion>4.0.0</modelVersion>  
  
    <groupId>com.cott.gmail</groupId>  
    <artifactId>api-interface</artifactId>  
    <version>1.0-SNAPSHOT</version>  
  
</project>

UserAddress.java

package com.cott.gmail.bean;  
  
import java.io.Serializable;  
  
public class UserAddress implements Serializable {  
    private Integer id;  
    private String userAddress;  
    private String userId;  
  
    public Integer getId() {  
        return id;  
    }  
  
    public void setId(Integer id) {  
        this.id = id;  
    }  
  
    public String getUserAddress() {  
        return userAddress;  
    }  
  
    public void setUserAddress(String userAddress) {  
        this.userAddress = userAddress;  
    }  
  
    public String getUserId() {  
        return userId;  
    }  
  
    public void setUserId(String userId) {  
        this.userId = userId;  
    }  
}

OrderService.java

package com.cott.gmail.service;  
  
import com.cott.gmail.bean.UserAddress;  
  
import java.util.List;  
  
public interface OrderService {  
  
    List<UserAddress> initOrder(String id);  
}

UserService.java

package com.cott.gmail.service;  
  
import com.cott.gmail.bean.UserAddress;  
  
import java.util.List;  
  
public interface UserService {  
    List<UserAddress> getAddress(String userId);  
}

3. Create producer SpringBoot project

pom.xml introduces the above interface project and dubbo dependency

<?xml version="1.0" encoding="UTF-8"?>  
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">  
    <modelVersion>4.0.0</modelVersion>  
    <parent>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-parent</artifactId>  
        <version>2.2.4.RELEASE</version>  
        <relativePath/> <!-- lookup parent from repository -->  
    </parent>  
    <groupId>com.cott.gmail</groupId>  
    <artifactId>boot-user-service-provider</artifactId>  
    <version>0.0.1-SNAPSHOT</version>  
    <name>boot-user-service-provider</name>  
    <description>Demo project for Spring Boot</description>  
  
    <properties>  
        <java.version>1.8</java.version>  
    </properties>  
  
    <dependencies>  
        <dependency>  
            <groupId>com.cott.gmail</groupId>  
            <artifactId>api-interface</artifactId>  
            <version>1.0-SNAPSHOT</version>  
        </dependency>  
  
        <dependency>  
            <groupId>com.alibaba.boot</groupId>  
            <artifactId>dubbo-spring-boot-starter</artifactId>  
            <version>0.2.0</version>  
        </dependency>  
  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter</artifactId>  
        </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>  
    </dependencies>  
  
    <build>  
        <plugins>  
            <plugin>  
                <groupId>org.springframework.boot</groupId>  
                <artifactId>spring-boot-maven-plugin</artifactId>  
            </plugin>  
        </plugins>  
    </build>  
  
</project>

To implement the UserService interface, the producer of dubbo needs to use @ Service annotation. In order to distinguish it from the @ Service annotation of spring, the @ Component annotation is used here. More articles on springboot: springboot content aggregation

UserServiceImpl.java

package com.cott.gmail.bootuserserviceprovider.service.impl;  
  
import com.alibaba.dubbo.config.annotation.Service;  
import com.cott.gmail.bean.UserAddress;  
import com.cott.gmail.service.UserService;  
import org.springframework.stereotype.Component;  
  
import java.util.Arrays;  
import java.util.List;  
  
@Service  
@Component  
public class UserServiceImpl implements UserService {  
  
    @Override  
    public List<UserAddress> getAddress(String uesrId) {  
        UserAddress userAddress1 = new UserAddress();  
        userAddress1.setId(1);  
        userAddress1.setUserAddress("1");  
        userAddress1.setUserId("1");  
        UserAddress userAddress2 = new UserAddress();  
        userAddress2.setId(2);  
        userAddress2.setUserAddress("2");  
        userAddress2.setUserId("2");  
  
        return Arrays.asList(userAddress1, userAddress2);  
    }  
}

Enable Dubbo annotation with @ EnableDubbo on the main method

BootUserServiceProviderApplication.java

package com.cott.gmail.bootuserserviceprovider;  
  
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;  
import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.SpringBootApplication;  
  
@EnableDubbo  
@SpringBootApplication  
public class BootUserServiceProviderApplication {  
  
    public static void main(String[] args) {  
        SpringApplication.run(BootUserServiceProviderApplication.class, args);  
    }  
  
}

Profile specifies application name, registry, service protocol and port number

application.yml

dubbo:  
  application:  
    name: boot-user-service-provider  
  registry:  
    address: 192.168.200.128:2181  
    protocol: zookeeper  
  protocol:  
    name: dubbo  
    port: 20880

Finally, start the program and check that the service has been registered in Dubbo admin.

4. Create a consumer spring boot project

pom.xml introduces the above interface project and dubbo dependency

<?xml version="1.0" encoding="UTF-8"?>  
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">  
    <modelVersion>4.0.0</modelVersion>  
    <parent>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-parent</artifactId>  
        <version>2.2.4.RELEASE</version>  
        <relativePath/> <!-- lookup parent from repository -->  
    </parent>  
    <groupId>com.cott.gmail</groupId>  
    <artifactId>boot-order-service-consumer</artifactId>  
    <version>0.0.1-SNAPSHOT</version>  
    <name>boot-order-service-consumer</name>  
    <description>Demo project for Spring Boot</description>  
  
    <properties>  
        <java.version>1.8</java.version>  
    </properties>  
  
    <dependencies>  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter</artifactId>  
        </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>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-web</artifactId>  
        </dependency>  
        <dependency>  
            <groupId>com.cott.gmail</groupId>  
            <artifactId>api-interface</artifactId>  
            <version>1.0-SNAPSHOT</version>  
            <scope>compile</scope>  
        </dependency>  
        <dependency>  
            <groupId>com.alibaba.boot</groupId>  
            <artifactId>dubbo-spring-boot-starter</artifactId>  
            <version>0.2.0</version>  
        </dependency>  
    </dependencies>  
  
    <build>  
        <plugins>  
            <plugin>  
                <groupId>org.springframework.boot</groupId>  
                <artifactId>spring-boot-maven-plugin</artifactId>  
            </plugin>  
        </plugins>  
    </build>  
  
</project>

Implement the orderservice interface, where @ Service is spring, and use @ Reference annotation to declare that UserService is injected through remote call

OrderServiceImpl.java

package com.cott.gmail.bootorderserviceconsumer.service.impl;  
  
import com.alibaba.dubbo.config.annotation.Reference;  
import com.cott.gmail.bean.UserAddress;  
import com.cott.gmail.service.OrderService;  
import com.cott.gmail.service.UserService;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.stereotype.Service;  
  
import java.util.List;  
  
@Service  
public class OrderServiceImpl implements OrderService {  
  
    @Reference  
    UserService userService;  
  
    @Override  
    public List<UserAddress> initOrder(String id) {  
        System.out.println("id= " + id);  
        List<UserAddress> list = userService.getAddress("1");  
        for (UserAddress user : list  
        ) {  
            System.out.println(user.getUserAddress());  
        }  
        return list;  
    }  
}

OrderController.java

package com.cott.gmail.bootorderserviceconsumer.controller;  
  
import com.cott.gmail.bean.UserAddress;  
import com.cott.gmail.service.OrderService;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.stereotype.Controller;  
import org.springframework.web.bind.annotation.GetMapping;  
import org.springframework.web.bind.annotation.RequestParam;  
import org.springframework.web.bind.annotation.ResponseBody;  
  
import java.util.List;  
  
@Controller  
public class OrderController {  
  
    @Autowired  
    OrderService orderService;  
  
    @ResponseBody  
    @GetMapping("/initOrder")  
    public List<UserAddress> initOrder(@RequestParam(name = "id") String id) {  
        return orderService.initOrder(id);  
    }  
}

@EnableDubbo enable Dubbo annotation

BootOrderServiceConsumerApplication.java

package com.cott.gmail.bootorderserviceconsumer;  
  
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;  
import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.SpringBootApplication;  
  
@EnableDubbo  
@SpringBootApplication  
public class BootOrderServiceConsumerApplication {  
  
    public static void main(String[] args) {  
        SpringApplication.run(BootOrderServiceConsumerApplication.class, args);  
    }  
  
}

The service name, registry, and tomcat external port number are declared in the configuration file

application.yml

dubbo:  
  application:  
    name: order-service-provider  
  registry:  
    address: 192.168.200.128:2181  
    protocol: zookeeper  
  
server:  
  port: 8081

So far, a simple dubbo project has been developed. Next, start the consumer project, enter the url in the browser, and get the return result

Tags: Programming Java Spring Maven Dubbo

Posted on Fri, 03 Apr 2020 18:48:23 -0700 by dusty