springboot+zookeeper+dubbo

springboot+zookeeper+dubbo,第1张

一个简易版工程终于调通了,使用springboot框架,zookeeper注册中心,dubbo服务(提供者和消费者)。

一、dubbo服务提供者:
1.SayHelloImpl.java

package com.dubbo.provider.demo.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.dubbo.demo.api.ISayHello;

@Service
public class SayHelloImpl implements ISayHello {
    @Override
    public String hello(String name) {
        String out = "hello,"+name;
        return out;
    }
}

2.DemoApplication.java

package com.dubbo.provider.demo;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@EnableDubbo
@SpringBootApplication
public class DemoApplication {

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

}

3.application.properties

server.port=20000
spring.application.name=dubbo-provider-demo
# dubbo
dubbo.application.name=dubbo-provider-demo
dubbo.protocol.name=dubbo
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.protocol.port=10012

4.pom.xml



    4.0.0

    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.6.RELEASE
    

    com.dubbo.demo
    dubbo-provider-demo
    1.0.0-SNAPSHOT

    
        dubboProviderDemo
    

    

        
            com.dubbo.demo
            dubbo-demo-api
            1.0.0-SNAPSHOT
        

        
            com.alibaba
            dubbo
            2.6.12
            
                
                    spring
                    org.springframework
                
            
        

        
            org.apache.zookeeper
            zookeeper
            3.7.0
            
                
                    slf4j-log4j12
                    org.slf4j
                
            
        

        
            org.springframework.boot
            spring-boot-starter-web
            
                
                    spring-boot-starter-logging
                    org.springframework.boot
                
            
        
        
            org.apache.curator
            curator-framework
            4.0.1
        
        
            junit
            junit
            4.13
            test
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    


    
        ${project.artifactId}
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


二、dubbo服务消费者:

1.CallProviderService.java

package com.dubbo.customer.demo.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dubbo.demo.api.ISayHello;
import org.springframework.stereotype.Service;

@Service
public class CallProviderService {

    @Reference
    ISayHello sayHello;

    public void callInterface(){
       String res = sayHello.hello("hl");
        System.out.println("res--"+res);
    }

}

2.DemoApplication.java

package com.dubbo.customer.demo;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableDubbo
public class DemoApplication {

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

}

3.CallProviderServiceController.java

package com.dubbo.customer.demo.controller;

import com.dubbo.customer.demo.service.CallProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("test")
public class CallProviderServiceController {

    @Autowired
    private CallProviderService callProviderService;

    @RequestMapping("aa")
    public void subscribeSayHello(){
        callProviderService.callInterface();
    }

}

4.application.properties

server.port=26000
spring.application.name=dubbo-customer-demo

dubbo.application.name=dubbo-customer-demo
dubbo.protocol.name=dubbo
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.consumer.check=false

5.pom.xml



    4.0.0

    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.6.RELEASE
    

    com.dubbo.demo
    dubbo-customer-demo
    1.0.0-SNAPSHOT

    
        dubboCustomerDemo
    

    
        
            com.dubbo.demo
            dubbo-demo-api
            1.0.0-SNAPSHOT
        
        
            com.alibaba
            dubbo
            2.6.12
            
                
                    spring
                    org.springframework
                
            
        
        
            org.apache.zookeeper
            zookeeper
            3.7.0
            
                
                    slf4j-log4j12
                    org.slf4j
                
            
        
        
            org.apache.logging.log4j
            log4j-api
            2.15.0
        
        
            org.springframework.boot
            spring-boot-starter-web
            
                
                    spring-boot-starter-logging
                    org.springframework.boot
                
            
        
        
            org.apache.curator
            curator-framework
            4.0.1
        
        
            junit
            junit
            4.13
            test
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        ${project.artifactId}
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


本地调试过程中遇到的问题:

1.由于本地电脑启用了虚拟网卡,导致启动起来的提供者IP和消费者IP不同;因为提供者和消费者都注册在本地zookeeper,在zookeeper上查看两个服务信息,其IP应该确保是同一个。

解决方式:禁止本地电脑网络的虚拟网卡。

2.两个服务正常启动后,消费者总是提示找不到服务者提供的服务。后来发现消费者服务里没有引用zookeeper。这个问题困扰比较久。

解决方式:在消费者的pom文件里引入zookeeper

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/734479.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-27
下一篇 2022-04-27

发表评论

登录后才能评论

评论列表(0条)

保存