1.最初にサーバープロジェクトを実現し、データインターフェイスを提供します
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 https://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.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.frank</groupId>
<artifactId>feign-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>feign-server</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties
server.port=8081
spring.application.name=user-service
server.servlet.context-path=/api
ユーザー
package com.frank.feignserver;
/**
* @author 小石潭记
* @date 2020/6/22 21:42
* @Description: ${todo}
*/
public class User {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public User(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public User() {
}
}
ServerController
package com.frank.feignserver;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
/**
* @author 小石潭记
* @date 2020/6/22 21:40
* @Description: ${todo}
*/
@RestController()
public class ServerController {
@GetMapping("/getSuccessInfo")
public Map<String,Object> getSuccessInfo(){
Map<String,Object> map = new HashMap<>();
map.put("code", 200);
map.put("msg", "success");
map.put("data", new User(1,"小石潭记",26));
return map;
}
@GetMapping("/getFailInfo")
public Map<String,Object> getFailInfo(){
Map<String,Object> map = new HashMap<>();
map.put("code", 404);
map.put("msg", "fail");
map.put("data", null);
return map;
}
}
FeignServerApplication
package com.frank.feignserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class FeignServerApplication {
public static void main(String[] args) {
SpringApplication.run(FeignServerApplication.class, args);
}
}
テストを開始します:(これは、テスト用の固定データの単純な戻り値です)
http:// localhost:8081 / api / getSuccessInfo
http:// localhost:8081 / api / getFailInfo
サーバーは正常に完了しました。
*********偽のクライアント*********
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 https://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.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.frank</groupId>
<artifactId>feign-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>feign-client</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-openfeign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-hystrix -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-hystrix-dashboard</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
ここで使用されているのは、Spring Cloud Starter OpenFeignとspringboot1.xに一貫性がないため、注意が必要です。
application.properties
server.port=8082
spring.application.name=feign-client
# 开启熔断
feign.hystrix.enabled=true
UserFeignClient
package com.frank.feignclient;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @author 小石潭记
* @date 2020/6/22 21:48
* @Description: ${todo}
*/
@FeignClient(
//服务名
name = "user-service",
//服务地址
url = "http://localhost:8081/api",
fallback = UserHihystric.class
)
public interface UserFeignClient {
//对应的服务里的接口地址,及请求方式
@RequestMapping(value = "/getSuccessInfo", method = RequestMethod.GET)
@ResponseBody
String getSuccessInfo();
@RequestMapping(value = "/getFailInfo", method = RequestMethod.GET)
@ResponseBody
String getFailInfo();
}
UserHihystric(服务异常时执行,熔断)
package com.frank.feignclient;
import org.springframework.stereotype.Component;
/**
* @author 小石潭记
* @date 2020/6/23 20:31
* @Description: ${todo}
*/
@Component
public class UserHihystric implements UserFeignClient {
@Override
public String getSuccessInfo(){
return "getSuccessInfo serve is bad.";
}
@Override
public String getFailInfo() {
return "getFailInfo serve is bad.";
}
}
UserController
package com.frank.feignclient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author 小石潭记
* @date 2020/6/20 21:38
* @Description: ${todo}
*/
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserFeignClient userFeignClient;
@GetMapping("/getSuccessInfo")
public Object getSuccessInfo(){
return userFeignClient.getSuccessInfo();
}
@GetMapping("/getFailInfo")
public Object getFailInfo(){
return userFeignClient.getFailInfo();
}
}
MyAdvice(这里暂时可以忽略)
package com.frank.feignclient;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
/**
* @author 小石潭记
* @date 2020/6/22 21:07
* @Description: ${todo}
*/
@ControllerAdvice
public class MyAdvice implements ResponseBodyAdvice<Object> {
/**
* 判断哪些需要拦截 return true 就会执行beforeBodyWrite
*/
@Override
public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) {
return true;
}
@Override
public Object beforeBodyWrite(Object obj, MethodParameter methodParameter, MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
System.out.println("==>beforeBodyWrite:" + obj.toString() + ","
+ methodParameter);
return obj;
}
}
FeignClientApplication
package com.frank.feignclient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
@EnableHystrixDashboard
public class FeignClientApplication {
public static void main(String[] args) {
SpringApplication.run(FeignClientApplication.class, args);
}
}
ここで、@ EnableFeignClientsアノテーションをスタートアップクラスに追加する必要があります
最後にテストを開始します。
http:// localhost:8082 / user / getSuccessInfo
http:// localhost:8082 / user / getFailInfo
サーバーを閉じます。回路ブレーカーの内容に戻ります。
サーバーからデータを正常に取得して完了します。!!