1. 程式人生 > 實用技巧 >spring cloud openfeign 使用記錄1

spring cloud openfeign 使用記錄1

1、背景

使用 zk 作為註冊中心,使用 Spring cloud openfeign 進行遠端呼叫

2、步驟

2.1 service-provider

1.Pom.xml

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

        <!-- Eureka 服務發現與註冊客戶端依賴 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!-- Zookeeper 服務發現與註冊客戶端依賴 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
        </dependency>
</dependencies>

2. Controller

@RestController
public class EchoSerivceController {

    private final Environment environment;

    public EchoSerivceController(Environment environment) {
        this.environment = environment;
    }

    //獲取 隨機 埠的 方法
    private String getPort() {
        return environment.getProperty("local.server.port"); //依賴注入時 動態 注入
    }

    @GetMapping("/echo/{message}")
    public String echo(@PathVariable String message) {
        return "[ECHO: "+ getPort() + " ] : " + message;
    }
}

3 properties

spring:
  application:
    name: service-provider
  cloud:
    zookeeper:
      enabled: false # Zookeeper 服務發現與註冊失效(預設)

server:
  port: 0 #隨機埠

## 預設 profile 關閉自動特性,
eureka:
  client:
    enabled: false # Eureka 服務發現與註冊失效(預設)

--- # Profile For Eureka
spring:
  profiles: eureka
# Eureka 客戶端配置
eureka:
  server: # 官方不存在的配置(自定義配置)
    host: 127.0.0.1
    port: 12345
  client:
    enabled: true
    serviceUrl:
      defaultZone: http://${eureka.server.host}:${eureka.server.port}/eureka
    registryFetchIntervalSeconds: 5 # 5 秒輪訓一次
  instance:  # 自定義 instanceId
    instanceId: ${spring.application.name}:${server.port}

--- # Profile For Zookeeper
spring:
  profiles: zookeeper
  cloud:
    zookeeper:
      enabled: true
      connectString: 127.0.0.1:2181

專案啟動 新增 引數。--spring.profiles.active=zookeeper, 是 zk 作為 註冊中心失效

2.2 service-client

1.pom.xml

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Eureka 服務發現與註冊客戶端依賴 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!-- Zookeeper 服務發現與註冊客戶端依賴 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
        </dependency>
    </dependencies>

2. 定義 feign介面

/**
 * 定義 feign 介面
 */
@FeignClient("service-provider") //應用(服務)名稱, 否則 ClientException: Load balancer does not have available server for client: echoServiceClient
public interface EchoServiceClient {

  //同 service-provider controller
    @GetMapping("/echo/{message}")
    public String echo(@PathVariable String message);
}

3. 定義client controller

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients  //掃描同包 package  使@FeignClient生效
@RestController
public class EchoServiceClientBootstrap {

    @Autowired
    private EchoServiceClient echoServiceClient;

    @GetMapping(value = "/call/echo/{message}")
    public String echo(@PathVariable String message) {

        return echoServiceClient.echo(message);
    }

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

3 測試

前提: 啟動 zk

專案啟動後,能在 zk 中看到 兩個模組

[zk: localhost:2181(CONNECTED) 4] ls /services/service-
service-client     service-provider   

異常一

Field echoServiceClient in com.gupaoedu.client.EchoServiceClientBootstrap required a bean of type 'com.gupaoedu.client.EchoServiceClient' that could not be found.

The injection point has the following annotations:
	- @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.gupaoedu.client.EchoServiceClient' in your configuration.

沒有新增。@EnableFeignClients

異常二

[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.RuntimeException: com.netflix.client.ClientException: Load balancer does not have available server for client: echoServiceClient] with root cause

com.netflix.client.ClientException: Load balancer does not have available server for client: echoServiceClient

@FeignClient("service-provider"). Value 不是 生產這的 application.name. 通過 spring.application.name 進行 例項的獲取

總結

1、所有的學習,從記錄開始; 所有的原理,從 使用開始

2、spring cloud feign的使用過程中,涉及很多細節: Enable 驅動模組、負載均衡實現等,後續 充實,聽的時候 似懂非懂,涉及的基礎太多了

參考:

小馬哥視訊