spring cloud备忘笔记-4-创建服务消费者(Feign)

spring cloud备忘笔记-4-创建服务消费者(Feign)


笔记索引:
spring cloud备忘笔记-0-目录索引

介绍

  Feign是是一个声明式的伪 Http 客户端,Feign本身就集成了ribbon,并且本身实现了负载均衡。采用基于接口的注解。实际开发中也直接用feign客户端。

开功

  创建项目前面的步骤和之前一样,一直自己手动的已经很熟悉了,就不多说了。创建一个hi-spring-cloud-web-admin-feign服务消费者项目
依赖

       <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
pom配置

  pom.xml配置好右边Maven Projects点击+让maven托管一下

<?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>

    <parent>
        <groupId>com.momomian</groupId>
        <artifactId>hi-spring-cloud-dependencies</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../hi-spring-cloud-dependencies/pom.xml</relativePath>
    </parent>

    <artifactId>hi-spring-cloud-web-admin-feign</artifactId>
    <packaging>jar</packaging>

    <name>hi-spring-cloud-web-admin-feign</name>
    <inceptionYear>2019-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.momomian.hi.spring.cloud.web.admin.feign.WebAdminFeignApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
入口
package com.momomian.hi.spring.cloud.web.admin.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);
    }
}

  @EnableFeignClients这个注解代表我是一个feign客户端,@EnableDiscoveryClient配置来寻找尤里卡

application.yml

spring:
  application:
    name: hi-spring-cloud-web-admin-feign
  thymeleaf:
    cache: false
    mode: LEGACYHTML5
    encoding: UTF-8
    servlet:
      content-type: text/html

server:
  port: 8785

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8780/eureka/ 

  和之前用ribbon一样我们创建一个接口调用服务,注意这里是interface接口
AdminService.java

package com.momomian.hi.spring.cloud.web.admin.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 = "hi-spring-cloud-service-admin")
public interface AdminService {

    @RequestMapping(value = "hi", method = RequestMethod.GET)
    public String sayHi(@RequestParam(value = "message") String message);
}

  这里@FeignClient(value = "hi-spring-cloud-service-admin")的value是服务提供者的name,在服务提供者项目的yml文件配置的application.name。
  @RequestMapping(value = “hi”, method = RequestMethod.GET),这个接口的写法和controller一样,表明你要访问哪个接口。
接下来我们创建controller层
AdminController.java

package com.momomian.hi.spring.cloud.web.admin.feign.controller;

import com.momomian.hi.spring.cloud.web.admin.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(value = "message") String message) {
        return adminService.sayHi(message);
    }
}

  这边@RequestParam注解必须要加上,用以确定匹配接口,否则会报405错误。
  这里的 @RequestMapping(value = “hi”, method = RequestMethod.GET)和上面service不一样,这里是controller的接口,你自己写的,上面必须指定服务提供者的接口,才能消费到那个接口。
  写到这里我们发现和ribbon从写法上的区别就是将服务提供者的name和接口配置使用注解的方式。然后采用的是接口式的注解方式

测试

  测试和上一篇一样,先启动eureka的项目,再启动两个服务提供者,最后启动feign的项目。
  调用一下8785的hi接口看看负载均衡已经实现。直接去参考上一篇:ribbon

笔记索引:
spring cloud备忘笔记-0-目录索引

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值