Spring Boot 整合 Dubbo 和 zookeeper

1)根据 dubbo 官方建议,首先创建一个接口项目,而这个项目只定义接口和实现类

    1.1  创建一个 maven 工程 ,用来管理所有的maven项目

    


  4.0.0
  com.demo.dubbo
  spring-dubbo-zook
  0.0.1-SNAPSHOT
  pom
  spring-dubbo-zook
  spring-dubbo-zook
  
  
  	1.8
  	UTF-8
  
  
  
  	
  		
		
		    org.springframework.boot
		    spring-boot-starter
		    2.2.0.RELEASE
		
		
		
		
		    org.springframework.boot
		    spring-boot-starter-web
		    2.2.0.RELEASE
		
		
  		
  		
	    
	        org.apache.dubbo
	        dubbo-spring-boot-starter
	        2.7.3
	     
	    
	    
		
		    org.apache.curator
		    curator-framework
		    4.2.0
		
		
		
		
		    org.apache.curator
		    curator-recipes
		    4.2.0
		 
    
	    
		
		    com.101tec
		    zkclient
		    0.11
		    
              
                  log4j
                  log4j
              
              
                  org.slf4j
                  slf4j-log4j12
              
          
		
  	
  
  
  
        
            
            
                maven-compiler-plugin
                
                3.5.1
                
                
                    1.8
                    1.8
                    
                    UTF-8
                
            
        
    
    
  
  spring-interface
  spring-provider
  spring-consumer
  

2) 创建接口

Spring Boot 整合 Dubbo 和 zookeeper_第1张图片

     2.2) 创建  service  接口包,并创建 UserService

package com.demo.springboot.service;

public interface UserService {
	String sayHello(String name);
}

    2.3) 将接口进行打包。

            maven -----> install

3) 创建服务提供者。

Spring Boot 整合 Dubbo 和 zookeeper_第2张图片

    3.1) 创建pom文件


  4.0.0
  
    com.demo.dubbo
    spring-dubbo-zook
    0.0.1-SNAPSHOT
  
  spring-provider
  spring-provider
  spring-provider
  
  
  
	  		
		    com.demo.dubbo
		    spring-interface
		    0.0.1-SNAPSHOT
	  	
  	
		
		    org.springframework.boot
		    spring-boot-starter
		
		
		
		
		    org.springframework.boot
		    spring-boot-starter-web
		
		
  		
  		
	    
	        org.apache.dubbo
	        dubbo-spring-boot-starter
	     
	    
	    
		
		    org.apache.curator
		    curator-framework		    
		
		
		
		
		    org.apache.curator
		    curator-recipes		    
		 
    
	    
		
		    com.101tec
		    zkclient		    
		    
              
                  log4j
                  log4j
              
              
                  org.slf4j
                  slf4j-log4j12
              
          
		
  	
  

     3.2) 创建 application.properties 文件

     

server.port: 8085
  
dubbo.application.name: spring-provider
dubbo.registry.address: zookeeper://192.168.190.133:2181
dubbo.scan.base-packages:com.demo.springboot.service

      3.3) 创建 service 文件夹 , 并创建 UserServiceImpl 文件

package com.demo.springboot.service;
import org.springframework.stereotype.Service;
@Service
@org.apache.dubbo.config.annotation.Service(interfaceClass=UserService.class,version="1.0.1")
public class UserServiceImpl implements UserService{
	@Override
	public String sayHello(String name) {
		// TODO Auto-generated method stub
		return "Hello" + name;
	}	
}

    3.4) 创建  SpringProvider.java  程序入口。

package com.demo.springboot;
import org.apache.dubbo.config.spring.context.annotation.EnableDubboConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubboConfig
public class SpringProvider {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SpringApplication.run(SpringProvider.class, args);
	}
}

4)  创建服务消费者

Spring Boot 整合 Dubbo 和 zookeeper_第3张图片

    4.1) 创建pom文件

    


  4.0.0
  
    com.demo.dubbo
    spring-dubbo-zook
    0.0.1-SNAPSHOT
  
  spring-consumer
  spring-consumer
  spring-consumer
  
  
  		
		    com.demo.dubbo
		    spring-interface
		    0.0.1-SNAPSHOT
	  	
  	
		
		    org.springframework.boot
		    spring-boot-starter
		
		
		
		
		    org.springframework.boot
		    spring-boot-starter-web
		
		
  		
  		
	    
	        org.apache.dubbo
	        dubbo-spring-boot-starter
	     
	    
	    
		
		    org.apache.curator
		    curator-framework		    
		
		
		
		
		    org.apache.curator
		    curator-recipes		    
		 
    
	    
		
		    com.101tec
		    zkclient		    
		    
              
                  log4j
                  log4j
              
              
                  org.slf4j
                  slf4j-log4j12
              
          
		
  

     4.2)创建 application.properties 文件  

server.port=80

dubbo.application.name=spring-consumer
dubbo.registry.address=zookeeper://192.168.190.133:2181

    4.3) 创建controller ,并创建 UserController.java文件

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.demo.springboot.service.UserService;
@RestController
public class UserController {
    
	@Reference(interfaceClass=UserService.class,version="1.0.1")
	private UserService userService;
	
	@GetMapping("/get/{name}")
	public String getUserName(@PathVariable("name") String name) {
		return userService.sayHello(name);
	}
}

    4.4)创建主启动类。SpringConsumer.java

package com.demo.springboot;

import org.apache.dubbo.config.spring.context.annotation.EnableDubboConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableDubboConfig
public class SpringConsumer {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SpringApplication.run(SpringConsumer.class, args);	
	}
}

5) 包结构

Spring Boot 整合 Dubbo 和 zookeeper_第4张图片

 

 

 

你可能感兴趣的:(Spring,Boot,SpringBoot,整合,Dubbo,Zookeeper)