springboot+dubbo+zookeeper

1在apache官网下载zookeeper

2.在conf下面新建一个zoo.cfg,拷贝zoo_sample.cfg即可

3.打开zkServer.cmd

provide-server

1.导入jar包

        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.6</version>
        </dependency>

        <!-- zk的依赖 -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.12.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>2.12.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.14</version>
            <!--排除这个slf4j-log4j12-->
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

配置文件

server.port=8081

# 服务应用名字
dubbo.application.name=provider-server
# 注册中心地址
dubbo.registry.address=zookeeper://127.0.0.1:2181
#那些服务要被注册
dubbo.scan.base-packages=com.lzh.service

consumer-server

依赖和生产者一样

配置文件

server.port=8082

# 服务应用名字
dubbo.application.name=consumer-server
# 注册中心地址
dubbo.registry.address=zookeeper://127.0.0.1:2181
#那些服务要被注册
@Controller
public class HelloController {

    @Reference
    TicketService ticketService;

    @ResponseBody
    @RequestMapping("/hello")
    public String test(){
        System.out.println(ticketService.getTicket());
        return "success";
    }
}

利用@Reference注解引入

注意:此类需要和生产者同名并且在相同目录下

@SpringBootApplication
@EnableDubbo
public class ConsumerServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConsumerServerApplication.class, args);
    }

}

在启动类加上@EnableDubbo就可以访问了

 

posted @ 2020-12-13 23:57  jesscia5  阅读(140)  评论(0编辑  收藏  举报