SpringCloud第五篇 feign
1.新建一個專案feign-client
選擇eureka discovery,feign,web三個依賴
2.啟動eureka,兩個eureka client
3.具體的pom.xml
<?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>com.star.guo</groupId>
<artifactId>springcloudversion</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.star.guo</groupId>
<artifactId>feign-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>feign-client</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.M3</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>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
4.核心配置檔案application.yml
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8000/eureka/
server:
port: 8004
spring:
application:
name: feign-client
5.添加註解
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
效果圖如下
6.新增一個Service介面
package com.star.guo.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(value = "EUREKA-CLIENT")
public interface FeignService {
/**
* hello方法
*
* 本例子未使用其他的路徑,如果使用的有配置就行如@GetMapping(value = "/hello")
*
* @param user 使用者名稱稱
* @return String
*/
@GetMapping
String hello(@RequestParam(value = "user") String user);
}
7.新增一個Controller,用於web測試feign呼叫情況
package com.star.guo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.star.guo.service.FeignService;
/**
* 訊息Controller,使用feign測試叢集效果
*
* @author Star.Guo
*
*/
@RestController
public class MessageController {
@Autowired
FeignService feignService;
/**
* Hello方法
*
* @param user 使用者名稱
* @return String
*/
@GetMapping
public String hello(String user) {
return feignService.hello(user);
}
}
8.瀏覽器訪問
http://localhost:8004/?user=yourname
重新整理會交替顯示下面內容則正常
hello yourname current port is:8001
hello yourname current port is:8002