Spring Cloud Alibaba實戰(八) - Dubbo + Nacos

大雨將至發表於2019-06-12

目錄

(一)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

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.github.autfish</groupId>
    <artifactId>payment-dubbo-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>payment-dubbo-service</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <alibaba.version>0.9.0.RELEASE</alibaba.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            <version>${alibaba.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>${alibaba.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-dubbo</artifactId>
            <version>${alibaba.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

在主類上啟用@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<Integer, Balance> 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的宣告資訊

 

 下面建立account應用。不確定什麼原因,使用JAVA 8的dubbo consumer端無法正常啟動,這裡用JAVA 10建立專案

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.github.autfish</groupId>
    <artifactId>account-dubbo-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>account-dubbo-service</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <alibaba.version>0.9.0.RELEASE</alibaba.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-dubbo</artifactId>
            <version>${alibaba.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>${alibaba.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

 同樣在主類上啟用@EnableDiscoveryClient註解

@SpringBootApplication
@EnableDiscoveryClient
public class AccountDubboServiceApplication {

把實體類Balance.java和介面宣告BalanceService.java複製到專案中,並與payment專案中這些類的包名相同

 

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<Integer, User> 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 

相關文章