转载

Spring Cloud Eureka Client及OpenFeign使用(Greenwich版本)

一·概述

在上一个篇幅中,介绍了Spring Cloud Eureka服务的使用。本文将介绍如何如何使用Eureka Client来注册服务,并如何使用OpenFeigh来调用对应的注册服务。

二·Eureka Client

Eureka Client简单来说,就是一个提供服务,并注册到Cureka Server仓库上的一个组件。

2.1 快速使用

创建Eureka Client,需要做如下准备工作:

spring-cloud-starter-netflix-eureka-client
@EnableEurekaClient

简单地HTTP REST服务的配置文件实例如下:

<?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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.netease.nis.cloud</groupId>
    <artifactId>consumer-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>consumer-client</name>
    <description>Demo project for Spring Boot</description>

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

</project>

比如,我们在一台机器上启动三个Eureka Client实例,分别绑定8881,8882,8883三个不同的端口,启动后这三个实例都会注册到Eureka Server上,那么application.yml的配置信息如下:

spring:
  application:
    name: spring-cloud-eureka-client

eureka:
  client:
    serviceUrl:
      defaultZone: http://netease-dev.com:9801/eureka/,http://netease-dev.com:9802/eureka/,http://netease-dev.com:9803/eureka/

---

spring:
  profiles: client1

server:
  port: 8881

---
spring:
  profiles: client2

server:
  port: 8882

---
spring:
  profiles: client3

server:
  port: 8883

eureka:
  instance:
    hostname: peer3.com
---

然后,我们就分别启动三个不同端口上的实例:

java -jar eureka-client-0.0.1-SNAPSHOT.jar --spring.profiles.active=client1
java -jar eureka-client-0.0.1-SNAPSHOT.jar --spring.profiles.active=client2
java -jar eureka-client-0.0.1-SNAPSHOT.jar --spring.profiles.active=client3

从下图中,我们就可以看出已经注册了三个实例了。

Spring Cloud Eureka Client及OpenFeign使用(Greenwich版本)

Eureka Client注册服务

三·OpenFeigh

OpenFeigh是一个注解声明式的web服务客户端,它使得调动注册REST服务更加方便。我们通过简单地在接口类上注解就可以轻松达到远程调用的目的,而且它还支持自定义的解码和编码器。

实际上,OpenFeigh本身也集成了Ribbon和Eureka,具有负载均衡的效果。

3.1 快速使用

使用OpenFeigh,需要做如下的准备工作:

spring-cloud-starter-openfeign
@EnableFeignClients
@FeignClient

以下是一个配置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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.netease.nis.cloud</groupId>
    <artifactId>comsumer-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>comsumer-client</name>
    <description>Demo project for Spring Boot</description>

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

</project>

接下来配置一下程序的参数(application.yml),其实大部分参数和Client区别不大:

eureka:
  client:
    registerWithEureka: false
#    fetchRegistry: false
    serviceUrl:
      defaultZone: http://netease-dev.com:9801/eureka/,http://netease-dev.com:9802/eureka/,http://netease-dev.com:9803/eureka/

server:
  port: 9000
spring:
  application:
    name: consumer-feign-client

为了调用之前写的注册微服务,我们需要编写一个接口,并添加注解,如下所示:

// GreetingClient.java

@FeignClient("spring-cloud-eureka-client")
public interface GreetingClient {
    @RequestMapping("/hello")
    String hello();

    @RequestMapping("/good")
    String good();
}

以上定义了2个接口,调用的注册服务名为spring-cloud-eureka-client,内部声明了两个接口,分别调用/hello和/good。然后我们在Controller中就可以使用了,如下:

@SpringBootApplication
@EnableFeignClients
@RestController
public class ComsumerClientApplication {
    @Autowired
    private GreetingClient greetingClient;

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

    @RequestMapping("/hello2")
    public String hello2() {
        String text = String.format("enhanced - %s", greetingClient.hello());
        System.out.println(text);
        return text;
    }

    @RequestMapping("/good2")
    public String good2() {
        String text = String.format("enhanced - %s", greetingClient.good());
        System.out.println(text);
        return text;
    }
}

配置比较简单,我们用浏览器打开:

调用微服务的结果

Spring Cloud Eureka Client及OpenFeign使用(Greenwich版本)

多刷新几次,然后可以看下日志,三个实例都有被调用,策略就是轮训的方式,达到负载的效果。

Eureka Client被调用的日志

Spring Cloud Eureka Client及OpenFeign使用(Greenwich版本)

3.2 断路器Hystrix

OpenFeigh 是支持断路器的,只需要确保Hystrix添加进来,并打开开关 feign.hystrix.enabled=true 即可。断路器的更多内容可以参考以下官网链接:

原文  http://www.easysb.cn/2019/06/421.html
正文到此结束
Loading...