Spring Boot集成Dubbo应用(1):Dubbo+Zk简单的入门案例

开始

下面是该demo所涉及到所有的工程,再此之前,记得打开Zookeeper
Spring Boot集成Dubbo应用(1):Dubbo+Zk简单的入门案例_第1张图片

接口工程/api

该工厂只提供一个接口,但并没有实现

Spring Boot集成Dubbo应用(1):Dubbo+Zk简单的入门案例_第2张图片

就这样,over,然后 执行命令 mvn clean install 打包到本地库,好给别的工程调用,真实开发中,都是通过mvn deploy 命令,传到私库(nexus)中,供团队统一调用的

配置父类/parent

pom文件配置

 
        org.springframework.boot
        spring-boot-starter-parent
        2.1.7.RELEASE
         
    
    com.sunfounder
    dubbo-service-user-parent
    1.0.0
    dubbo-service-user-parent
    
        1.8
        2.7.1
        2.1.7.RELEASE
    
    
    
            
            
                org.springframework.boot
                spring-boot-dependencies
                ${spring-boot.version}
                pom
                import
            

            
            
                org.apache.dubbo
                dubbo-dependencies-bom
                ${dubbo.version}
                pom
                import
            

            
                org.apache.dubbo
                dubbo
                ${dubbo.version}
                
                    
                        org.springframework
                        spring
                    
                    
                        javax.servlet
                        servlet-api
                    
                    
                        log4j
                        log4j
                    
                
            
        
    

    
        
            org.springframework.boot
            spring-boot-starter
        

        
        
            com.sunfounder
            dubbo-service-user-api
            1.0.0
        
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        

        
        
            com.alibaba.boot
            dubbo-spring-boot-starter
            0.2.0
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


就这样,over,什么都不要动,后面要用这个工程做父类的,需要先删除test包下的测试启动类,打包项目mvn clean install

服务提供商工程/provider

pom.xml配置

复制粘贴就好了下面的配置,服务提供商继承父工程的依赖配置就够用了

<parent>
        <groupId>com.sunfoundergroupId>
        <artifactId>dubbo-service-user-parentartifactId>
        <version>1.0.0version>
        <relativePath/> 
parent>

yml文件配置

spring:
  application:
    name: dubbo-service-user-provider

user:
  service:
    version: 1.0.0

dubbo:
  scan:
  	# 请使用basePackage
    basePackages: com.sunfounder.dubbo.service.user.provider.api
  application:
    id: dubbo-service-user-provider
    name: dubbo-service-user-provider
  registry:
    address: zookeeper://47.9*.*.79:2181 # Zookeeper地址,这是单应用,非集群,修改到你的zk地址
    id: zookeeper # 选择注册中心
  protocol:
    name: dubbo
    port: 12345
    id: dubbo

代码实现

业务实现类

import com.sunfounder.ezblockstudio.service.UserService;
import org.apache.dubbo.config.annotation.Service;

/**
 * @auther lucas
 * @Description 接口实现类
 * @Date 2019/8/8
 **/
@Service(version="${user.service.version}")
public class UserServiceImpl implements UserService {
	
	@Value("${dubbo.application.name}")
    private String serviceName;
    
    @Override
    public String sayHi() {
        return String.format("[%s] : 你好 / Hello", serviceName);
    }
}

启动类

import com.alibaba.dubbo.container.Main;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DubboServiceUserProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(DubboServiceUserProviderApplication.class, args);
        Main.main(args);
    }
}

服务消费者 / consumer

pom配置

这其实是个web工程,需要引入web依赖,对外提供接口

<parent>
        <groupId>com.sunfoundergroupId>
        <artifactId>dubbo-service-user-parentartifactId>
        <version>1.0.0version>
        <relativePath/> 
parent>

<dependencies>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
dependencies>

yml配置文件

spring:
  application:
    name: dubbo-service-user-consumer
user:
  service:
    version: 1.0.0
dubbo:
  scan:
    basPackages: com.sunfounder.dubbo.service.user.consumer.controller
  application:
    id: dubbo-service-user-consumer
    name: dubbo-service-user-consumer
  protocol:
    id: dubbo
    name: dubbo
    port: 12345
  registry:
    id: zookeeper # 选择注册中心
    address: zookeeper://47.92.9*.*:2181 # Zookeeper地址,这是单应用,非集群
server:
  port: 9090

代码实现

启动类不用修改,主要看controller包下类的实现

Spring Boot集成Dubbo应用(1):Dubbo+Zk简单的入门案例_第3张图片

控制层代码

import com.alibaba.dubbo.config.annotation.Reference;
import com.sunfounder.dubbo.service.user.api.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @auther lucas
 * @Description
 * @Date 2019/8/9
 **/
@RestController
public class UserController {

    @Reference(version = "1.0.0")
    private UserService service;

    @GetMapping(value = "hi")
    public String sayHi(){
        return service.sayHi();
    }
}

运行项目

先运行服务提供者,然后启动服务消费者,访问服务消费者对外提供的接口

Spring Boot集成Dubbo应用(1):Dubbo+Zk简单的入门案例_第4张图片
下一篇 : 熔断器Hystrix

参考文献

Spring Boot for Dubbo

你可能感兴趣的:(微服务)