上一篇,SpringCloudAlibaba篇(三)整合Sentinel(限流、流量整形、熔断降级、系统负载保护、热点防护,分布式服务架构的高可用流量防护组件)
Apache Dubbo (incubating) |ˈdʌbəʊ| 是一款高性能、轻量级的开源 Java RPC 分布式服务框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。她最大的特点是按照分层的方式来架构,使用这种方式可以使各个层之间解耦合(或者最大限度地松耦合)。从服务模型的角度来看,Dubbo 采用的是一种非常简单的模型,要么是提供方提供服务,要么是消费方消费服务,所以基于这一点可以抽象出服务提供方(Provider)和服务消费方(Consumer)两个角色。
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-dubboartifactId>
<version>${spring-cloud-alibaba.version}version>
dependency>
dubbo:
application:
qos-enable: false
qos-accept-foreign-ip: false
registry:
address: spring-cloud://localhost
consumer:
# check为false,则关闭该Consumer下所有服务检查
check: false
dubbo:
protocol:
# dubbo 协议
name: dubbo
# dubbo 协议端口( -1 表示自增端口,从 20880 开始)
port: 20881
cloud:
# producer 服务名 , "" 为不加载,多个服务名用逗号分隔
subscribed-services: ""
spring.main.allow-circular-references=true
pom
这里的service-starter-parent 是我在SpringCloudAlibaba篇(二)自定义的,可以去看一下前面的文章
<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">
<parent>
<artifactId>SpringCloudAlibaba2022artifactId>
<groupId>top.fategroupId>
<version>1.0.0version>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>order-serviceartifactId>
<name>${project.artifactId}name>
<version>${fate.project.version}version>
<packaging>jarpackaging>
<dependencies>
<dependency>
<groupId>top.fategroupId>
<artifactId>service-starter-parentartifactId>
<version>1.0.0version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-antrun-pluginartifactId>
<executions>
<execution>
<phase>packagephase>
execution>
executions>
plugin>
plugins>
build>
project>
@SpringBootApplication
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
}
url:
nacos: localhost:8848
spring:
application:
name: order-service #实例名
profiles:
active: dev
cloud:
nacos:
discovery:
#集群环境隔离
cluster-name: shanghai
#命名空间
namespace: ${spring.profiles.active}
#持久化实例 ture为临时实例 false为持久化实例 临时实例发生异常直接剔除, 而持久化实例等待恢复
ephemeral: true
#注册中心地址
server-addr: ${url.nacos}
config:
namespace: ${spring.profiles.active}
file-extension: yaml
#配置中心地址
server-addr: ${url.nacos}
extension-configs[0]:
data-id: mysql-user.yaml
group: DEFAULT_GROUP
refresh: false
extension-configs[1]:
data-id: sentinel.yaml
group: DEFAULT_GROUP
refresh: false
extension-configs[2]:
data-id: dubbo.yaml
group: DEFAULT_GROUP
refresh: false
dubbo:
protocol:
# dubbo 协议
name: dubbo
# dubbo 协议端口( -1 表示自增端口,从 20880 开始)
port: 20882
cloud:
subscribed-services: user-service
spring.main.allow-circular-references=true
server:
port: 8082
<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">
<parent>
<artifactId>SpringCloudAlibaba2022artifactId>
<groupId>top.fategroupId>
<version>1.0.0version>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>service-apiartifactId>
<name>${project.artifactId}name>
<version>${fate.project.version}version>
<packaging>pompackaging>
<description>SpringCloudAlibaba 微服务API集合description>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<configuration>
<skip>trueskip>
<finalName>${project.name}finalName>
configuration>
plugin>
plugins>
build>
project>
<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">
<parent>
<artifactId>service-apiartifactId>
<groupId>top.fategroupId>
<version>1.0.0version>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>user-service-apiartifactId>
<name>${project.artifactId}name>
<version>${fate.project.version}version>
<packaging>jarpackaging>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
properties>
project>
public interface UserService {
String getServiceName();
}
<dependency>
<groupId>top.fategroupId>
<artifactId>user-service-apiartifactId>
<version>1.0.0version>
dependency>
@DubboService
public class UserServiceImpl implements UserService {
@Override
public String getServiceName() {
return "this is UserService";
}
}
@EnableDubbo(scanBasePackages = "top.fate.service")
<dependency>
<groupId>top.fategroupId>
<artifactId>user-service-apiartifactId>
<version>1.0.0version>
dependency>
@RestController
@RequestMapping(value = "order")
public class OrderController {
@DubboReference
private UserService userService;
@GetMapping("getUserService")
public String getUserService(){
return userService.getServiceName();
}
}
访问http://localhost:8082/order/getUserService