zoukankan      html  css  js  c++  java
  • 2020-03-1817:32:22springboot与分布式之dubbo+zookeeper

    zookeeper的启动:

    ./zkServer.sh start

    连接zookeeper:

    ./zkCli.sh

    查看节点:

    ls /

    临时关闭linux防火墙centOS6:

    /etc/init.d/iptables stop 

    注意:本案例是基于springboot1.5.12版本,springboot2.X版本目前相同代码是存在问题的

    服务提供者:

    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 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>1.5.12.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.study.springboot-dubbo</groupId>
        <artifactId>serprovider</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>serprovider</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
    
            <dependency>
                <groupId>com.alibaba.boot</groupId>
                <artifactId>dubbo-spring-boot-starter</artifactId>
                <version>0.1.0</version>
            </dependency>
    
            <!--引入zookeeper的客户端工具-->
            <!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient -->
            <dependency>
                <groupId>com.github.sgroschupf</groupId>
                <artifactId>zkclient</artifactId>
                <version>0.1</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</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>

    yml:

    dubbo:
      application:
        name: provider-ticket
      registry:
        address: zookeeper://192.168.213.128:2181
      scan:
        base-packages: com.study.springboot.dubboprovider.service
    server:
      port: 8801

    主启动类:

    package com.study.springboot.dubboprovider;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class SerproviderApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SerproviderApplication.class, args);
        }
    
    }

    service接口:

    package com.study.springboot.dubboprovider.service;
            //com.study.springboot.dubboprovider.service.TicketServie
    
    
    public interface TicketServie {
        public String sall(String msg);
    }

    service实现类:

    package com.study.springboot.dubboprovider.service;
    
    import com.alibaba.dubbo.config.annotation.Service;
    import com.study.springboot.dubboprovider.service.TicketServie;
    import org.springframework.stereotype.Component;
    
    
    @Component
    @Service
    public class TicketServiceImpl implements TicketServie {
        @Override
        public String sall(String msg) {
            System.out.println("售票成功。。请"+msg+"耐心等待出票");
            return "售票成功。。请"+msg+"耐心等待出票(*^_^*)。。。";
        }
    }

    服务消费者:
    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 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>1.5.12.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.study.springboot.dubboconsumer</groupId>
        <artifactId>dubboconsumer</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>dubboconsumer</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
    
            <dependency>
                <groupId>com.alibaba.boot</groupId>
                <artifactId>dubbo-spring-boot-starter</artifactId>
                <version>0.1.0</version>
            </dependency>
    
            <!--引入zookeeper的客户端工具-->
            <!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient -->
            <dependency>
                <groupId>com.github.sgroschupf</groupId>
                <artifactId>zkclient</artifactId>
                <version>0.1</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</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>

    yml:

    dubbo:
      application:
        name: consumer-ticket
      registry:
        address: zookeeper://192.168.213.128:2181
    server:
      port: 8802

    主启动:

    package com.study.springboot.dubboconsumer;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class DubboconsumerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DubboconsumerApplication.class, args);
        }
    
    }

    需要有和服务提供者相同包下的相同名称的接口:

    service接口:

    package com.study.springboot.dubboprovider.service;
    
    public interface TicketServie {
        public String sall(String msg);
    }

    controller:

    package com.study.springboot.dubboconsumer.controller;
    
    
    import com.alibaba.dubbo.config.annotation.Reference;
    import com.study.springboot.dubboprovider.service.TicketServie;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class ConsumerController {
    
        //com.study.springboot.dubboprovider.service.TicketServie
        @Reference
        private TicketServie ticketServie;
    
        @GetMapping("/test/dubbo/{name}")
        public String getResult(@PathVariable("name") String name){
            System.out.println("开始调用dubbo服务提供者提供的方法。。。。。。。");
            String result = ticketServie.sall(name);
            System.out.println("结束调用dubbo服务提供者提供的方法OK");
            return result;
        }
    }
  • 相关阅读:
    Java equals()和hashCode()重写总结
    常见ORM框架理解
    struts2的第一个小页面
    Java开发StringBuilder类
    “中软综合项目实训”——把学生当员工培养
    中软寻梦
    软件开发学习的5大技巧
    从学生心理入手
    IT技术人员的职业发展方向
    驱动创新 引领未来
  • 原文地址:https://www.cnblogs.com/lxw-all/p/12519274.html
Copyright © 2011-2022 走看看