场景

SpringCloud-服务注册与实现-Eureka创建服务注册中心(附源码下载):

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102535957

SpringCloud-服务注册与实现-Eureka创建服务提供者(附源码下载):

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102558004

SpringCloud-创建服务消费者-Ribbon方式(附代码下载):

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102558080

在上面已经实现服务注册中心、服务提供者和以Ribbon方式实现服务消费者的前提下,使用另一种Feign方式实现服务消费者。

Feign

Feign 是一个声明式的伪 Http 客户端,它使得写 Http 客户端变得更简单。使用 Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,可使用 Feign 注解和 JAX-RS 注解。Feign 支持可插拔的编码器和解码器。Feign 默认集成了 Ribbon,并和 Eureka 结合,默认实现了负载均衡的效果

注:

博客:
https://blog.csdn.net/badao_liumang_qizhi

关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

参考上面构建项目的方式,依次建立目录hello-spring-cloud-web-admin-feign目录以及在

目录下新建pom.xml,并将其托管。然后新建src/main/java目录和src/main/resources目录并分别进行目录设置。

然后在java下新建包,包下新建启动类,在resources下新建配置文件application.yml。

完成后的目录为:

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.</modelVersion> <parent>
<groupId>com.badao</groupId>
<artifactId>hello-spring-cloud-dependencies</artifactId>
<version>1.0.-SNAPSHOT</version>
<relativePath>../hello-spring-cloud-dependencies/pom.xml</relativePath>
</parent> <artifactId>hello-spring-cloud-web-admin-feign</artifactId>
<packaging>jar</packaging> <name>hello-spring-cloud-web-admin-feign</name>
<url>https://blog.csdn.net/badao_liumang_qizhi</url>
<inceptionYear>-Now</inceptionYear> <dependencies>
<!-- Spring Boot Begin -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Boot End --> <!-- Spring Cloud Begin -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- Spring Cloud End --> <!-- 解决 thymeleaf 模板引擎一定要执行严格的 html5 格式校验问题 -->
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.badao.hello.spring.cloud.web.admin.feign.WebAdminFeignApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>

注:

这里的parent标签要与上面的统一的依赖管理对应起来。

要修改指定的程序入口类为自己相应的路径。

然后应用启动类的代码:

package com.badao.hello.spring.cloud.web.feign;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class WebAdminFeignApplication {
public static void main(String[] args) {
SpringApplication.run(WebAdminFeignApplication.class, args);
}
}

注:

通过 @EnableDiscoveryClient 注解注册到服务中心

通过 @EnableFeignClients 注解开启 Feign 功能

然后是配置文件application.yml

spring:
application:
name: hello-spring-cloud-web-admin-feign
thymeleaf:
cache: false
mode: LEGACYHTML5
encoding: UTF-
servlet:
content-type: text/html server:
port: eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/

注:

1.服务注册与发现是根据上面的name去寻找。

2.port表示端口号。

3.serviceURL设置eureka的地址,与上面创建服务注册中心时的URL对应。

与使用Ribbon方式不同的是,这里需要创建service接口,而不是service类。

AdminService接口代码:

package com.badao.hello.spring.cloud.web.feign.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam; @FeignClient(value = "hello-spring-cloud-service-admin")
public interface AdminService { @RequestMapping(value = "hi", method = RequestMethod.GET)
public String sayHi(@RequestParam(value = "message") String message);
}

注:

通过@FeignClient(value =
"hello-spring-cloud-service-admin")来指定调用哪个服务。

这里就是对应上面服务提供者的配置文件的name属性。

import com.badao.hello.spring.cloud.web.feign.service.AdminService;
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; @RestController
public class AdminController { @Autowired
private AdminService adminService; @RequestMapping(value = "hi", method = RequestMethod.GET)
public String sayHi(@RequestParam String message) {
return adminService.sayHi(message);
}
}

为了体现出负载均衡效果,我们要启动两台service-admin,即启动两个服务提供者。

我们先启动服务注册中心Eureka服务8761端口,再以8762端口启动一个服务提供者,然后点击Run-Edit
Configuration,将启动单实例去掉。

然后修改服务提供者的配置文件中端口号为8763,再启动一个服务提供者。

消费者要想实现负载均衡的效果,应该一会访问8762的服务提供者,一会访问8763的服务提供者。

然后运行当前服务消费者的启动程序。

打开浏览器输入:

http://localhost:8765/hi?message=HelloFeign

此时的架构

一个服务注册中心,Eureka Server,端口号为:8761
service-admin 工程运行了两个实例,端口号分别为:8762,8763
web-admin-feign 工程端口号为:8765

源码下载

https://download.csdn.net/download/badao_liumang_qizhi/11867357

SpringCloud-创建服务消费者-Feign方式(附代码下载)的更多相关文章

  1. SpringCloud-创建服务消费者-Ribbon方式(附代码下载)

    场景 SpringCloud-服务注册与实现-Eureka创建服务注册中心(附源码下载): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/deta ...

  2. SpringCloud-使用熔断器防止服务雪崩-Ribbon和Feign方式(附代码下载)

    场景 SpringCloud-服务注册与实现-Eureka创建服务注册中心(附源码下载): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/deta ...

  3. 创建服务消费者(Feign)

    概述 Feign 是一个声明式的伪 Http 客户端,它使得写 Http 客户端变得更简单.使用 Feign,只需要创建一个接口并注解.它具有可插拔的注解特性,可使用 Feign 注解和 JAX-RS ...

  4. 从实例一步一步入门学习SpringCloud的Eureka、Ribbon、Feign、熔断器、Zuul的简单使用(附代码下载)

    场景 SpringCloud -创建统一的依赖管理: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102530574 Sprin ...

  5. Dubbo搭建HelloWorld-搭建服务提供者与服务消费者并完成远程调用(附代码下载)

    场景 Dubbo简介与基本概念: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103555224 Dubbo环境搭建-ZooKe ...

  6. springcloud-Netflix创建服务消费者

    目录 springcloud-Netflix创建服务消费者 Ribbon 创建服务消费者-Ribbon方式 ribbon的架构 Feign 创建包和基本项目结构 创建Feign访问服务的接口和访问co ...

  7. SpringCloud学习系列之二 ----- 服务消费者(Feign)和负载均衡(Ribbon)使用详解

    前言 本篇主要介绍的是SpringCloud中的服务消费者(Feign)和负载均衡(Ribbon)功能的实现以及使用Feign结合Ribbon实现负载均衡. SpringCloud Feign Fei ...

  8. springCloud学习-服务消费者(rest+ribbon)

    1.ribbon简介 spring cloud的Netflix中提供了两个组件实现软负载均衡调用:ribbon和feign. Ribbon 是一个基于 HTTP 和 TCP 客户端的负载均衡器 它可以 ...

  9. 创建服务消费者(Ribbon)

    概述 在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于 http restful 的.Spring cloud 有两种服务调用方式,一种是 ribbon + restTempla ...

随机推荐

  1. 更强的 JsonPath 兼容性及性能测试

    更强的 JsonPath 兼容性及性能测试 最近给自己的json框架snack3添加了json path支持.搞好之后,找了两个市面上流行框架比较性测试,以助自己改进框架的性能和兼容性. 测了一圈之后 ...

  2. Mybatis整合spring(适合小白)

    目录 1.整合思路 2.整合需要的jar包 3.整合的步骤 4.Dao的开发的两种实现方式 6.Dao的开发的实现方式总结图 @ Mybatis整合spring其实就是SSM框架中SM的整合集成. 1 ...

  3. vue 中使用 watch 的各种问题

    报错: Method "watch" has type "object" in the component definition. Did you refere ...

  4. springboot2中使用dubbo的三重境界

    在springboot中使用dubbo,本来是件挺简单的事情,但现实的世界就是如此的复杂,今天我用一个亲身经历的跳坑和填坑的事来讲在spring boot中使用高版本dubbo(当当的魔改版)的三重境 ...

  5. synchronized被这么问,谁能受得了

    synchronized是面试中经常会被问到的知识点,相关的问题点也很多,问题答案涉及的知识点也很多,有经验的面试官就会顺着你的答案不断追问一下,下面的对话场景就是相关面试题的连环炮. 面试官:说一下 ...

  6. springmvc运行流程简单解释(源码解析,文末附自己画的流程图)

    首先看一下DispatcherServlet结构: 观察HandlerExecutionChain对象的创建与赋值,这个方法用来表示执行这个方法的整条链. 进入getHandler方法: 此时的变量h ...

  7. Vue组件应用

    Vue的组件是可复用的 Vue 实例,且带有一个名字 .我们可以在一个通过 new Vue 创建的 Vue 根实例中,把这个组件作为自定义元素来使用.因为组件是可复用的 Vue 实例,所以它们与 ne ...

  8. 信鸽推送Push API

    目录 信鸽推送 push API 0. 基本 push 1. 根据 token list,推送到android和ios 2. 推送到android和ios 所有用户 信鸽推送 push API 参考: ...

  9. 2019企业linux运维最需要的了解的一些硬件基础知识

    第3章 服务器    245 3.1 电脑的种类    245 3.2 服务器的介绍    246 3.2.1 服务器的类别    246 3.2.2 服务器的性能    247 3.2.3 服务器的 ...

  10. 踩坑了!使用 @Autowired 注入成功,GetBean 方法却获取不到?!

    本文首发于个人微信公众号:Coder小黑 踩坑了?! 之前推文已经讲过 当@Transactional遇到@CacheEvict,你的代码是不是有bug! 现在要在事务提交之后清除缓存.在Spring ...