Examples of the recording dubbo + zookeeper

 Tip: To set up a direct look at the example can be skipped the

A. The development of infrastructure projects

The need to increase the number of traditional server architecture mvc project will realize the full function of the whole system written in a project deployed on a machine, with the increasing amount of users rose, a single server can not withstand the surge of projects requested by the user, and through the load balanced cluster project will be deployed across multiple servers, users can improve the project's total number of requests processed.

When the user mvc architecture project in one or several modules of the large amount of requests, other modules the user requests a smaller amount, then load balancing cluster each server deployment is still a complete project, the actual need for load balancing only the user requests a large amount of several modules, where the system can be distributed to the functional module as a service.

In the project is too large, when you deploy a single project can not meet the demand, the project needs to be split into separate functional modules of different services deployed on different servers (ie distributed).

In distributed in between the various services to each other calls need to be accessed over the network in the case of you know the address. At this point a need that can make all services other intermediary service address, thus creating a service registry, service projects need to tell the service registry it is doing, that is conduct service registration,

You can call the other items to be invoked when a specific service can find the address registration services to the service center, access to services through a network address the service address on their own projects, which structure the whole process involved is content rpc architecture.

rpc architecture:

It includes service providers, service consumers, service registry;

: Service registry to save the service provider information in the service provider conduct service registration, service discovery service provider return address in the service of consumers;

Service provider provider: be registered in the service registry service, call waiting service consumers;

Service consumer consumer: service discovery in the service registry, a service call after obtaining the address of the service provider service registry returned;

rpc architecture Disadvantages: When too much is not known whether the project split Service service is by whom;

soa service architecture: plus service management system based on the architecture of the rpc;

 

What are the two .Zookeeper and Dubbo? What each act as a role in the project?

What is Zookeeper:

Apache ZooKeeper is a service used by the cluster (group of nodes) for coordination between themselves and to maintain share data through a robust synchronization technology. ZooKeeper itself is a distributed application that provides services to write distributed applications.

zookeeper act as role:

In the rpc architecture zookeeper is a registered service center, all services registered (registration service) in zookeeper, when a service invokes another service, ask the zookeeper to the network address of the service (service discovery).

What dubbo is:

Dubbo is a high performance, lightweight open-source Java RPC framework, which provides three core capabilities: an interface for remote method invocation, intelligent fault tolerance and load balancing, and automatic registration and service discovery.

dubbo act as role :

rpc-based systems in all services need to be registered service (service registry to tell who I am) and service discovery (tell me where to turn registry service), service providers and service consumers (that is, the service itself) may use dubbo responsible for service registration and service discovery.

dubbo handling of rpc architecture drawbacks:

Dubbo provides dubbo-admin service management system, run dubbo-admin service management system in the building dubbo + zookeeper, will become a service oriented architecture soa from rpc architecture.

 III. Dubbo + zookeeper build a small example

Can be installed and set up before the official start zookeeper (slightly), then install dubbo-admin, download dubbo-admin.war, renamed ROOT.war, put tomcat, you can modify dubbo.properties them after starting, and will address modify your account password into a zookeeper installed, and then restart the project

dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest

You can then access localhost: 8080 (modified to ROOT.war is to be able localhost: 8080 direct access to the project), log dubbo-admin can go shopping

 You can start to build in step 3 or 4 items, then to dubbo-admin service management system to see whether consumers and provide a corresponding data, this can be the basis to determine whether the service is successfully registered.

 

1. Create a maven project and create three sub-projects in the project, public service project for writing services interfaces, dubboProvider to achieve service Interface, dubboConsumer calling for the achievement of public service interface in the dubboProvider

2. Add an interface in service projects:

package com.lwl.service;

public interface DemoService {

    String sayHello(String name);
}

3.dubboProvider of operation:

3-1) was added in dependent projects pom.xml dubboProvider

        <!--公共服务接口依赖-->
        <dependency>
            <groupId>com.lwl</groupId>
            <artifactId>service</artifactId>
            <version>1.0</version>
        </dependency>
        <!--dubbo依赖 https://mvnrepository.com/artifact/com.alibaba/dubbo -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.10</version>
            <!-- 排除dubbo依赖自带的spring和netty依赖,防止自带的依赖与项目内依赖版本冲突,如果本身项目没有,无需排除 -->
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-web</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-context</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-beans</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.jboss.netty</groupId>
                    <artifactId>netty</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--zkclient依赖 https://mvnrepository.com/artifact/com.101tec/zkclient -->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.11</version>
        </dependency>
        <!--dubbo自身的spring相关依赖被排除,此处将dubbo所需要的对应依赖引入-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.1.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.1.8.RELEASE</version>
        </dependency>

3-2)创建DemoServiceImpl 类,实现公共服务接口

package com.lwl.service.impl;

import com.lwl.service.DemoService;

public class DemoServiceImpl implements DemoService {
    @Override
    public String sayHello(String name) {
        return "Hello " + name;
    }
}

3-3)在web.xml中配置启动spring

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

  <!-- 启动spring容器 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

</web-app>

3-4)在applicationContext.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-3.0.xsd 
                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
                            http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">


    <!--启动扫描-->
    <context:component-scan base-package="com.lwl.service"/>
    <!--声明公共服务接口实现类的bean-->
    <bean id="demoService" class="com.lwl.service.impl.DemoServiceImpl"/>
<!--dubbo:application:提供方的应用名--> <dubbo:application name="dubbo-provider"/> <!--dubbo:registry:注册中心的类型和地址--> <dubbo:registry address="zookeeper://127.0.0.1:2181" check="false" subscribe="false" register=""/> <!--dubbo:protocol:这个服务要暴露在哪个端口上(使用方根据这个端口使用服务)--> <dubbo:protocol name="dubbo" port="20880"/> <!--dubbo:service:设置暴露的服务接口,ref 为该接口的 bean,timeout 为超时时间--> <dubbo:service interface="com.lwl.service.DemoService" ref="demoService" timeout="600000"/> </beans>

4.在dubboConsumer项目中的操作:

4-1)在dubboConsumer项目的pom.xml文件添加依赖:

<!--公共服务接口依赖-->
        <dependency>
            <groupId>com.lwl</groupId>
            <artifactId>service</artifactId>
            <version>1.0</version>
        </dependency>
        <!--dubbo依赖 https://mvnrepository.com/artifact/com.alibaba/dubbo -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.10</version>
            <!-- 排除dubbo自带的spring和netty,使用项目的包,如果本身项目没有则无需排除 -->
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-web</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-context</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-beans</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.jboss.netty</groupId>
                    <artifactId>netty</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--zkclient依赖 https://mvnrepository.com/artifact/com.101tec/zkclient -->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.11</version>
        </dependency>

        <!-- SpringMVC: https://mvnrepository.com/artifact/org.springframework/spring-webmvc-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.8.RELEASE</version>
    </dependency>

4-2)在web.xml中启动springmvc

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

  <!-- SpringMVC核心控制器 -->
  <servlet>
    <servlet-name>spring-mvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring-mvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>


  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

4-3)在spring-mvc.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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-3.2.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.2.xsd
                        http://code.alibabatech.com/schema/dubbo
                        http://code.alibabatech.com/schema/dubbo/dubbo.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

    <!-- 注解探测器-->
    <context:component-scan base-package="com.lwl.controller"/>
    <context:annotation-config/>

    <!--dubbo:application: 使用方的应用名-->
    <dubbo:application name="dubbo-consumer"/>
    <!--dubbo:registry:注册中心的类型和地址-->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" check="false"/>
    <!--dubbo:reference:要使用的服务的接口,并将返回的注入 bean,名称为id设的值-->
    <dubbo:reference interface="com.lwl.service.DemoService" id="demoService"/>

    <!-- 注册注解驱动 -->
    <mvc:annotation-driven/>

</beans>

4-4)创建controller:

package com.lwl.controller;

import com.lwl.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class DemoController {

    // @Autowired
    // 默认按byType自动注入,当找不到对应类型的bean时才按byName自动注入,都找不到时抛出异常
    // 默认情况下必须要求依赖对象必须存在,如果要允许null值可使用@Autowired(required=false)
    // @Resource
    // 默认按byName自动注入,找不到时抛出异常
    @Resource//或使用@Autowired(required = false)
    private DemoService demoService;

    @GetMapping("/{name}")
    public String get(@PathVariable String name) {
        return demoService.sayHello(name);
    }

}

5.先启动dubboProvider项目,随后启动dubboConsumer项目,启动后访问http://localhost:8080/dubboConsumer/** 返回  Hello **   则配置成功

 

 

Guess you like

Origin www.cnblogs.com/new-life/p/11241722.html