SpringCloud(三)服务提供与调用

embedded/2024/9/23 17:30:24/

## 服务提供

我们假设服务提供者有一个hello方法,可以根据传入的参数,提供输出“hello xxx,this is first messge”的服务
 

1、pom包配置

创建一个springboot项目,pom.xml中添加如下配置:

<dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency>
</dependencies>


### 2、配置文件

application.properties配置如下:

spring.application.name=spring-cloud-producer
server.port=9000
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

参数在上一篇都已经解释过,这里不多说。
 

3、启动类

启动类中添加@EnableDiscoveryClient注解

@SpringBootApplication
@EnableDiscoveryClient
public class ProducerApplication {public static void main(String[] args) {SpringApplication.run(ProducerApplication.class, args);}
}


### 4、controller

提供hello服务

@RestController
public class HelloController {@RequestMapping("/hello")public String index(@RequestParam String name) {return "hello "+name+",this is first messge";}
}

添加@EnableDiscoveryClient注解后,项目就具有了服务注册的功能。启动工程后,就可以在注册中心的页面看到SPRING-CLOUD-PRODUCER服务。

到此服务提供者配置就完成了。
 

服务调用

1、pom包配置

和服务提供者一致

<dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency>
</dependencies>


### 2、配置文件

application.properties配置如下:

spring.application.name=spring-cloud-consumer
server.port=9001
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/


### 3、启动类

启动类添加@EnableDiscoveryClient@EnableFeignClients注解。

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ConsumerApplication {public static void main(String[] args) {SpringApplication.run(ConsumerApplication.class, args);}}
  • @EnableDiscoveryClient :启用服务注册与发现
  • @EnableFeignClients:启用feign进行远程调用

Feign是一个声明式Web Service客户端。使用Feign能让编写Web Service客户端更加简单, 它的使用方法是定义一个接口,然后在上面添加注解,同时也支持JAX-RS标准的注解。Feign也支持可拔插式的编码器和解码器。Spring Cloud对Feign进行了封装,使其支持了Spring MVC标准注解和HttpMessageConverters。Feign可以与Eureka和Ribbon组合使用以支持负载均衡。
 

4、feign调用实现

@FeignClient(name= "spring-cloud-producer")
public interface HelloRemote {@RequestMapping(value = "/hello")public String hello(@RequestParam(value = "name") String name);
}
  • name:远程服务名,及spring.application.name配置的名称

此类中的方法和远程服务中contoller中的方法名和参数需保持一致。


### 5、web层调用远程服务

将HelloRemote注入到controller层,像普通方法一样去调用即可。

@RestController
public class ConsumerController {@AutowiredHelloRemote HelloRemote;@RequestMapping("/hello/{name}")public String index(@PathVariable("name") String name) {return HelloRemote.hello(name);}}

到此,最简单的一个服务注册与调用的例子就完成了。


## 测试

简单调用

依次启动spring-cloud-eureka、spring-cloud-producer、spring-cloud-consumer三个项目

先输入:http://localhost:9000/hello?name=neo 检查spring-cloud-producer服务是否正常

返回:hello neo,this is first messge

说明spring-cloud-producer正常启动,提供的服务也正常。

浏览器中输入:http://localhost:9001/hello/neo

返回:hello neo,this is first messge

说明客户端已经成功的通过feign调用了远程服务hello,并且将结果返回到了浏览器。
 

负载均衡

以上面spring-cloud-producer为例子修改,将其中的controller改动如下:

@RestController
public class HelloController {@RequestMapping("/hello")public String index(@RequestParam String name) {return "hello "+name+",this is producer 2  send first messge";}
}

在配置文件中改动端口:

spring.application.name=spring-cloud-producer
server.port=9003
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

打包启动后,在eureka就会发现两个服务提供者,如下图:

然后在浏览器再次输入:http://localhost:9001/hello/neo 进行测试:

第一次返回结果:hello neo,this is first messge

第二次返回结果:hello neo,this is producer 2 send first messge


http://www.ppmy.cn/embedded/110917.html

相关文章

1.SpringCloud与SpringCloud Alibaba

SpringCloud与SpringCloud Alibaba主要讲解的内容&#xff1a; 备注&#xff1a;黑色部分是springcloud社区原版&#xff0c;红色的是SpringCloud Alibaba 服务注册与发现 Consul Alibaba Nacos 服务调用和负载均衡 LoadBalancer OpenFeign 分布式事务 Alibaba Seata 服务熔…

跨系统环境下LabVIEW程序稳定运行

在LabVIEW开发中&#xff0c;不同电脑的配置和操作系统&#xff08;如Win11与Win7&#xff09;可能对程序的稳定运行产生影响。为了确保程序在不同平台上都能正常且稳定运行&#xff0c;需要从兼容性、驱动、以及性能优化等多个方面入手。本文将详细介绍如何在不同系统环境下&a…

【华为OD】2024D卷——生成哈夫曼树

题目描述&#xff1a; 给定长度为n的无序的数字数组&#xff0c;每个数字代表二叉树的叶子节点的权值&#xff0c;数字数组的值均大于等于1。 请完成一个函数&#xff0c;根据输入的数字数组&#xff0c;生成哈夫曼树&#xff0c;并将哈夫曼树按照中序遍历输出。为了保证输出的…

网络学习-eNSP配置路由器

#PC1网关&#xff1a;192.168.1.254 #PC3网关&#xff1a;192.168.3.254 #PC4网关&#xff1a;192.168.4.254# 注&#xff1a;路由器接口必须配置不同网段IP地址 <Huawei>system-view Enter system view, return user view with CtrlZ. #给路由器两个接口配置IP地址 [Hua…

Linux 上安装 PostgreSQL

Linux 上安装 PostgreSQL PostgreSQL 是一款功能强大的开源关系数据库管理系统,因其稳定性、可扩展性和先进的功能而广受欢迎。在 Linux 系统上安装 PostgreSQL 是一个相对直接的过程,但具体步骤可能会因您使用的 Linux 发行版而异。本文将介绍在几种流行的 Linux 发行版上安…

AIPaperGPT写论文靠谱吗?

AIPaperGPT&#xff0c;论文写作神器~ https://www.aipapergpt.com/ 在信息爆炸的今天&#xff0c;学术写作的挑战日益增加&#xff0c;而AIPaperGPT作为一款旨在提升写作效率的工具&#xff0c;其可靠性自然成为了用户关注的焦点。本文将从多个维度对AIPaperGPT进行全面评估&…

CCF编程能力等级认证GESP—C++1级—20240907

CCF编程能力等级认证GESP—C1级—20240907 单选题&#xff08;每题 2 分&#xff0c;共 30 分&#xff09;判断题&#xff08;每题 2 分&#xff0c;共 20 分&#xff09;编程题 (每题 25 分&#xff0c;共 50 分)小杨购物第二题 单选题&#xff08;每题 2 分&#xff0c;共 30…

链表的实现

链表是数据结构中一种基础且重要的数据结构&#xff0c;它允许我们有效地在序列中插入和删除元素&#xff0c;而无需重新分配整个数据结构。与数组相比&#xff0c;链表提供了更高的灵活性&#xff0c;但也可能在访问速度上有所牺牲。现在我将将从基础概念出发&#xff0c;逐步…