一)公共应用

建立一个公共的应用,专门放置公共的pojo和接口。

 1,pojo中UserAddress.java内容,注意要序列化

public class UserAddress implements Serializable {
    private Integer id;
    private Integer userId;
    private String address;

    public UserAddress(Integer id, Integer userId, String address) {
        this.id = id;
        this.userId = userId;
        this.address = address;
    }

    @Override
    public String toString() {
        return "UserAddress{" +
                "id=" + id +
                ", userId=" + userId +
                ", address='" + address + '\'' +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

2,公共接口UserService

public interface UserService {
    List<UserAddress> getAddressByUserId(Integer id);
}

二)提供者应用

实现接口,暴露服务,提供接口实现给消费者应用

 1,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.company</groupId>
  <artifactId>dubbo-demo</artifactId>
  <packaging>pom</packaging>
  <version>1.0-SNAPSHOT</version>
  <modules>
    <module>common-api</module>
      <module>order-service-consumer</module>
      <module>user-service-provider</module>
  </modules>

  <name>dubbo-demo</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>

    <spring-version>5.2.0.RELEASE</spring-version>
    <junit-version>4.12</junit-version>
    <dubbo-version>2.6.6</dubbo-version>
    <netty-version>4.1.32.Final</netty-version>
    <curator-version>4.0.1</curator-version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>${junit-version}</version>
      <scope>test</scope>
    </dependency>

    <!--dubbo依赖-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>dubbo</artifactId>
      <version>${dubbo-version}</version>
    </dependency>

    <!--使用dubbo,必须先引用netty和curator,否则会报错-->
    <dependency>
      <groupId>io.netty</groupId>
      <artifactId>netty-all</artifactId>
      <version>${netty-version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.curator</groupId>
      <artifactId>curator-framework</artifactId>
      <version>${curator-version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.curator</groupId>
      <artifactId>curator-recipes</artifactId>
      <version>${curator-version}</version>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

 2,实现接口UserServiceImpl.java

public class UserServiceImpl implements UserService {
    private static List<UserAddress> list=new ArrayList<>();

    static {
        list.add(new UserAddress(1, 100, "北京")) ;
        list.add(new UserAddress(2, 100, "上海"));
        list.add(new UserAddress(3, 200, "广州")) ;
        list.add(new UserAddress(4, 300, "深圳"));
    }

    @Override
    public List<UserAddress> getAddressByUserId(Integer id) {
        List<UserAddress> userList = new ArrayList<>();
        for (int i = 0; i <list.size() ; i++) {
            UserAddress address = list.get(i);
            if(id.equals(address.getUserId())){
                userList.add(address);
            }
        }
        return userList;
    }
}

3,配置spring全局配置文件,provider.xml暴露服务。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans.xsd 
       http://code.alibabatech.com/schema/dubbo 
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 提供方应用信息,用于计算依赖关系-->
    <dubbo:application name="user-service-provider"/>
    <!--使用dubbo协议在20880端口暴露服务-->
    <dubbo:protocol name="dubbo" port="20880"/>
    <!-- 使用multicast广播注册中心暴露服务地址 提供者和消费者在同一机器上需要加上unicast=false-->
    <dubbo:registry address="multicast://224.5.6.7:1234?unicast=false"/>
    <!--声明需要暴露的服务接口-->
    <dubbo:service interface="com.company.service.UserService" ref="userService"/>
    <!--和本地bean一样实现服务-->
    <bean id="userService" class="com.company.service.UserServiceImpl"/>
</beans>

4,启动主程序,暴露服务

public class ProviderMain {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
        context.start();
        System.in.read();
    }
}

三)消费者应用

远程调用提供者应用的接口

 1,pom文件添加依赖

<?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.company</groupId>
  <artifactId>order-service-consumer</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>order-service-consumer</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>

      <spring-version>5.2.0.RELEASE</spring-version>
      <junit-version>4.12</junit-version>
      <dubbo-version>2.6.6</dubbo-version>
      <netty-version>4.1.32.Final</netty-version>
      <curator-version>4.0.1</curator-version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>${junit-version}</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>dubbo</artifactId>
      <version>${dubbo-version}</version>
    </dependency>

    <dependency>
      <groupId>com.company</groupId>
      <artifactId>common-api</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>

    <dependency>
      <groupId>io.netty</groupId>
      <artifactId>netty-all</artifactId>
      <version>${netty-version}</version>
    </dependency>

    <dependency>
      <groupId>org.apache.curator</groupId>
      <artifactId>curator-framework</artifactId>
      <version>${curator-version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.curator</groupId>
      <artifactId>curator-recipes</artifactId>
      <version>${curator-version}</version>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

2,配置spring全局配置文件consumer.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans.xsd 
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context.xsd   
       http://code.alibabatech.com/schema/dubbo 
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!--扫描包下的组件-->
    <context:component-scan base-package="com.company.service"/>
    <!-- 消费方应用信息,用于计算依赖关系-->
    <dubbo:application name="order-service-consumer"/>
    <!-- 使用multicast广播注册中心暴露服务地址 提供者和消费者在同一机器上需要加上unicast=false-->
    <dubbo:registry address="multicast://224.5.6.7:1234?unicast=false"/>
    <!-- 生成远程服务代理,可以和本地bean一样使用userService -->
    <dubbo:reference id="userService" interface="com.company.service.UserService"/>

</beans>

3,远程调用提供者接口,处理业务逻辑

public interface OrderService {
    List<UserAddress> getAddressByUserId(Integer id);
}
@Service("orderService")
public class OrderServiceImpl implements OrderService {

    @Autowired
    private UserService userService;

    @Override
    public List<UserAddress> getAddressByUserId(Integer id) {
        return userService.getAddressByUserId(id);
    }
}

4,启动主程序,远程调用服务

public class ConsumerMain {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml");
        context.start();
        OrderService orderService = context.getBean("orderService", OrderServiceImpl.class);
        List<UserAddress> addressList = orderService.getAddressByUserId(300);
        addressList.forEach(System.out::println);
        System.out.println("执行oK");
    }
}

注意事项:dubbo的注册中心有很多,当使用multicast时,如果消费者应用和提供者应用在同一台机器上,两边需要设置unicast=false,如:multicast://224.5.6.7:1234?unicast=false。否则,会报错;如果本机刚好有使用vmware虚拟机,需要禁用虚拟机网络,因为Dubbo默认使用单播发送提供者地址信息给消费者,而安装Vmware虚拟机会导致网络混乱,Multicast注册中心会出错。

设备管理器===》网络适配器===》禁用Vmware相关的网络适配器

 

 posted on 2021-04-11 23:20  会飞的金鱼  阅读(124)  评论(0)    收藏  举报