Feign負載均衡(客戶端)
阿新 • • 發佈:2018-08-14
sof work autowire lns string 案例 創建app ice instance
Feign是一個聲明式的Web Service客戶端,比Ribbon好用,默認也是輪巡。我們只需要使用Feign創建一個接口,並用註解就好了。
案例編寫:
一:搭建Eureka服務器
目錄結構:
1.1導入依賴包
<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> <groupId>org.springCloud</groupId> <artifactId>first-server</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- 基於springboot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.8.RELEASE</version> <relativePath/> </parent> <!-- springcloud依賴 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR3</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <!-- eureka服務器依賴 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies> </project>
1.2編寫配配置文件 在resource目錄下創建application.yml文件
# 指定默認端口
server:
port: 8761
# eureka默認把自己到服務器註冊會報錯,這裏禁止掉
eureka:
client:
register-with-eureka: false
fetch-registry: false
1.3編寫啟動類
package org.wulei; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication//表示這是一個springboot應用 @EnableEurekaServer// 申明這是一個eureka服務器 public class ServerApp { public static void main(String[] args) { SpringApplication.run(ServerApp.class, args); //new SpringApplicationBuilder(ServerApp.class).web(true).run(args); } }
這時,瀏覽器訪問localhost:8761會進入eureka管控臺,出現這個頁面就表示啟動成功了,但這個時候還沒有服務註冊上來,下面我們繼續編寫服務端與客戶端。
二:編寫服務提供者
目錄結構:
2.1 導入依賴包
<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>
<groupId>org.springCloud</groupId>
<artifactId>spring_feign_police</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 基於springboot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath/>
</parent>
<!-- springcloud -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR3</version>
<type>pom</type>
<scope>import </scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- eureka客戶端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
</project>
2.2 編寫appliaction.yml配置文件
# 設置服務名稱
spring:
application:
name: police
# 設置eureka服務器的註冊地址
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
2.3 編寫實體類,供接口返回json字符串使用。
public class Police {
private Integer id;
private String name;
//記錄訪問路徑用
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
2.4 Controller層
import javax.servlet.http.HttpServletRequest;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class PoliceController {
@RequestMapping(value = "/call/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public Police call(@PathVariable Integer id,HttpServletRequest req) {
Police p = new Police();
p.setId(id);
p.setName("吳磊");
p.setMessage(req.getRequestURL().toString());
return p;
}
@RequestMapping("/hello/{name}")
public String hello(@PathVariable String name){
return "hello"+name;
}
}
2.5 編寫啟動類
import java.util.Scanner;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient//開啟eureka服務
public class PoliceServer {
public static void main(String[] args) {
//new SpringApplicationBuilder(PoliceServer.class).web(true).run(args);
//1. 啟動main方法後,在控制臺輸入端口號.
Scanner scan = new Scanner(System.in);
String port = scan.nextLine();
//以輸入的端口號啟動
new SpringApplicationBuilder(PoliceServer.class)
.properties("server.port="+port).run(args);
//2. 啟動2次,分別以8080, 8081啟動,瀏覽器訪問測試一下。
}
}
分別訪問8080和8081端口進行測試, 刷新eureka管控臺,這裏看到我們的服務已經註冊上去了。
三:編寫服務調用者
目錄結構:
3.1 導入依賴包
<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>
<groupId>org.crazyit.cloud</groupId>
<artifactId>spring_feign_person</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 基於springboot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath/>
</parent>
<!-- springCloud -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- eureka客戶端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- feign客戶端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
</dependencies>
</project>
3.2 編寫application.yml配置信息
# 指定端口
server:
port: 9000
# 服務名稱
spring:
application:
name: invoker
# eureka服務器註冊地址
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
3.3 我們要調用服務提供者的 host:port/call/{ id } 接口返回實體類信息,所以把之前的實體復制過來,減少系統耦合度。
public class Police {
private Integer id;
private String name;
private String message;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
3.4 用feign註解來遠程調用
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
//指定服務提供者的服務名
@FeignClient("police")
public interface HelloClient {
//翻譯服務提供者的地址
@RequestMapping(method = RequestMethod.GET, value="/hello/{name}")
String hello(@PathVariable("name") String name);
//翻譯服務提供者的地址
@RequestMapping(method = RequestMethod.GET, value="/call/{id}")
Police getPolice(@PathVariable("id") Integer id);
}
3.5 編寫controller
package org.wulei;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
//把剛才的feign註解類依賴過來
@Autowired
private HelloClient helloClient;
@RequestMapping(method = RequestMethod.GET, value="/router")
public String router() {
String result = helloClient.hello("angus");
return result;
}
@RequestMapping(method = RequestMethod.GET, value="/police",
produces = MediaType.APPLICATION_JSON_VALUE)
public Police getPolice() {
Police p = helloClient.getPolice(1);
return p;
}
}
3.6 編寫主函數入口
@SpringBootApplication
@EnableEurekaClient//開啟eureka
@EnableFeignClients//開啟feign
public class ServerMain {
public static void main(String[] args) {
new SpringApplicationBuilder(ServerMain.class).web(true).run(args);
}
}
3.7 測試 我們調用消費者的接口,實際是用feign遠程調用的服務提供者。刷新eureka管控臺,可以看到消費者註冊成功了。
Feign負載均衡(客戶端)