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

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

创新互联专注为客户提供全方位的互联网综合服务,包含不限于网站设计、网站制作、普陀网络推广、微信小程序定制开发、普陀网络营销、普陀企业策划、普陀品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联为所有大学生创业者提供普陀建站搭建服务,24小时服务热线:028-86922220,官方网址:www.cdcxhl.com

二、配置文件
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=6020
spring.application.name=springcloud-eureka-customer
eureka.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;

@RestController
public 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服务消费)


文章题目:Idea下SpringCloud2实验(三、Eureka+Fegin服务消费)
标题来源:http://scyanting.com/article/gccpej.html