Dubbo

1.介绍

1.什么是Dubbo:

  Dubbo是一款高性能、轻量级的开源Java RPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以

​ 分布式的远程调用框架,RPC框架。

2.什么是RPC

​ 通过ip地址和端口号进行远程调用service工程,执行service代码

3.为什么需要

为了高可用行性和高性能----电商

4.原理 见图

5.使用

2.一个简单的使用

0. 说明

首先先开Provider 再开Consumer

Provider 提供者 8081端口

Consumer 消费者 8082 端口

1.导入坐标依赖

P: pom

<packaging>war</packaging>


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

  <dependencies>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.2.8.RELEASE</version>
    </dependency>
    
    <dependency>
      <groupId>org.apache.dubbo</groupId>
      <artifactId>dubbo</artifactId>
      <version>2.7.8</version>
    </dependency>

    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.12</version>
    </dependency>


  </dependencies>

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>11</source>
          <target>11</target>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <configuration>
          <port>8081</port>
          <uriEncoding>UTF-8</uriEncoding>
          <path>/</path>
        </configuration>
      </plugin>
    </plugins>
  </build>


</project>

C:pom

<packaging>war</packaging>

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

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.2.8.RELEASE</version>
    </dependency>
    
    <dependency>
      <groupId>org.apache.dubbo</groupId>
      <artifactId>dubbo</artifactId>
      <version>2.7.8</version>
    </dependency>

    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.12</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>11</source>
          <target>11</target>
        </configuration>
      </plugin>
      
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <configuration>
          <port>8082</port>
          <path>/</path>
          <uriEncoding>UTF-8</uriEncoding>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

2.提供者

1.User.java

@Setter
@Getter
@ToString
public class User implements Serializable {
    private String id;
    private String loginAct;
    private String name;
    private String loginPwd;
    private String email;
}

2.Service 接口

public interface UserService {
    public List<User> findAll();
}

3.Service实现类

public class UserServiceImpl implements UserService {
    @Override
    public List<User> findAll(){
        List<User> userList = new ArrayList<>();
        for(int i=0;i<10;i++){
            User user = new User();

            user.setId(i+"");
            user.setName("Tony"+i);

            userList.add(user);
        }
        return userList;
    }
}

4.配置文件applicationContext-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://dubbo.apache.org/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://dubbo.apache.org/schema/dubbo
       http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!--配置dubbo的参数-->
    <!--直连,不使用注册中心-->
    <!--注册到注册中心的工程名称,必须要配置,且唯一。-->
    <dubbo:application name="dubbo-provider"/>

    <!--使用dubbo协议,以及配置端口号,默认为20880-->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!--注册服务,UserService userService = new UserServiceImpl();-->
    <bean id="userService" class="cn.ccut.dob.service.impl.UserServiceImpl" />
    <!--    采用直连的方式进行服务的注册-->
    <dubbo:service interface="cn.ccut.dob.service.UserService" ref="userService" registry="N/A"/>

    <!--    注册时参数和协议
        dubbo://192.168.254.1:20880/cn.ccut.dubbo.service.UserService?
            anyhost=true&
            application=dubbo-provider&
            bind.ip=192.168.254.1&
            bind.port=20880&
            dubbo=2.6.2&
            generic=false&
            interface=cn.ccut.dubbo.service.UserService&
            methods=findAll&
            pid=5788&
            side=provider&
            timestamp=1598584434334

        dubbo version: 2.6.2,
        current host: 192.168.254.1-->

</beans>

5.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
		 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
         http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
		 id="WebApp_ID" version="4.0">
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext*.xml</param-value>
  </context-param>
  <!--加载Spring容器的监听器-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
</web-app>

3.消费者

3.1 service and user cp provider

3.2 userController

package cn.ccut.dob.controller;
import cn.ccut.dob.domain.User;
import cn.ccut.dob.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
//@RequestMapping("/use")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/findAll")
    @ResponseBody
    public List<User> findAll(){
        System.out.println("[UserService] : "+userService);
        List<User> all = userService.findAll();
        System.out.println("[userList] : "+all);
        return all;
    }
}

3.3 applicationContext-consumer.xml: 加上mvc 命名空间

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

    <!--  springmvc的配置  -->
    <context:component-scan base-package="cn.ccut.dob.controller"/>
    <mvc:annotation-driven/>

    <!--    配置dubbo-->
    <!--    配置工程名称-->
    <dubbo:application name="dubbo-consumer"/>

    <dubbo:reference id="userService"
                     interface="cn.ccut.dob.service.UserService"
                     url="dubbo://localhost:20880"
                     registry="N/A"></dubbo:reference>
</beans>

3.4 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
		 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
         http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
		 id="WebApp_ID" version="4.0">

  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath*:applicationContext-consumer.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

4.优化

可以把user 和userService 抽取出来

posted @ 2020-09-28 00:17  Tony小哥  阅读(173)  评论(0编辑  收藏  举报