1.首先实现server项目,提供数据接口
pom.xml
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.3.1.RELEASE
com.frank
feign-server
0.0.1-SNAPSHOT
feign-server
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-maven-plugin
application.properties
server.port=8081
spring.application.name=user-service
server.servlet.context-path=/api
user
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 getSuccessInfo(){
Map map = new HashMap<>();
map.put("code", 200);
map.put("msg", "success");
map.put("data", new User(1,"小石潭记",26));
return map;
}
@GetMapping("/getFailInfo")
public Map getFailInfo(){
Map 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
服务端完成成功。
*********feign客户端*********
pom.xml
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.3.1.RELEASE
com.frank
feign-client
0.0.1-SNAPSHOT
feign-client
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-openfeign
2.0.2.RELEASE
org.springframework.cloud
spring-cloud-starter-netflix-hystrix
2.0.2.RELEASE
org.springframework.cloud
spring-cloud-netflix-hystrix-dashboard
2.0.0.RELEASE
org.springframework.boot
spring-boot-maven-plugin
这里使用的是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
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
关闭服务端:返回熔断里面的内容。
成功获取服务端的数据,搞定!!!