博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Idea下SpringCloud2实验(三、Eureka+Fegin服务消费)
阅读量:6446 次
发布时间:2019-06-23

本文共 4433 字,大约阅读时间需要 14 分钟。

一、创建SpringBoot的项目springcloud-eureka-customer,操作同

二、配置文件

1、pom.xml

4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.4.RELEASE
com.jane
springcloud-eureka-customer
0.0.1-SNAPSHOT
springcloud-eureka-customer
Demo project for Spring Boot
1.8
Greenwich.SR1
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-openfeign
2.0.2.RELEASE
org.springframework.cloud
spring-cloud-openfeign-core
2.0.2.RELEASE
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin

2、启动文件

package com.jane.springcloudeurekacustomer;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.openfeign.EnableFeignClients;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.ComponentScans;@SpringBootApplication@EnableDiscoveryClient@EnableFeignClients(basePackages = "com.jane")@ComponentScan("com.jane")public class SpringcloudEurekaCustomerApplication {    public static void main(String[] args) {        SpringApplication.run(SpringcloudEurekaCustomerApplication.class, args);    }}

3、application.properties

server.port=6020spring.application.name=springcloud-eureka-customereureka.client.service-url.defaultZone=http://127.0.0.1:6001/eureka/eureka.client.fetchRegistry=true

4、Fegin客户端TestFeginServiceClient

package com.jane.service;import org.springframework.cloud.openfeign.FeignClient;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;@FeignClient("springcloud-eureka-client-order")public interface TestFeginServiceClient {    @RequestMapping(value = "/test", method = RequestMethod.GET)    public String test();}

5、调用文件TestController

package com.jane.controller;import com.jane.service.TestFeginServiceClient;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class TestController {    @Autowired    TestFeginServiceClient testService;    /**     * 普通Restful     * @return     */    @RequestMapping(value = "/local", method = RequestMethod.GET)    public String local() {        return "本地local调用";    }    /**     * 利用Fegin客户端实现RPC调用order服务     * @return     */    @RequestMapping(value = "/order/test", method = RequestMethod.GET)    public String test(){        return testService.test();    }}

三、启动文件

1、服务中心
Idea下SpringCloud2实验(三、Eureka+Fegin服务消费)
2、服务消费
2.1本地调用
Idea下SpringCloud2实验(三、Eureka+Fegin服务消费)
2.2RPC调用
Idea下SpringCloud2实验(三、Eureka+Fegin服务消费)

四、FeignClient本身集成了本地负载均衡Netflix的Ribbon,所以可以轻松实现负载均衡。

1、修改client-order端口号6011,多启动一个服务
Idea下SpringCloud2实验(三、Eureka+Fegin服务消费)
Idea下SpringCloud2实验(三、Eureka+Fegin服务消费)

2、消费服务多次调用结果如下:

Idea下SpringCloud2实验(三、Eureka+Fegin服务消费)
Idea下SpringCloud2实验(三、Eureka+Fegin服务消费)

转载于:https://blog.51cto.com/janephp/2391474

你可能感兴趣的文章