1、Dubbo Spring Cloud 概述
Spring Cloud 为Java环境中解决微服务问题提供了非常完整的方案,所以在最近几年时间,Spring Cloud成了很多公司首选的技术方案。但是随着运用规模的扩大,Spring Cloud在服务治理领域局限性逐步显露出来。相对来说,在服务治理方面,Apache Dubbo有着非常大的优势,并且在Spring Cloud出现之前,它就已经被很多公司作为服务治理及微服务基础设施的首选框架。
Dubbo Spring Cloud 的出现,使Dubbo既能够完全整合到Spring Cloud的技术栈中,享受Spring Cloud生态中的技术支持和标准化输出,又能够弥补Spring Cloud中服务治理这方面的短板。
2、使用Dubbo Spring Cloud实现服务提供者和服务消费者
2.1、实现Dubbo服务提供方
步骤:
1、添加一个普通Maven工程
代码语言:javascript复制<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.11.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.1.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
2、创建两个模块:spring-cloud-dubbo-api(普通工程)和spring-cloud-dubbo-provider(spring cloud工程)
3、在spring-cloud-dubbo-api,添加依赖
代码语言:javascript复制 <parent>
<artifactId>spring-dubbo</artifactId>
<groupId>com.tyschool</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-dubbo-api</artifactId>
<properties>
<java.version>1.8</java.version>
</properties>
4、声明接口
代码语言:javascript复制public interface IHelloService {
String sayHello(String name);
}
5、安装至本地仓库
6、添加服务提供者工程,添加依赖
代码语言:javascript复制 <parent>
<groupId>com.tyschool</groupId>
<artifactId>spring-dubbo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
<dependency>
<groupId>com.tyschool</groupId>
<artifactId>sample-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
7、修改application.properties
代码语言:javascript复制spring.application.name=springclouddubboprovider
dubbo.protocol.id=dubbo
# Dubbo 服务暴露的协议配置,其中子属性 name 为协议名称,port 为协议端口( -1 表示自增端口,从 20880 开始)
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
spring.cloud.zookeeper.discovery.register=true
spring.cloud.zookeeper.connect-string=localhost:2181
8、添加服务类
代码语言:javascript复制@Service
public class HelloServiceImpl implements IHelloService {
@Value("${spring.application.name}")
private String serviceName;
@Override
public String sayHello(String s) {
return serviceName;
}
}
2.2 实现Dubbo服务消费者
步骤:
1、添加消费者工程,添加依赖
代码语言:javascript复制 <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>com.tyschool</groupId>
<artifactId>sample-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2、添加application.properties
代码语言:javascript复制spring.application.name=spring-cloud-dubbo-consumer
spring.cloud.zookeeper.discovery.register=false
spring.cloud.zookeeper.connect-string=localhost:2181
server.port=8097
3、添加启动类
代码语言:javascript复制@SpringBootApplication
public class SpirngCloudDubboApplication {
public static void main(String[] args) {
SpringApplication.run(SpirngCloudDubboApplication.class,args);
}
}
4、添加测试Controller
代码语言:javascript复制@RestController
public class HelloController {
@Reference
private IHelloService helloService;
@GetMapping("/hello")
public String hello(){
return helloService.sayHello("sss");
}
}