SpringCloud系列教程 | 服务的提供与Feign调用

本篇文章中需要三个角色,分别是服务的提供者,服务的消费者,还有一个是上一篇文章的主角——注册中心Eureka(使用单机版本即可,本篇的示例也会使用单机版本的Eureka)。

整体流程为:

先启动注册中心Eureka
启动服务的提供者将提供服务,并将服务注册到注册中心Eureka上
启动服务的消费者,在注册中心中找到服务并完成消费

  1. 服务提供者
  2. pom.xml
<?xml version="1.0" encoding="UTF-8"?>


4.0.0

org.springframework.boot
spring-boot-starter-parent
2.1.6.RELEASE


com.springcloud
producer
0.0.1-SNAPSHOT
producer
Demo project for Spring Boot

<properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <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-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
COPY 2. 配置文件application.yml server: port: 8080 spring: application: name: spring-cloud-producer eureka: client: service-url: defaultZone: http://localhost:8761/eureka/COPY 3. 启动类ProducerApplication.java 增加@EnableEurekaClient,如果是其他注册中心可以使用注解@EnableDiscoveryClient来进行服务的注册

package com.springcloud.producer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class ProducerApplication {

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

}
COPY
4. Controller
package com.springcloud.producer.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**

  • Created with IntelliJ IDEA.
  • @Date: 2019/7/2
  • @Time: 0:02
  • @email: inwsy@hotmail.com
  • Description:
    */
    @RestController
    public class HelloController {
    @RequestMapping("/hello")
    public String hello(@RequestParam String name) {
    return “hello “+name+”,producer is ready”;
    }
    }
    COPY
    先在可以先启动上一篇当中单机版的Eureka,再启动我们刚写好的producer服务提供者,启动成功后,访问链接http://localhost:8761/,可以看到我们的的服务提供者producer已经成功注册在注册中心上了。
    在这里插入图片描述

至此,服务的提供者已经配置完成。

  1. 服务消费者
  2. pom.xml
<?xml version="1.0" encoding="UTF-8"?>


4.0.0

org.springframework.boot
spring-boot-starter-parent
2.1.6.RELEASE


com.springcloud
consumers
0.0.1-SNAPSHOT
consumers
Demo project for Spring Boot

<properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>

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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
COPY spring-boot-starter-web: 这个包是通用的web开发包,里面包含了spring-web、spring-webmvc等包

spring-cloud-starter-openfeign: 这个包是springcloud对于Feign的封装,Feign是一个声明式的Web服务客户端。它支持Feign本身的注解、JAX-RS注解以及SpringMVC的注解。Spring Cloud集成Ribbon和Eureka以在使用Feign时提供负载均衡的http客户端。

  1. 配置文件application.yml
    server:
    port: 8081
    spring:
    application:
    name: spring-cloud-consumers
    eureka:
    client:
    service-url:
    defaultZone: http://localhost:8761/eureka/COPY
  2. 启动类ConsumersApplication.java
    同上,增加@EnableEurekaClient,如果是其他注册中心可以使用注解@EnableDiscoveryClient来进行服务的注册

package com.springcloud.consumers;

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

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class ConsumersApplication {

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

}
COPY
@EnableFeignClients: 这个注解是通知SpringBoot在启动的时候,扫描被 @FeignClient 修饰的类,@FeignClient这个注解在进行远程调用的时候会用到。

  1. Feign远程调用
    Feign是一个声明式Web Service客户端。使用Feign能让编写Web Service客户端更加简单, 它的使用方法是定义一个接口,然后在上面添加注解,同时也支持JAX-RS标准的注解。Feign也支持可拔插式的编码器和解码器。Spring Cloud对Feign进行了封装,使其支持了Spring MVC标准注解和HttpMessageConverters。Feign可以与Eureka和Ribbon组合使用以支持负载均衡。

创建一个remote接口

package com.springcloud.consumers.remote;

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

/**

  • @Author: shiyao.wei
  • @Date: 2019/7/2 11:14
  • @Version: 1.0
  • @Desc:
    */
    @FeignClient(name= “spring-cloud-producer”)
    public interface HelloRemote {
    @RequestMapping(value = “/hello”)
    String hello(@RequestParam(value = “name”) String name);
    }COPY
    name:远程服务名,及spring.application.name配置的名称
    此类中的方法和远程服务中contoller中的方法名和参数需保持一致
  1. web层调用远程接口 Controller
    package com.springcloud.consumers.controller;

import com.springcloud.consumers.remote.HelloRemote;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**

  • @Author: shiyao.wei

  • @Date: 2019/7/2 11:25

  • @Version: 1.0

  • @Desc:
    */
    @RestController
    public class HelloController {
    @Autowired
    HelloRemote helloRemote;

    @RequestMapping("/hello/{name}")
    public String index(@PathVariable(“name”) String name) {
    return helloRemote.hello(name);
    }
    }
    COPY
    现在,一个最简单的服务注册和调用的例子就完成了。

  1. 测试
    简单调用
    顺次启动eureka、producer、consumer三个项目

启动成功后,先在浏览器输入http://localhost:8080/hello?name=springcloud

可以看到页面显示:hello springcloud,producer is ready

证明我们的producer已经正常启动,提供的服务也正常

接下来,我们测试服务消费者,在浏览器中输入:http://localhost:8081/hello/spring

可以看到页面显示:hello spring,producer is ready

说明客户端已经成功的通过feign调用了远程服务hello,并且将结果返回到了浏览器。

负载均衡
将上面的producer复制一份,修改名称为producer2,修改pom.xml中的\为producer2,修改其中的Controller:

package com.springcloud.producer.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**

  • Created with IntelliJ IDEA.
  • @Date: 2019/7/2
  • @Time: 0:02
  • @email: inwsy@hotmail.com
  • Description:
    */
    @RestController
    public class HelloController {
    @RequestMapping("/hello")
    public String hello(@RequestParam String name) {
    return “hello “+name+”,producer2 is ready”;
    }
    }COPY
    修改application.yml配置文件启动端口为8082

启动我们刚复制好的producer2,这时可以看一下注册中心Eureka,我们现在已经有两个producer服务了。

在这里插入图片描述

这时我们再去访问:http://localhost:8081/hello/spring

第一次返回结果:hello spring,producer is ready

第二次返回结果:hello spring,producer2 is ready

连续刷新页面,两个结果会交替出现,说明注册中心提供了服务负载均衡功能。将服务数提高到N个,会发现测试结果一样,请求会自动轮询到每个服务端来处理。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值