SpringCloud Hoxton 整合spring-cloud-alibaba 使用nacos作为注册中心 dubbo为RPC

第一步IDEA创建一个名字为cloud-mall的maven工程,pom加入如下配置:



    4.0.0
    com.cloud.mall
    cloud-mall
    1.0-SNAPSHOT
    
        cloud-payment-nacos-8002
        cloud-order-nacos-8003
        cloud-base-framework
    
    pom

    
        2.2.2.RELEASE
    

    
        
            
                org.springframework.boot
                spring-boot-dependencies
                2.2.2.RELEASE
                pom
                import
            

            
                org.springframework.cloud
                spring-cloud-dependencies
                Hoxton.SR1
                pom
                import
            

            
                com.alibaba.cloud
                spring-cloud-alibaba-dependencies
                2.1.0.RELEASE
                pom
                import
            

            
                com.cloud.mall
                cloud-base-framework
                1.0-SNAPSHOT
            
        
    

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


第二部创建一个公用基础模块cloud-base-framework

创建两个接口:OrderService和PaymentService

第三步分别创建cloud-payment-nacos-8002(服务提供者)cloud-order-nacos-8003(服务消费者)两个module

cloud-payment-nacos-8002 pom如下:


    
        cloud-mall
        com.cloud.mall
        1.0-SNAPSHOT
    
    4.0.0

    cloud-payment-nacos-8002
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
        
 
        
           com.cloud.mall
            cloud-base-framework
        

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

yml如下:

server:
  port: 8002
spring:
  application:
    name: payment-nacos
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
      

dubbo:
  registry:
    address: spring-cloud://localhost:8848
  protocol:
    name: dubbo
  scan:
    base-packages: com.cloud.mall.service



management:
  endpoints:
    web:
      exposure:
        include: "*"

启动类记得加上@EnableDiscoveryClient、@EnableDubbo

创建一个PaymentServiceImpl

package com.cloud.mall.service;
import com.cloud.mall.service.payment.PaymentService;
import org.apache.dubbo.config.annotation.Service;
@Service
public class PaymentServiceImpl implements PaymentService {
    public String pay() {
        return "---- paying-----";
    }
}

 

cloud-order-nacos-8003 pom如下:



    
        cloud-mall
        com.cloud.mall
        1.0-SNAPSHOT
    
    4.0.0

    cloud-order-nacos-8003
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
        
        
            com.alibaba.cloud
            spring-cloud-starter-dubbo
        


        
            com.cloud.mall
            cloud-base-framework
        
    

yml:

server:
  port: 8003
spring:
  application:
    name: order-nacos
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848


management:
  endpoints:
    web:
      exposure:
        include: "*"
dubbo:
  registry:
    address: spring-cloud://localhost:8848

启动函数加@EnableDiscoveryClient、@EnableDubbo

创建OrderServiceImpl

package com.cloud.mall.service;

import com.cloud.mall.service.order.OrderService;
import com.cloud.mall.service.payment.PaymentService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

/**
 * User: Wang Pengfei
 * Date: 2020/4/3
 * Time: 1:56 下午
 * Description: No Description
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Reference
    private PaymentService paymentService;

    public String getOrder() {
        String pay = paymentService.pay();
        System.out.println(pay);
        return pay;
    }
}

创建OrderController

package com.cloud.mall.controller;

import com.cloud.mall.service.order.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * User: Wang Pengfei
 * Date: 2020/4/3
 * Time: 2:29 下午
 * Description: No Description
 */
@RestController
@RequestMapping("/")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping("")
    public String order(){
        return orderService.getOrder();
    }


}

 

最后分别启动payment和order可以在nacos web界面看到两个服务:

SpringCloud Hoxton 整合spring-cloud-alibaba 使用nacos作为注册中心 dubbo为RPC_第1张图片

最后访问http://localhost:8003

SpringCloud Hoxton 整合spring-cloud-alibaba 使用nacos作为注册中心 dubbo为RPC_第2张图片

至此SpringCloud整合nacos和dubbo完成

你可能感兴趣的:(springcloud,nacos,springcloud,dubbo)