springCloud - 第4篇 - 消费者调用服务 ( Feign )

前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到教程。

一、新建 ribbon 工程:

1. file - new - module 

2. spring Initializr - module SDK 选择自己的 JDK ,其余的可以不用填写,next。

3. 填写工程相关信息:包名、工程名等,next。

4. spring cloud discovery - 勾选 eureka discover client,next。

或:spring cloud routing - 勾选 openfeign,next。 ( 这步这2种都可。)

5. 工程名,代码存放位置等,finish 。

6. 工程结构如下:

7. pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.feign</groupId>
    <artifactId>service-feign</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>service-feign</name>
    <description>服务消费 feign 方式</description>

    <parent>
        <groupId>com.base</groupId>
        <artifactId>base-config</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

</project>

 8. 在工程启动类上加注解:@EnableFeignClients 。

package com.feign.servicefeign;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;


@SpringBootApplication

/**
 * 基于接口的注解,可插拔,可使用 Feign注解 和 JAX-RS注解
 * Feign 默认集成 Ribbon,并和 Eureka 结合,默认实现负载均衡。
 */
@EnableFeignClients

// 标明自已为服务
@EnableEurekaClient
public class ServiceFeignApplication {

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

}

9. 创建 interface SeeParamService,使用注解指定要调用的服务 @FeignClient ( value = "服务名" )

package com.feign.servicefeign;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @Description:
 * @Auther: silence
 * @Date: 2019/7/23 20:09
 */
@FeignClient(value = "see-param")
public interface SeeParamService {

    @RequestMapping(value = "/seeParam", method = RequestMethod.GET)
    String seeParam(@RequestParam(value = "param") String param);

}

对外访问 controller :

package com.feign.servicefeign;

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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author yujiang
 * @description
 * @date 2019/7/23 20:50
 */
@RestController
public class SeeParamController {

    @Autowired
    SeeParamService seeParamService;

    @RequestMapping(value = "/seeParam", method = RequestMethod.GET)
    public String seeParam(@RequestParam String param) {
        return seeParamService.seeParam(param);
    }

}

10. 配置文件相关设置:



# 注册中心 - 端口: 1234、工程名: eureka (见 eureka 工程中配置)。
eureka.client.serviceUrl.defaultZone= http://localhost:1234/eureka/

# 端口
server.port= 8702

# 工程名
spring.application.name= feign

11. 浏览器访问:  http://localhost:8702/seeParam?param=我爱小熊 。刷新多次请求,得到不同端口服返回的结果 。

 

12. 从注册中心可知,当前注册了5 个服务,访问注册中心:http://localhost:1234/

13. 不断刷新 ribbon 工程访问地址,可见 8801、8802、8803 都有请求到。图见第 11 点。

说明 负载均衡 已实现,消费者(服务请求方应用)请求到了不同的生产者(服务提供方应用)。

14.总结:

此时 整个工程体系为:

1个注册中心:eureka 工程,端口:1234 。

3个生产者 see-param ,分别占用端口:8801、8802、8803 ,三者都向 eureka  注册,暴露自已提供的服务。

1个消费者 ribbon 工程,端口:8701 。向 eureka 注册 ,订阅自已所需要的服务。

另 1 个消费者 feign 工程 ,端口:8702。向 eureka 注册 ,订阅自已所需要的服务。

feign 有作负载均衡,故 在调用生产者服务时,可轮流请求到不同的生产者服务。

-------------------------------------------------------------

下一篇:springCloud - 第5篇 - 断路器 Hystrix ( Feign 、Ribbon )

源码见:https://gitee.com/FJ_WoMenDeShiJie/springcloud-ribbon.git

-------------------------------------------------------------

PS:这个系列不定时更新,只是个人的学习分享,

内容全程参考书目:

《Spring Cloud 与 Docker 微服务架构空实战 》、

《Spring Cloud 微服务实战》及此书作者博客:http://blog.didispace.com/spring-cloud-learning/

《深入理解 Spring Cloud 与微服务构建》及此书作者博客:https://blog.csdn.net/forezp/article/details/70148833
--------------------------------------------------------------

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值