Eclipse(STS) 初次搭建Spring Cloud項目之聲明式REST調用+負載均衡實現Feign(四)

1、什麼是Feign

  1. Feign是一個http請求調用的輕量級框架,能夠以Java接口註解的方式調用Http請求,而不用像Java中經過封裝HTTP請求報文的方式直接調用。Feign經過處理註解,將請求模板化,當實際調用的時候,傳入參數,根據參數再應用到請求上,進而轉化成真正的請求,這種請求相對而言比較直觀。
  2. Feign被普遍應用在Spring Cloud 的解決方案中,是學習基於Spring Cloud 微服務架構不可或缺的重要組件。
  3. Feign具備可插拔的註解特性,可以使用Feign註解和JAX-RS註解。
  4. Feign默認集成了Ribbon,並和Eureka結合,默認實現了負載均衡的效果。

2、下面來看看具體實現

首先建立Feign模塊,cloud-demo-feignweb

下面是項目結構spring

pom文件以下

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.hewl</groupId>
    <artifactId>cloud-demo-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>cloud-demo-feign</artifactId>
  <name>cloud-demo-feign</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </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>
  </dependencies>
</project>
複製代碼

application.yml文件以下

server:
 port: 8811 # 你的端口
spring:
  application:
    name: feign
eureka:
  client:
    service-url:
      defaultZone: http://admin:admin@server1:8761/eureka/,http://admin:admin@server2:8762/eureka/
複製代碼

啓動類內容以下,加上@EnableFeignClients註解開啓Feign的功能

package com.hewl;

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 FeignApplication {
	
	public static void main(String[] args) {
		SpringApplication.run(FeignApplication.class, args);
	}
}
複製代碼

定義Feign接口,SchedualCilentName

package com.hewl.feign;

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

@FeignClient(value="service-eureka-client")
public interface SchedualCilentName {
	
	@GetMapping(value = "/test")
	public String testFromClientOne(@RequestParam("name") String name);

}
複製代碼

定義對外的controller

package com.hewl.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hewl.feign.SchedualCilentName;

@RestController
public class TestController {

	@Autowired
	public SchedualCilentName schedualCilentName;
	
	@GetMapping(value="/test")
	public String test (String name) {
		return schedualCilentName.testFromClientOne(name);
	}
}

複製代碼

按順序啓動項目

  1. 啓動eureka-server
  2. 修改端口分別啓動兩次eureka-client
  3. 啓動feign客戶端

訪問localhost:8811/test?name=張三,訪問成功而且已經成功啓動負載均衡

相關文章
相關標籤/搜索