Spring Cloud Alibaba实战(八) - Dubbo + Nacos

目录

(一)Nacos动态配置
(二)Nacos注册中心
(三)Sentinel之限流
(四)Sentinel之熔断
(五)Gateway之路由、限流
(六)Gateway之鉴权、日志
(七)Gateway搭配Nacos实现动态路由
(八)Dubbo + Nacos

正文

通常来说,RPC协议比REST具有更好的性能,服务的内部调用可以考虑使用RPC方式来提高效率。下面来改造account和payment服务,在account中通过dubbo协议来调用payment的查询余额接口,并使用Nacos作为注册中心。

首先创建基于Dubbo的payment服务应用(SpringBoot),即providor端:

pom.xml



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.5.RELEASE
         
    
    com.github.autfish
    payment-dubbo-service
    0.0.1-SNAPSHOT
    payment-dubbo-service
    Demo project for Spring Boot

    
        1.8
        0.9.0.RELEASE
    

    
        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.cloud
            spring-cloud-starter-alibaba-nacos-config
            ${alibaba.version}
        

        
            org.springframework.cloud
            spring-cloud-starter-alibaba-nacos-discovery
            ${alibaba.version}
        

        
            org.springframework.cloud
            spring-cloud-starter-dubbo
            ${alibaba.version}
        

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

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

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

在主类上启用@EnableDiscoveryClient注解

@SpringBootApplication
@EnableDiscoveryClient
public class PaymentDubboServiceApplication {

修改Balance类实现Serializable接口

public class Balance implements Serializable {

    private int id;
    private int diamond;
    private int ticket;
    private String message;

接口声明

public interface BalanceService {

    Balance getBalance(Integer id);
}

接口实现类,注意注解@Service位于dubbo包中而非通常用的Spring注解,并且声明协议为dubbo

import org.apache.dubbo.config.annotation.Service;

import java.util.HashMap;
import java.util.Map;

@Service(protocol = "dubbo")
public class InMemoryBalanceService implements BalanceService {

    final static Map balanceMap = new HashMap() {{
        put(1, new Balance(1, 10, 1000));
        put(2, new Balance(2, 0, 10000));
        put(3, new Balance(3, 100, 0));
    }
    };

    @Override
    public Balance getBalance(Integer id) {
        if(id != null && balanceMap.containsKey(id)) {
            return balanceMap.get(id);
        }
        return new Balance(0, 0, 0, "不存在");
    }
}

在bootstrap.yml中配置Nacos

spring:
  application:
    name: payment-dubbo-service
  main:
    allow-bean-definition-overriding: true
  cloud:
    nacos:
      config:
        server-addr: 127.0.0.1:8848
      discovery:
        enabled: true
        register-enabled: true
        server-addr: 127.0.0.1:8848

在application.yml中配置dubbo

spring:
  profiles:
    active: dev

dubbo:
  scan:
    base-packages: com.github.autfish.dubbo.service
  protocols:
    dubbo:
      name: dubbo
      port: -1
  registry:
    address: spring-cloud://localhost
  application:
    qos:
      enable: false

分别启动Nacos和应用,然后进入Nacos后台可以看到dubbo的声明信息

Spring Cloud Alibaba实战(八) - Dubbo + Nacos_第1张图片

 Spring Cloud Alibaba实战(八) - Dubbo + Nacos_第2张图片

 下面创建account应用。不确定什么原因,使用JAVA 8的dubbo consumer端无法正常启动,这里用JAVA 10创建项目

pom.xml



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.5.RELEASE
         
    
    com.github.autfish
    account-dubbo-service
    0.0.1-SNAPSHOT
    account-dubbo-service
    Demo project for Spring Boot

    
        0.9.0.RELEASE
    

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

        
            org.springframework.boot
            spring-boot-actuator
        

        
            org.springframework.cloud
            spring-cloud-starter-dubbo
            ${alibaba.version}
        

        
            org.springframework.cloud
            spring-cloud-starter-alibaba-nacos-discovery
            ${alibaba.version}
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

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

 同样在主类上启用@EnableDiscoveryClient注解

@SpringBootApplication
@EnableDiscoveryClient
public class AccountDubboServiceApplication {

把实体类Balance.java和接口声明BalanceService.java复制到项目中,并与payment项目中这些类的包名相同

 Spring Cloud Alibaba实战(八) - Dubbo + Nacos_第3张图片

User.java

public class User {

    private int id;
    private String name;
    private Balance balance;

AccountController

import org.apache.dubbo.config.annotation.Reference;

@RestController
public class AccountController {

    final static Map userMap = new HashMap() {{
            put(1, new User(1, "张三"));
            put(2, new User(2, "李四"));
            put(3, new User(3, "王五"));
        }
    };

    @Reference
    private BalanceService balanceService;

    @RequestMapping("/acc/user")
    public User getUser(@RequestParam Integer id) {
        if(id != null && userMap.containsKey(id)) {
            User user = userMap.get(id);
            user.setBalance(balanceService.getBalance(id));
            return user;
        }
        return new User(0, "");
    }
}

bootstrap.yml

spring:
  main:
    allow-bean-definition-overriding: true
  # default disable all
  cloud:
    nacos:
      discovery:
        enabled: true
        register-enabled: true
        server-addr: 127.0.0.1:8848
    zookeeper:
      enabled: false
    consul:
      enabled: false

eureka:
  client:
    enabled: false

ribbon:
  nacos:
    enabled: false

 application.yml

spring:
  application:
    name: account-dubbo-service
dubbo:
  registry:
    address: spring-cloud://localhost
  cloud:
    subscribed-services: ${provider.application.name}
  application:
    qos:
      enable: false
server:
  port: 8085

provider:
  application:
    name: payment-dubbo-service

启动应用并测试

本期源码

链接:https://pan.baidu.com/s/1AMOhmtx9M83NERX9PbmCIA 
提取码:f96r 

你可能感兴趣的:(Spring,Cloud,Alibaba,Dubbo,Nacos,Spring,Cloud)