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
<?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
相關文章
- Spring Cloud Alibaba之NacosSpringCloud
- Spring Alibaba Nacos替換dubbo zookeeperSpring
- Spring Cloud Alibaba(2)---Nacos概述SpringCloud
- Spring Cloud Alibaba實戰(七) - Gateway搭配Nacos實現動態路由SpringCloudGateway路由
- Spring Cloud Alibaba生態探索:Dubbo、Nacos及Sentinel的完美結合SpringCloud
- Spring Cloud Alibaba | Nacos配置管理SpringCloud
- Spring Cloud Alibaba(5)---Nacos(配置中心)SpringCloud
- Spring Cloud Alibaba微服務實戰SpringCloud微服務
- spring cloud alibaba springboot nacos 版本對應CloudSpring Boot
- Spring Cloud Alibaba(4)---Nacos(註冊中心)SpringCloud
- Spring Cloud Alibaba 整合 Nacos 實現服務配置中心SpringCloud
- Spring Cloud Alibaba(11)---Sentinel+Nacos持久化SpringCloud持久化
- Spring Cloud Alibaba Nacos 之 灰度釋出(思路分享)SpringCloud
- Spring Cloud Alibaba Nacos異常Java.Net.UnknownHostExceptionSpringCloudJavaException
- 13 Spring Cloud Alibaba簡介和Nacos安裝SpringCloud
- Spring Cloud Alibaba基礎教程:Nacos的叢集部署SpringCloud
- Spring Cloud Alibaba | Nacos服務註冊與發現SpringCloud
- 普通springcloud eureka 和 spring cloud Alibaba nacos 註冊中心SpringGCCloud
- Spring Cloud Alibaba系列之分散式服務元件DubboSpringCloud分散式元件
- Spring Cloud Alibaba 基礎教程:Nacos 生產級版本 0.8.0SpringCloud
- Spring Cloud Alibaba基礎教程:使用Nacos作為配置中心SpringCloud
- Spring Cloud Alibaba基礎教程:Nacos 生產級版本 0.8.0SpringCloud
- Spring Cloud Alibaba系列(二)nacos作為服務配置中心SpringCloud
- Spring Cloud Alibaba基礎教程:Nacos的資料持久化SpringCloud持久化
- Spring Cloud Alibaba 使用Nacos作為服務註冊中心SpringCloud
- Spring Cloud Alibaba 使用Nacos作為配置管理中心SpringCloud
- Spring Boot Dubbo NacosSpring Boot
- Spring Cloud Alibaba基礎教程:Sentinel使用Nacos儲存規則SpringCloud
- Spring Cloud Alibaba:Nacos 作為註冊中心和配置中心使用SpringCloud
- Spring Cloud Alibaba基礎教程:Nacos配置的多環境管理SpringCloud
- Spring Cloud Alibaba(6)---Nacos持久化Mysql8.0版本SpringCloud持久化MySql
- Spring Cloud Alibaba(7)---docker-compose搭建nacos1.4.0叢集SpringCloudDocker
- 專案實戰|千鋒Java微服務架構之Alibaba篇_Spring Cloud AlibabaJava微服務架構SpringCloud
- Spring Cloud Alibaba基礎教程:使用Nacos實現服務註冊與發現SpringCloud
- Spring Cloud Alibaba SentinelSpringCloud
- Spring Cloud Alibaba SeataSpringCloud
- Spring Cloud實戰系列(八) - 微服務監控Spring Boot AdminCloud微服務Spring Boot
- Spring Cloud Alibaba---服務註冊、發現、管理中心NacosSpringCloud