只使用Feign不引入Eureka

强烈推荐一个大神的人工智能的教程:http://www.captainbed.net/zhanghan

【前言】

       随着业务不断的扩展以及开发团队的不断壮大,单体服务已经满足我们现在项目的需求;于是乎我们在新做的项目中果断采用了微服务,与此同时将我们的老项目逐步往微服务方向上改造;

        在技术选型中,根据我们的需求以及未来发展的趋势,我们选择了SpringCloud全家桶同时对其中几个组件做了替换,比如:配置中心采用携程的Apollo,调用链监控采用了点评的Cat等等;后续文章会逐步讲解;

        今天为大家分享的是自己在做Feign的验证时的一个经典案例,以此来快速上手Feign;

【实战】

         大家提起Feign潜意识里认为要配合Eureka注册中心来使用,有些项目正处在改造的初期或项目中之前用的注册中心不是Eureka;没关系,其实Feign单独也可以使用;

          一、项目目录结构

                parent(pom) :父Pom,jar包统一版本管理,聚合Pom;

                ---api:API工程服务提供方暴露服务被消费方依赖;

                ---consumer:服务消费方;

                ---provider:服务提供方;

          二、重要代码

                1、API接口

package com.feign.api;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@FeignClient(name = "provider", url = "http://localhost:8081")
public interface GetNumApi {
    @RequestMapping(value = "/getNum", method = RequestMethod.GET)
    int getRandomInt();
}

                2、消费者调用

package com.feign.consumer.controller;

import com.feign.api.GetNumApi;
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
    private GetNumApi restFulService;


    @RequestMapping(value = "/consumer/getNum", method = RequestMethod.GET)
    public int getRandomInt() {

        return restFulService.getRandomInt();
    }

}

                3、消费者启动类

package com.feign.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication(scanBasePackages = {"com.feign.consumer"})
@EnableFeignClients(basePackages = {"com.feign.api"})
public class ConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }

}

          三、效果展示

          访问http://localhost:8080/consumer/getNum

          四、本案例的代码

          我的github---https://github.com/dangnianchuntian/springcloud

【总结】

       1、不断的实践,不断的完善;

       2、快速做出案例,深入思去思考,比如:本案例中不用Eureka等服务中心有什么优势同时有什么不足?Feign相比与httpclient有什么优势和不足?模块划分是否合理?如何在以后的升级中管理好版本?

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

当年的春天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值