SpringCloud第三篇:服务的提供与Feign调用

上一篇,我们介绍了注册中心的搭建,包括集群环境吓注册中心的搭建,这篇文章介绍一下如何使用注册中心,创建一个服务的提供者,使用一个简单的客户端去调用服务端提供的服务。

本篇文章中需要三个角色,分别是服务的提供者,服务的消费者,还有一个是上一篇文章的主角——注册中心Eureka(使用单机版本即可,本篇的示例也会使用单机版本的Eureka)。(了解源码可+求求: 1791743380)

整体流程为:

先启动注册中心Eureka

启动服务的提供者将提供服务,并将服务注册到注册中心Eureka上

启动服务的消费者,在注册中心中找到服务并完成消费

1. 服务提供者

1. pom.xml

<?xml version="1.0" encoding="UTF-8"?> 4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.6.RELEASE <!-- lookup parent from repository --> com.springcloud producer 0.0.1-SNAPSHOT producer Demo project for Spring Boot 1.8 Greenwich.SR1 org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin

2. 配置文件application.yml

server:  port:8080spring:  application:    name:spring-cloud-producereureka:  client:    service-url:      defaultZone:http://localhost:8761/eureka/

3. 启动类ProducerApplication.java

增加@EnableEurekaClient,如果是其他注册中心可以使用注解@EnableDiscoveryClient来进行服务的注册

packagecom.springcloud.producer;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication@EnableEurekaClientpublicclassProducerApplication{publicstaticvoidmain(String[] args){        SpringApplication.run(ProducerApplication.class, args);    }}

4. Controller

packagecom.springcloud.producer.controller;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestParam;importorg.springframework.web.bind.annotation.RestController;/** * Created with IntelliJ IDEA. * *@Date: 2019/7/2 *@Time: 0:02 *@email: inwsy@hotmail.com * Description: */@RestControllerpublicclassHelloController{@RequestMapping("/hello")publicStringhello(@RequestParam String name){return"hello "+name+",producer is ready";    }}

先在可以先启动上一篇当中单机版的Eureka,再启动我们刚写好的producer服务提供者,启动成功后,访问链接http://localhost:8761/,可以看到我们的的服务提供者producer已经成功注册在注册中心上了。

至此,服务的提供者已经配置完成。

2. 服务消费者

1. pom.xml

<?xml version="1.0" encoding="UTF-8"?>4.0.0org.springframework.bootspring-boot-starter-parent2.1.6.RELEASE<!-- lookup parent from repository -->com.springcloudconsumers0.0.1-SNAPSHOTconsumersDemo project for Spring Boot1.8Greenwich.SR1org.springframework.cloudspring-cloud-starter-netflix-eureka-clientorg.springframework.bootspring-boot-starter-weborg.springframework.cloudspring-cloud-starter-openfeignorg.springframework.bootspring-boot-starter-testtestorg.springframework.cloudspring-cloud-dependencies${spring-cloud.version}pomimportorg.springframework.bootspring-boot-maven-plugin

spring-boot-starter-web: 这个包是通用的web开发包,里面包含了spring-web、spring-webmvc等包

spring-cloud-starter-openfeign: 这个包是springcloud对于Feign的封装,Feign是一个声明式的Web服务客户端。它支持Feign本身的注解、JAX-RS注解以及SpringMVC的注解。Spring Cloud集成Ribbon和Eureka以在使用Feign时提供负载均衡的http客户端。

2. 配置文件application.yml

server:  port:8081spring:  application:    name:spring-cloud-consumerseureka:  client:    service-url:      defaultZone:http://localhost:8761/eureka/

3. 启动类ConsumersApplication.java

同上,增加@EnableEurekaClient,如果是其他注册中心可以使用注解@EnableDiscoveryClient来进行服务的注册

packagecom.springcloud.consumers;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cloud.netflix.eureka.EnableEurekaClient;importorg.springframework.cloud.openfeign.EnableFeignClients;@SpringBootApplication@EnableEurekaClient@EnableFeignClientspublicclassConsumersApplication{publicstaticvoidmain(String[] args){        SpringApplication.run(ConsumersApplication.class, args);    }}

@EnableFeignClients: 这个注解是通知SpringBoot在启动的时候,扫描被 @FeignClient 修饰的类,@FeignClient这个注解在进行远程调用的时候会用到。

4. Feign远程调用

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

创建一个remote接口

packagecom.springcloud.consumers.remote;importorg.springframework.cloud.openfeign.FeignClient;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestParam;/** *@Author: shiyao.wei *@Date: 2019/7/2 11:14 *@Version: 1.0 *@Desc: */@FeignClient(name="spring-cloud-producer")publicinterfaceHelloRemote{@RequestMapping(value ="/hello")Stringhello(@RequestParam(value ="name")String name);}

name:远程服务名,及spring.application.name配置的名称

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

5. web层调用远程接口 Controller

packagecom.springcloud.consumers.controller;importcom.springcloud.consumers.remote.HelloRemote;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.PathVariable;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;/** *@Author: shiyao.wei *@Date: 2019/7/2 11:25 *@Version: 1.0 *@Desc: */@RestControllerpublicclassHelloController{@AutowiredHelloRemote helloRemote;@RequestMapping("/hello/{name}")publicStringindex(@PathVariable("name")String name){returnhelloRemote.hello(name);    }}

现在,一个最简单的服务注册和调用的例子就完成了。

3. 测试

简单调用

顺次启动eureka、producer、consumer三个项目

启动成功后,先在浏览器输入http://localhost:8080/hello?name=springcloud

可以看到页面显示:hello springcloud,producer is ready

证明我们的producer已经正常启动,提供的服务也正常

接下来,我们测试服务消费者,在浏览器中输入:http://localhost:8081/hello/spring

可以看到页面显示:hello spring,producer is ready

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

负载均衡

将上面的producer复制一份,修改名称为producer2,修改pom.xml中的\\为producer2,修改其中的Controller:

packagecom.springcloud.producer.controller;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestParam;importorg.springframework.web.bind.annotation.RestController;/** * Created with IntelliJ IDEA. * *@Date: 2019/7/2 *@Time: 0:02 *@email: inwsy@hotmail.com * Description: */@RestControllerpublicclassHelloController{@RequestMapping("/hello")publicStringhello(@RequestParam String name){return"hello "+name+",producer2 is ready";    }}

修改application.yml配置文件启动端口为8082

启动我们刚复制好的producer2,这时可以看一下注册中心Eureka,我们现在已经有两个producer服务了。

这时我们再去访问:http://localhost:8081/hello/spring

第一次返回结果:hello spring,producer is ready

第二次返回结果:hello spring,producer2 is ready

连续刷新页面,两个结果会交替出现,说明注册中心提供了服务负载均衡功能。将服务数提高到N个,会发现测试结果一样,请求会自动轮询到每个服务端来处理。    

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 211,042评论 6 490
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 89,996评论 2 384
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 156,674评论 0 345
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,340评论 1 283
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,404评论 5 384
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,749评论 1 289
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,902评论 3 405
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,662评论 0 266
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,110评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,451评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,577评论 1 340
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,258评论 4 328
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,848评论 3 312
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,726评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,952评论 1 264
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,271评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,452评论 2 348