网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

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://bjjierui.cn/article/gccpej.html

其他资讯