【SpringCloud Greenwich版本】第三章:服务消费者(Feign)

一、SpringCloud版本

本文介绍的Springboot版本为2.1.1.RELEASE,SpringCloud版本为Greenwich.RC1,JDK版本为1.8,集成环境为IntelliJ IDEA

二、Feign介绍

Feign是一个声明式的Web服务客户端。这使得Web服务客户端的写入更加方便。要使用Feign创建一个界面并对其进行注释。它具有可插入注释支持,包括Feign注释和JAX-RS注释。Feign还支持可插拔编码器和解码器。Spring Cloud增加了对Spring MVC注释的支持,并使用Spring Web中默认使用的HttpMessageConverters。Spring Cloud集成Ribbon和Eureka以在使用Feign时提供负载均衡的http客户端。

集成Feign需在您的项目中包含Feign,请使用组org.springframework.cloud和工件ID spring-cloud-starter-openfeign的启动器

三、创建Feign服务

  • 3.1创建

选择Cloud Routing–Feign创建一个新的module工程,取名为cloudcustomer
【SpringCloud Greenwich版本】第三章:服务消费者(Feign)_第1张图片pom配置文件如下,可以看到增加了spring-cloud-starter-openfeign依赖

<?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.jthao</groupId>
    <artifactId>cloudcustomer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>cloudcustomer</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>com.jthao</groupId>
        <artifactId>cloudser</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

</project>

修改工程中配置文件,指定服务注册中心地址,端口为8004,服务名为cloudcustomer

eureka.client.service-url.defaultZone: http://localhost:8001/eureka/
server.port=8004
spring.application.name=cloudcustomer
  • 3.2启动

需在启动类上增加@EnableDiscoveryClient和@EnableFeignClients注解,其中@EnableFeignClients注解开启访问功能

package com.jthao.cloudcustomer;

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

@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication
public class CloudcustomerApplication {

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

}

通过Feign定义一个接口,这里我们定义为cloudclient工程的test接口

package com.jthao.cloudcustomer.service;

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

@FeignClient(name = "cloudclient")
public interface TestService {
    @RequestMapping("/test")
    String hello(@RequestParam String name);
}

再定义一个controller来访问这个接口

package com.jthao.cloudcustomer.controller;

import com.jthao.cloudcustomer.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @Autowired
    TestService testService;

    @RequestMapping(value = "/test")
    public String test(@RequestParam String name) {

        return testService.hello(name);
    }

}
  • 3.3访问

通过浏览器多次访问http://localhost:8004/test?name=honghong,我们可以看到如下展示

honghong===端口:8002被调用了===
honghong===端口:8003被调用了===

四、更多文章阅读

【SpringCloud Greenwich版本】汇总
【JAVA设计模式】汇总

你可能感兴趣的:(SpringCloud)