共计 6169 个字符,预计需要花费 16 分钟才能阅读完成。
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 工程
<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,添加依赖
<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、声明接口
public interface IHelloService {String sayHello(String name);
}
5、安装至本地仓库
6、添加服务提供者工程,添加依赖
<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
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、添加服务类
@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、添加消费者工程,添加依赖
<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
spring.application.name=spring-cloud-dubbo-consumer
spring.cloud.zookeeper.discovery.register=false
spring.cloud.zookeeper.connect-string=localhost:2181
server.port=8097
3、添加启动类
@SpringBootApplication
public class SpirngCloudDubboApplication {public static void main(String[] args) {SpringApplication.run(SpirngCloudDubboApplication.class,args);
}
}
4、添加测试 Controller
@RestController
public class HelloController {@Reference
private IHelloService helloService;
@GetMapping("/hello")
public String hello(){return helloService.sayHello("sss");
}
}
正文完
星哥玩云-微信公众号