目录

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

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐