本文共 6042 字,大约阅读时间需要 20 分钟。
SpringCloud(二):服务提供与调用【Finchley版】
上一篇我们介绍了服务注册与发现,这一篇我们来讲讲服务提供与调用,服务的提供者像注册中心注册服务,调用方从注册中心拉去服务进行使用,逻辑非常的简单,Spring cloud有两种服务调用方式,一种是 ribbon + restTemplate,另一种是 feign,今天我们就来简单的学习一下这两种调用方法。
在该demo中将有4个服务,注册中心、服务提供方、基于ribbon + restTemplate的服务调用方、基于feign的服务调用方,在后面的demo中,都将使用nacos作为服务注册中心并且默认启动。服务提供方
服务提供方功能非常简单,提供一个hello方法,输出hello spring cloud+机器端口。创建一个名为 hello-provider-service 的 springboot 项目,对项目进行下面一系列改造。
1、pom.xml
在 pom.xml 里引入 nacos 依赖和 springboot web 依赖。
org.springframework.boot spring-boot-starter-web # naocs服务注册依赖 org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-starter-alibaba-nacos-discovery 0.2.2.RELEASE
2、引导类
在引导类上添加@EnableDiscoveryClient
注解
@SpringBootApplication@EnableDiscoveryClientpublic class HelloProviderServiceApplication { public static void main(String[] args) { SpringApplication.run(HelloProviderServiceApplication.class, args); }}
3、服务提供类
建立一个HelloController类,编写一个hello方法。
@RestControllerpublic class HelloController { // 服务器端口 @Value("${server.port}") String port; @GetMapping(path = "/hello") public String hello() { return "hello spring cloud ,i am port :" + port; }}
4、application.yml
server: port: 8085spring: application: name: hello-provider-service cloud: nacos: discovery: server-addr: 127.0.0.1:8848
修改好之后,我们将服务提供方项目打包,执行mvn install -Dmaven.test.skip=true
命令,就可以在target
目录下找到jar包,我们需要将服务端部署两份,可以使用服务调用方的负载均衡功能。进入到target
目录,执行下面两条命令,来启动两个提供方服务
java -jar hello-provider-service-0.0.1-SNAPSHOT.jar --server.port=8085
java -jar hello-provider-service-0.0.1-SNAPSHOT.jar --server.port=8086
经过上述操作之后,我们已经将服务提供方部署好了。 Ribbon + RestTemplate 服务调用方
Ribbon 是一个客户端负载均衡器,可以让您对HTTP和TCP客户端的行为进行大量控制。
RestTemplate 是 spring 框架提供的可用于在应用中调用 rest 服务的工具类,它简化了与 http 服务的通信方式,统一了 RESTful 的标准,封装了 http 链接, 我们只需要传入url及返回值类型即可。相较于之前常用的 HttpClient,RestTemplate 是一种更优雅的调用 RESTful 服务的方式,在 springboot 中有默认的 RestTemplate bean。
创建一个名为 ribbon-customer-service 的 springboot 项目,该项目也需要像注册中心注册,创建完项目之后,对项目进行下面改造。
1、pom.xml
在 pom.xml 里引入ribbon
、nacos
依赖包
org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-starter-alibaba-nacos-discovery 0.2.2.RELEASE org.springframework.cloud spring-cloud-starter-netflix-ribbon
2、引导类
在引导类上添加@EnableDiscoveryClient
注解和实例化一个具有负载均衡功能的 RestTemplate
bean ,通过添加 @LoadBalanced
注解来表明这个restRemplate开启负载均衡的功能
@SpringBootApplication@EnableDiscoveryClientpublic class RibbonCustomerServiceApplication { public static void main(String[] args) { SpringApplication.run(RibbonCustomerServiceApplication.class, args); } @Bean @LoadBalanced // 开启负载均衡 RestTemplate restTemplate(){ return new RestTemplate(); }}
3、业务层
service 层
创建HelloService
类并且添加@Service
注解
@Servicepublic class HelloService { @Autowired private RestTemplate restTemplate; public String hello(){ // 根据服务名调用 return restTemplate.getForObject("http://hello-provider-service/hello",String.class); }}
controller 层
@RestControllerpublic class HelloController { @Autowired private HelloService helloService; @GetMapping(path = "/hello") public String hello(){ return helloService.hello(); }}
4、application.yml 配置文件
server: port: 8087spring: application: name: ribbon-customer-service cloud: nacos: discovery: server-addr: 127.0.0.1:8848
启动 ribbon-customer-service 项目,在浏览器里访问http://127.0.0.1:8087/hello
,你将交替看到
hello spring cloud ,i am port :8085
hello spring cloud ,i am port :8086
基于 Feign 的服务调用方
Feign 是一种声明式、模板化的 HTTP 客户端,是一种基于接口注解的编程模式,在使用 Feign 做服务调用的时候,它能够让你像调用本地方法一样,完全感觉不到这是一个通过 http 请求调用远程方法。Feign 主要有如下特性:
- 可插拔的注解支持,包括Feign注解和JAX-RS注解
- 支持可插拔的HTTP编码器和解码器
- 支持Hystrix和它的Fallback
- 支持Ribbon的负载均衡
- 更多介绍请访问
https://github.com/openfeign/feign
上面我们简单的介绍了 Feign ,接下来我们创建一个名为 feign-customer-service 的 springboot 项目,按照套路对项目进行以下几步改造。
1、pom.xml
在 pom.xml 中添加nacos
、feign
的依赖
org.springframework.cloud spring-cloud-starter-alibaba-nacos-discovery 0.2.2.RELEASE org.springframework.cloud spring-cloud-starter-openfeign
2、引导类
在引导类跟上添加@EnableFeignClients
注解来启动feign
功能
@SpringBootApplication@EnableDiscoveryClient@EnableFeignClientspublic class FeignCustomerServiceApplication { public static void main(String[] args) { SpringApplication.run(FeignCustomerServiceApplication.class, args); }}
3、业务层
service 层
创建一个HelloService
接口,通过@FeignClient
来指定调用那个服务
@FeignClient(value = "hello-provider-service")public interface HelloService { @GetMapping(path = "/hello") String hello();}
controller 层
创建一个HelloController
类,对外提供一个hello
方法即可
@RestControllerpublic class HelloController { @Autowired private HelloService helloService; @GetMapping(path = "/hello") public String hello(){ return helloService.hello(); }}
5、application 配置
server: port: 8088spring: cloud: nacos: discovery: server-addr: 127.0.0.1:8848 application: name: feign-customer-service
启动feign-customer-service
项目,在浏览器里访问http://127.0.0.1:8088/hello
,我们能够看到跟上面一样的效果。浏览器里面交替显示:
hello spring cloud ,i am port :8085
hello spring cloud ,i am port :8086
本文demo下载 :
转载地址:https://blog.csdn.net/z694644032/article/details/96571338 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!