Springboot SpringCloudAlibaba Nacos 项目搭建

依赖版本:

spring-boot:2.3.12.RELEASE

spring-cloud-alibaba:2.2.7.RELEASE

spring-cloud:Hoxton.SR12

nacos:2.0.3

1.部署搭建Nacos注册中心

Linux Nacos 快速启动_nacos linux快速启动-CSDN博客

2.构建项目

源码地址

2.1 项目目录

Springboot SpringCloudAlibaba Nacos 项目搭建_第1张图片

2.2 根pom.xml



    4.0.0

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

    com.ocean
    dubbo-cloud
    pom
    1.0-SNAPSHOT

    
        dubbo-order
        dubbo-user
    

    
        UTF-8
        UTF-8
        1.8
        Hoxton.SR12
        2.2.7.RELEASE
    

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

    
        
            org.springframework.boot
            spring-boot-starter-actuator
            
                
                    org.springframework.boot
                    spring-boot-starter
                
            
        

        
        
            com.alibaba.cloud
            spring-cloud-starter-dubbo
            
                
                    spring-context-support
                    com.alibaba.spring
                
            
        

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

        
            org.projectlombok
            lombok
            true
        

        
            com.alibaba.spring
            spring-context-support
            1.0.11
        
    

2.3 服务提供者

2.3.1 目录结构

Springboot SpringCloudAlibaba Nacos 项目搭建_第2张图片

2.3.2 pom.xml




    
        dubbo-cloud
        com.ocean
        1.0-SNAPSHOT
    
    4.0.0

    dubbo-user
    pom

    
        dubbo-user-api
        dubbo-user-provider
    

    
        8
        8
    







    
        dubbo-user
        com.ocean
        1.0-SNAPSHOT
    
    4.0.0
    dubbo-user-api

    
        UTF-8
        UTF-8
        1.8
    

    
        
            com.ocean
            dubbo-user-provider
            1.0-SNAPSHOT
        
    

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








    
        dubbo-user
        com.ocean
        1.0-SNAPSHOT
    

    4.0.0
    jar
    dubbo-user-provider

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        
    

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

2.3.3 dubbo-user-provideer代码

目录

Springboot SpringCloudAlibaba Nacos 项目搭建_第3张图片

代码

UserProviderService.java

package com.ocean.provider;

public interface UserProviderService {

    String getUserName();

}

2.3.4 dubbo-user-api代码

目录

Springboot SpringCloudAlibaba Nacos 项目搭建_第4张图片

代码

UserApi.java

package com.ocean.controller;

import com.ocean.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class UserApi {

    @Resource
    private UserService userService;

    @GetMapping(value = "/api/user/getName")
    public String getOrderInfo() {
        return userService.getName();
    }

}

UserProviderServiceImpl.java 

package com.ocean.provider;

import com.ocean.service.UserService;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;

@DubboService
public class UserProviderServiceImpl implements UserProviderService {

    @Resource
    private UserService userService;

    @Override
    public String getUserName() {
        return userService.getName();
    }
}

UserService.java 

package com.ocean.service;

import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class UserService {

    public String getName() {
        return UUID.randomUUID().toString();
    }

}

UserApplication.java 

package com.ocean;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication
public class UserApplication {

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

}

application.yml 

server:
  port: 6668

spring:
  application:
    name: user
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
  main:
    allow-bean-definition-overriding: true

dubbo:
  application:
    qos-enable: false
    id: user
  scan:
    base-packages: com.ocean.provider
  protocols:
    dubbo:
      name: dubbo
      port: -1
  registry:
    address: spring-cloud://127.0.0.1

2.4 服务消费者

2.4.1 目录结构

Springboot SpringCloudAlibaba Nacos 项目搭建_第5张图片

2.4.2 pom.xml




    
        dubbo-cloud
        com.ocean
        1.0-SNAPSHOT
    
    4.0.0

    dubbo-order
    pom

    
        dubbo-order-api
    

    
        8
        8
    






    
        dubbo-order
        com.ocean
        1.0-SNAPSHOT
    
    4.0.0
    dubbo-order-api

    
        UTF-8
        UTF-8
        1.8
    

    
        
            com.ocean
            dubbo-user-provider
            1.0-SNAPSHOT
        
    

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

2.4.3 代码

OrderApi.java

package com.ocean.controller;

import com.ocean.service.OrderService;
import com.ocean.vo.OrderVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class OrderApi {

    @Resource
    private OrderService orderService;

    @GetMapping(value = "/api/order/{id}")
    public OrderVO getOrderInfo(@PathVariable("id") String id) {
        return orderService.getOrderInfo(id);
    }

}

 ​​​​​​​OrderService.java  

package com.ocean.service;

import com.ocean.provider.UserProviderService;
import com.ocean.vo.OrderVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

@Service
public class OrderService {

    @DubboReference
    private UserProviderService userProviderService;

    public OrderVO getOrderInfo(String id) {
        return new OrderVO(id, userProviderService.getUserName());
    }

}

​​​​​​​OrderVO.java 

package com.ocean.vo;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@ToString
@Getter
@Setter
public class OrderVO {

    private String orderId;

    private String userName;


    public OrderVO(String orderId, String userName) {
        this.orderId = orderId;
        this.userName = userName;
    }
}

OrderApplication.java 

package com.ocean;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication
public class OrderApplication {

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

}

application.yml  

server:
  port: 6667

spring:
  application:
    name: order
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
  main:
    allow-bean-definition-overriding: true

dubbo:
  cloud:
    subscribed-services: user
  application:
    qos-enable: false
    id: order
  scan:
    base-packages: com.ocean.provider
  protocols:
    dubbo:
      name: dubbo
      port: -1
  registry:
    address: spring-cloud://127.0.0.1

https://download.csdn.net/download/qq_34253002/88522267?spm=1001.2014.3001.5501 

你可能感兴趣的:(Java,spring,boot,java,dubbo)