实现 Service2 | 学习笔记

简介: 快速学习实现 Service2。

开发者学堂课程【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 之间也是要进行远程调用

 

图片.png

 

 

一、定义 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 confignacos 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

namespacec67e497-a698-4d6d-9bb1-cfac5f5b51c4 #开发环境

groupNACOS_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:

checkfalse  

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里的字符串。

图片.png

相关文章
|
5月前
|
XML 数据库 Android开发
Service介绍
Service介绍
46 0
|
10月前
|
Kubernetes 负载均衡 容器
k8s(8)Service(服务)
Service(服务)
65 0
|
10月前
|
Kubernetes 网络协议 Cloud Native
Service 基础
Service 基础
|
API 开发工具 Android开发
Service基础
Service基础
84 0
Service基础
|
Kubernetes 负载均衡 网络协议
k8s service 总结
k8s service 总结
302 0
k8s service 总结
|
XML 运维 Dubbo
实现 Service1 | 学习笔记
快速学习实现 Service1.
157 0
实现 Service1 | 学习笔记
|
API 调度
从Service到WorkManager
关于Service,想必大家都太熟悉了,今天我们就再回顾下它的使用、概念、区别、变更历史等等。
473 0
从Service到WorkManager
|
Kubernetes 负载均衡 网络协议
k8s service
Kubernetes Service 定义了这样一种抽象:一个 Pod 的逻辑分组,一种可以访问它们的策略——通常称为微服务。这一组 Pod 能够被 Service 访问到,通常是通过 Label Selector 实现的。
7075 0
|
监控 关系型数据库 Unix
|
监控 Unix 关系型数据库