开发者学堂课程【Spring Cloud Alibaba Nacos 详解(下):实现 Service2】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/725/detail/12944
实现 service2
内容介绍:
一、 定义 service-2-api
二、 实现 service-2-server
三、 实现 service1 调用 service2
在整个系统架构中,除了 application 调用 service 之外,service 与 service 之间也是要进行远程调用
一、定义 service-2-api
定义service-2-api工程,pom.xml如下:
<parent>
<artifactId>Service2</artifactId>
<groupId>com.itheima.nacos</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.o.o</ modelVersion>
<artifactId>service-2-api</ artifactId>I
并定义服务接口,Dubbo 服务接口是服务提供方与消费方的远程通讯契约,通常由普通的 Java 接口( interface )来声明,
如 P roviderService 接口:
package com.itheima.microservice.service2.api;
public interface ProviderService {
String service();
}
二、实现 service-2-server
1.初始化 service-2-server Maven 工程
首先,创建 artfactId 名为 service-2-server 的 Maven 工程,并在其 pom.xml 文件中增添 Dubbo Spring Cloud 必要的依赖:
dependencies>
<dependency>
<groupId>com. ithe ima . nacos</ groupId>
<artifactId>service- 2- api</artifactId>
<vers ion>1.0- SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com. alibaba . cloud</groupId>
<artifactId>spring- cloud- starter alibaba- nacos config</
artifactId>
</dependency>
<dependency >
<groupId>com . alibaba . clouds/groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</ artifactId>
</dependency>
<dependency>
在引入 nacos config 和 nacos discovery 之后,我们就可以写实现的接口。
2.实现 Dubbo 服务
ProviderService 作为暴露的 Dubbo 服务接口,服务提供方 service-2-server 需要将其实现:
package com.Itheima.microservice.service2.servic;
@org.apache.ubbo.config.annotation.Service
public class ProviderServiceImpl implements ProviderService {
@Override
public String service( ) {
return "Provider invoke";
}
}
其中,@org.apache.dubbo.config.annotation.Service 是 Dubbo 服务注解,仅声明该 java 服务(本地)实现为 Dubbo 服务。因此, 下一步需要将其配置 Dubbo 服务(远程)。
3.配置 Dubbo 服务
在暴露 Dubbo 服务方面,推荐开发人员外部化配置的方式,即指定 Java 服务实现类的扫描基准包。
Dubbo Spring Cloud 继承了 Dubbo Spring Boot 的外部化配置特性,也可以通过标注
@DubboComponentScan 来实现基准包扫描。
同时, Dubbo 远程服务需要暴露网络端口, 并设定通讯协议,完整的 YAML 配置如下所示:
server:
port: ${port : 56040} #
启动端口 命令行注入
spring:
application:
name: service2
main:
allow-bean-definition-overriding: true # Spring Boot 2.1
需要设定
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
namespace: c67e4a97-a698-4d6d-9bb1-cfac5f5b51c4
cluster-name: DEFAULT
config:
server-addr: 127.0.0.1:8848 #
配置中心地址
file-extension: yaml
namespace
:c67e497-a698-4d6d-9bb1-cfac5f5b51c4 #开发环境
group
:NACOS_MICROSERVICE_GROUP # xx业务组
dubbo:
scan :
# dubbo
服务扫描基准包
base-packages : com.itheima.microservice
protocol:
# dubbo
协议
name: dubbo
# dubbo
协议端口(-1表示自增端口,从20880开始)
port: ${dubbo_port : 20891}
registry:
address: nacos
://127.0.0.1:8848
application:
qos-enable: false
consumer:
check
:false
4.启动服务提供方应用
Dubbo Spring Cloud 引导类与普通 Spring Cloud 应用并无差别,如下所示:
@SpringBootApplication
@EnableDiscoveryClient
public class Service2Bootstrap {
public static void main(String[ ] args) {
SpringApplication.run(Service2Bootstrap. class, args);
}
}
在引导 Service2Bootstrap 之前,请提前启动 Nacos 服务器。当 Service2Bootstrap 启动后,应用 service将出现在 Nacos 控制台界面。
启动之后,可以观察 nacos 服务列表 Dev,点 service2 后的详情,端口为20891。至此,service2 实现完成
三、实现 service1调用 service2
1.引用 service2
在 service2 中添加 service1 的依赖:
<dependency>
<groupId>com.itheima.nacos</groupId>
<artifactId>service-2-api</artifactId>
<version>1.0- SNAPSHOT</version>
</dependency>
2.实现远程调用
@org.apache.dubbo.config.annotation.Service
public class ConsumerServiceImpl implements Consumer
Service {
@Reference
ProviderService providerService;
public String service() {
return "Consumer invoke |” +providerService.service();
将 service1重启,service1 重启刷新之后,最终返回的内容应该包括 service2里的字符串。