栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

Dubbo + SpringBoot + Nacos 部署服务

Dubbo + SpringBoot + Nacos 部署服务

目录

服务发现示意图

版本信息

项目结构

spring-boot-dubbo-base

pom.xml

User

UserService

spring-boot-dubbo-consumer

pom.xml

application.yml

SpringBootDubboConsumerApplication

userController

spring-boot-dubbo-provider

pom.xml

UserServiceImpl

SpringBootDubboProviderApplication

application.yml

测试


服务发现示意图

不恰当的比喻Provider(购房者)、Container(中介手机号)、Registry(中介)、Consumer(卖房者)、Monitor(房产局)

Container启动,Provider 注册地址到注册中心,Consumer 从注册中心异步读取和订阅 Provider 地址列表,当Provider服务下单或宕机时,Registry也会异步通知Consumer,当Consumer消费服务时,使用Registry订阅的地址访问访问Provider,Consumer、Provider同时会向Monitor异步传输调用信息。

版本信息
  • springboot  2.5.4
  • maven 3.3.9
  • nacos 2.0.3
  • dubbo 2.7.0

项目结构
|-- spring-boot-dubbo (父级工程)
    |-- spring-boot-dubbo-base (基础工程)
    |-- spring-boot-dubbo-consumer (消费者)
    |-- spring-boot-dubbo-provider (生产者)

spring-boot-dubbo-base

pom.xml


    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.5.4
         
    
    com.example
    spring-boot-dubbo-base
    0.0.1-SNAPSHOT
    spring-boot-dubbo-base
    Demo project for Spring Boot
    
        1.8
        2020.0.3
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.projectlombok
            lombok
            true
        

    
    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

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


User

User由于需要网络传输,必须实现序列化

package com.example.base.model;

import lombok.Builder;
import lombok.Data;
import java.io.Serializable;

@Data
@Builder
public class User implements Serializable {
    private Integer id;
    private String name;
}

UserService
package com.example.base.service;

import com.example.base.model.User;

public interface UserService {

    //查询用户
    User findById(Integer id);
}

spring-boot-dubbo-consumer

pom.xml


    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.5.4
         
    
    com.example
    spring-boot-dubbo-consumer
    0.0.1-SNAPSHOT
    spring-boot-dubbo-consumer
    Demo project for Spring Boot
    
        1.8
        2020.0.3
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.projectlombok
            lombok
            true
        

        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
            2.1.1.RELEASE
        


        
        
            com.example
            spring-boot-dubbo-base
            0.0.1-SNAPSHOT
        

        
            org.apache.dubbo
            dubbo-spring-boot-starter
            2.7.5
            
                
                    org.apache.dubbo
                    dubbo
                
            
        

        
            org.apache.dubbo
            dubbo
            2.7.5
        


    
    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

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


application.yml
server:
  port: 9012
dubbo:
  # 配置服务信息
  application:
    name: dubbo-consumer
    # 禁用QOS同一台机器可能会有端口冲突现象
    qos-enable: false
    qos-accept-foreign-ip: false
  # 配置注册中心
  registry:
    address: nacos://192.168.14.1:8848

SpringBootDubboConsumerApplication

@EnableDubbo不能少

package com.example.consumer;

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

@SpringBootApplication
@EnableDubbo //开启dubbo的注解支持
public class SpringBootDubboConsumerApplication {

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

}

userController
  • 如果提供服务者@Service拥有版本号,@Reference也必须拥有版本号,用于解决server多实现。
  • check=false可以不用验证生产者是否可用,如果使用默认true,如果生产者不可用,dubbo会阻止springboot启动
package com.example.consumer.controller;

import com.example.base.model.User;
import com.example.base.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping
public class UserController {

//    @Reference(version = "1.0.0",group = "UserGroup",interfaceClass = UserService.class)
    @Reference(check=false)
    UserService userService;

    @GetMapping("/user")
    public User getUser(){
        User user = userService.findById(1);
        return user;
    }
}

spring-boot-dubbo-provider

pom.xml


    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.5.4
         
    
    com.example
    spring-boot-dubbo-provider
    0.0.1-SNAPSHOT
    spring-boot-dubbo-provider
    Demo project for Spring Boot
    
        1.8
        2020.0.3
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
            2.1.1.RELEASE
        

        
            org.projectlombok
            lombok
            true
        

        
        
            com.example
            spring-boot-dubbo-base
            0.0.1-SNAPSHOT
        

        
        
            org.apache.dubbo
            dubbo-spring-boot-starter
            2.7.5
            
                
                    org.apache.dubbo
                    dubbo
                
            
        

        
            org.apache.dubbo
            dubbo
            2.7.5
        


    
    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

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


UserServiceImpl

注:此处的@Service是dubbo的,不能选错

package com.example.provider.serviceImpl;

import com.example.base.model.User;
import com.example.base.service.UserService;
import org.apache.dubbo.config.annotation.Service;

//@Service(version = "1.0.0",group = "UserGroup",interfaceClass = UserService.class)
@Service
public class UserServiceImpl implements UserService {

    @Override
    public User findById(Integer id) {
        User user = User.builder()
                .id(1)
                .name("向先生")
                .build();
        return user;
    }

}

SpringBootDubboProviderApplication

注:不能少@EnableDubbo //开启Dubbo的注解支持

package com.example.provider;

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

@EnableDubbo //开启Dubbo的注解支持
@SpringBootApplication
public class SpringBootDubboProviderApplication {

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

}

application.yml
server:
  port: 9011
dubbo:
  # 配置服务信息
  application:
    name: dubbo-consumer
    # 禁用QOS同一台机器可能会有端口冲突现象
    qos-enable: false
    qos-accept-foreign-ip: false
  # 配置注册中心
  registry:
    address: nacos://192.168.14.1:8848

测试

 http://localhost:9012/user

结果

{"id":1,"name":"向先生"}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/279828.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号