controller调用controller的方法_微服务SpringCloud之服务调用与负载均衡

上一篇我们学习了服务的注册与发现,本篇博客是在上一篇的基础上学习服务的调用。上一博客主要创建了Eureka的服务端和一个Client,该Client包含了一个Controller用来提供对外服务供外部调用,可以作为生产者。

一、引入依赖

前面创建了EurekaClient的项目,在项目中引入了spring-cloud-starter-netflix-eureka-client用来注册服务,是生产者,这里调用属于消费者,同样也需要引入spring-cloud-starter-netflix-eureka-client,这里还使用了openfeign来调用生产者提供的服务。具体pom.xml如下,主要引入spring-cloud-starter-netflix-eureka-client、spring-boot-starter-web、spring-cloud-starter-openfeign、spring-cloud-openfeign-core。

<?xml version="1.0" encoding="UTF-8"?>4.0.0org.springframework.boot spring-boot-starter-parent 2.1.6.RELEASEcom.example EurekaConsumer 0.0.1-SNAPSHOTEurekaConsumerDemo project for Spring Boot1.8Greenwich.SR1org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.cloud spring-cloud-starter-openfeign org.springframework.cloud spring-cloud-openfeign-core 2.1.2.RELEASEorg.springframework.boot spring-boot-starter-test testorg.springframework.cloud spring-cloud-dependencies ${spring-cloud.version}pomimportorg.springframework.boot spring-boot-maven-plugin war

二、在main类中引入@EnableDiscoveryClient、@EnableFeignClients注解

package com.example.demo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.openfeign.EnableFeignClients;@SpringBootApplication@EnableDiscoveryClient@EnableFeignClientspublic class EurekaConsumerApplication { public static void main(String[] args) { SpringApplication.run(EurekaConsumerApplication.class, args); }}

@EnableDiscoveryClient :启用服务注册与发现

@EnableFeignClients:启用feign进行远程调用

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

三、配置文件

spring.application.name=spring-cloud-consumerserver.port=9001eureka.client.serviceUrl.defaultZone=http://localhost:8088/eureka/

四、feign调用实现

package com.example.demo;import org.springframework.cloud.openfeign.FeignClient;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;@FeignClient(name= "spring-cloud-producer")public interface HelloRemote { @RequestMapping(value = "/hello") public String hello(@RequestParam(value = "name") String name);}

name:远程服务名,及spring.application.name配置的名称,此类中的方法和远程服务中contoller中的方法名和参数需保持一致。

五、web层调用远程服务

这里使用@Autowired将远程访问类注入到Controller中用来调用服务。

package com.example.demo;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class ConsumerController { @Autowired HelloRemote HelloRemote;  @RequestMapping("/hello") public String index(@RequestParam String name) { return HelloRemote.hello(name); }}

六、测试

分别启动Server、Client、Consumer,依次输入http://localhost:8088/、http://localhost:9000/hello?name=cuiyw、http://localhost:9001/hello?name=cuiyw。端口8088是Eureka Server端,9000是Eureka Client,作为生产者,端口9001也是Eureka Client,作为消费者,在端口9001中消费者调用生产者9000提供的服务。

53b620d5f5ec4c8c901120a4f9e22462
bc734375d06f49c18e2338ad4dfaa47b
c86cb6c1e91943a19f19845dc8d8912a

七、负载均衡

修改EurekaClient项目中的Controller和配置文件。在Controller中的index方法增加了修改了return的值,用来区分生产者。在application.properties中修改了端口号。

package com.example.demo;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class HelloController { @RequestMapping("/hello") public String index(@RequestParam String name) { return "hello "+name+",this is first messge 2"; }}
spring.application.name=spring-cloud-producerserver.port=9002eureka.client.serviceUrl.defaultZone=http://localhost:8088/eureka/

启动该Client,这时在Eureka Server中可以看到两个生产者。

d3fc70e76a1c4a82b35ac7037c92a775

在浏览器中数次刷新url:http://localhost:9001/hello?name=cuiyw,可以看到不同的响应结果。

68d9fe29cdd84683a347f529a4eb3117
4cb85c7062a64e07bee9a34625ea149c

八、小结

本篇主要学习了下服务的调用以及简单的使用openfeign进行的负载均衡,后面会参考纯洁的微笑的博文学习熔断器Hystrix。

本篇博客参考:http://www.ityouknow.com/springcloud/2017/05/12/eureka-provider-constomer.html

九、号外

本人的新书已出版上架,书名Spring快速入门,天猫、京东也都能搜得到,这里也算是王婆卖瓜自卖自夸一下,大家如果感兴趣可以了解一下,个人感觉对初学者来说还是值得一读的。

ff14685344f24a87bf2e235ebe6695c4

作者:社会主义接班人

出处:http://www.cnblogs.com/5ishare/

本文版权归作者和博客园共有,未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。如文中有错误,欢迎指出。以免更多的人被误导。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值