springCloud踩坑系列-Eureka-使用Feign进行服务调用

创建消费端/服务端

微服务的特点即一个服务即是服务端也是消费端,我们在springCloud踩坑系列-Eureka-单节点服务端一文中创建了父工程spring-cloud,现在我们在父工程下创建一个子项目:eureka-client1.其pom文件如下:


<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.0modelVersion>

    <groupId>com.vj.studygroupId>
    <artifactId>eureka-client1artifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>

    <name>eureka-client1name>
    <description>eurekaclient project for Spring Bootdescription>

    <parent>
        <artifactId>spring-cloudartifactId>
        <groupId>com.vj.studygroupId>
        <version>0.0.1-SNAPSHOTversion>
    parent>


    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-openfeignartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>

,新建java class: eureka-client1.src.main.java.com.vj.study.eurekaclient1.EurekaClientApplication.java
springCloud踩坑系列-Eureka-使用Feign进行服务调用_第1张图片
代码为:

package com.vj.study.eurekaclient1;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class EurekaClientApplication {

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

    @FeignClient(name = "client2")
    interface FeignClientSecond {
        @RequestMapping("/ClientSecond/greet")
        String greet();
    }

    @RestController
    @RequestMapping("/ClientFirst")
    class ClientFirstController{
        @Autowired
        private FeignClientSecond feignClientSecond;

        @RequestMapping("/greet")
        public String greet(){
            return "hello,I`m ClientFirst, from ClientFirst";
        }

        @RequestMapping("/clientSecondGreet")
        public String clientSecondGreet(){
            return feignClientSecond.greet();
        }


    }


}

复制eureka-client1为eureka-client2,eureka-client1-2,我们设置client1和client1-2提供相同的服务clientFirst,client2为单独的服务clientSecond.模拟服务调用时的负载均衡.在clientSecond里面调用clientFirst的服务,看结果是否来自client-1和client1-2,在client-1和client1-2中调用clientSecond,模拟当高可用的某个服务挂掉后是否其它节点能继续提供服务.

  • eureka-client1-2模块中的pom文件:

<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.0modelVersion>

    <groupId>com.vj.studygroupId>
    <artifactId>eureka-client1-2artifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>

    <name>eureka-client1-2name>
    <description>eurekaclient project for Spring Bootdescription>

    <parent>
        <artifactId>spring-cloudartifactId>
        <groupId>com.vj.studygroupId>
        <version>0.0.1-SNAPSHOTversion>
    parent>


    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-openfeignartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>

  • eureka-client1-2模块中的yml配置文件:
server:
  port: 6033
spring:
  application:
    name: client1

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    serviceUrl:
      defaultZone: http://server1:6021/eureka/,http://server2:6022/eureka/,http://server3:6033/eureka/    # 指定注册中心的地址
  instance:
    prefer-ip-address: false
ribbon:
  eureka:
    enabled: true


  • eureka-client1-2模块中的EurekaClientApplication.java:
package com.vj.study.eurekaclient3;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class EurekaClientApplication {

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

    @FeignClient(name = "client2")
    interface FeignClientSecond {
        @RequestMapping("/ClientSecond/greet")
        String greet();
    }

    @RestController
    @RequestMapping("/ClientFirst")
    class ClientFirstController{
        @Autowired
        private FeignClientSecond feignClientSecond;

        @RequestMapping("/greet")
        public String greet(){
            return "hello,I`m ClientFirst, from ClientThird";
        }

        @RequestMapping("/clientSecondGreet")
        public String clientSecondGreet(){
            return feignClientSecond.greet();
        }


    }
}

可以看到,eureka-client1-2模块中除了端口和模块名和eureka-client1不一样之外其它都是一样的,因为这里我们将它和eureka-client1作为多节点部署.

  • eureka-client2模块中的pom文件:

<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.0modelVersion>

    <groupId>com.vj.studygroupId>
    <artifactId>eureka-client2artifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>

    <name>eureka-client2name>
    <description>eurekaclient project for Spring Bootdescription>

    <parent>
        <artifactId>spring-cloudartifactId>
        <groupId>com.vj.studygroupId>
        <version>0.0.1-SNAPSHOTversion>
    parent>


    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-openfeignartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>

  • eureka-client2模块中的yml配置文件:
server:
  port: 6032
spring:
  application:
    name: client2

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    serviceUrl:
      defaultZone: http://server1:6021/eureka/,http://server2:6022/eureka/,http://server3:6033/eureka/    # 指定注册中心的地址
  instance:
    prefer-ip-address: false
ribbon:
  eureka:
    enabled: true


  • eureka-client2模块中的EurekaClientApplication.java:
package com.vj.study.eurekaclient2;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class EurekaClientApplication {

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

    @FeignClient(name = "client1")
    interface FeignClientFirst {
        @RequestMapping("/ClientFirst/greet")
        String greet();
    }

    @RestController
    @RequestMapping("/ClientSecond")
    class ClientSecondController{
        @Autowired
        private FeignClientFirst feignClient;

        @RequestMapping("/greet")
        public String greet(){
            return "hello,I`m ClientSecond, from ClientSecond";
        }

        @RequestMapping("/clientFirstGreet")
        public String clientFirstGreet(){
            return feignClient.greet();
        }

    }

}

运行与测试服务

访问服务注册中心,查看服务注册情况

分别运行三个模块中的EurekaClientApplication.java.我们先去eureka服务注册中心看一下现在服务的注册情况.
这里服务中心我采用了改hosts的方式部署,详情见springCloud踩坑系列-Eureka-构建高可用服务注册中心.
访问http://server1:6021/, 界面如下:
springCloud踩坑系列-Eureka-使用Feign进行服务调用_第2张图片

可以看到,我们服务注册中心三个节点正常运行,eureka-client1和eureka-client1-2组成了client1服务,eureka-client2注册成了client2服务,所有服务正常运行,现在我们继续进行服务调用测试.

访问服务
  • 访问http://server1:6031/ClientFirst/greet:
    springCloud踩坑系列-Eureka-使用Feign进行服务调用_第3张图片
  • 访问http://server1:6031/ClientFirst/clientSecondGreet:
    springCloud踩坑系列-Eureka-使用Feign进行服务调用_第4张图片
  • 访问eureka-client1-2下的http://server1:6033/ClientFirst/greet:
    在这里插入图片描述
  • 访问eureka-client1-2下的http://server1:6033/ClientFirst/clientSecondGreet:
    springCloud踩坑系列-Eureka-使用Feign进行服务调用_第5张图片
  • 访问eureka-client2下的http://server3:6032/ClientSecond/greet:
    在这里插入图片描述
测试负载均衡
  • 访问eureka-client2下的http://server3:6032/ClientSecond/clientFirstGreet:
    在这里插入图片描述
    刷新:
    springCloud踩坑系列-Eureka-使用Feign进行服务调用_第6张图片
    说明负载均衡生效.
模拟服务掉线,测试高可用
  • 访问eureka-client1-2下的http://server1:6033/ClientFirst/greet:
    springCloud踩坑系列-Eureka-使用Feign进行服务调用_第7张图片

  • 访问eureka-client2下的http://server3:6032/ClientSecond/clientFirstGreet:
    springCloud踩坑系列-Eureka-使用Feign进行服务调用_第8张图片
    刷新:
    springCloud踩坑系列-Eureka-使用Feign进行服务调用_第9张图片
    ok,高可用测试完成.

结束语

这里只是做了关于eureka服务调用功能的测试,即侧重于调用成功与否,关于feign传参等复杂调用,我们下篇见

你可能感兴趣的:(springCloud,eureka,feign)