本篇是以上一篇为基础。本文主要介绍Spring Cloud Feign声明式服务调用。为啥不介绍ribbon和hystrix。主要是因为Feign本身就以ribbon和hystrix为基础实现的,并且在真正的工作过程中基本上都是基于Spring Cloud Feign。所以Spring Cloud系列就直接跳过绍ribbon和hystrix。
Feign集成了ribbon和hystrix,所以Feign具有客户端负载均衡和服务容错得功能。所以在部分服务出现宕机得时候,正常得业务不受影响正常运行。直接上项目代码
1.在上篇基础之上咱们要在上一篇实现得outp-cis-service做出简单得修改,只需要修改TestController.java
package com.goodluck;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
* Created by 76706 on 2020/6/18.
*/
@RestController
public class TestController {
@Autowired
private TestService testService;
@RequestMapping("/test")
public String getId(){
return testService.getIdd()+"";
}
Logger logger= LoggerFactory.getLogger(getClass());
@RequestMapping("/hello")
public String hello(){
// ServiceInstance instance = client.getLocalServiceInstance();
return "hello2";
}
@RequestMapping(value = "/hello1",method = RequestMethod.GET)
public String hello(@RequestParam String name){
// ServiceInstance instance = client.getLocalServiceInstance();
return "hello2"+name;
}
@RequestMapping(value = "/hello2",method = RequestMethod.GET)
public User hello(@RequestHeader String name, @RequestHeader Integer age){
// ServiceInstance instance = client.getLocalServiceInstance();
return new User(name,age);
}
@RequestMapping(value = "/hello3",method = RequestMethod.POST)
public String hello(@RequestBody User user){
// ServiceInstance instance = client.getLocalServiceInstance();
return "hello2"+user.getName()+","+user.getAge();
}
}
新建Feign项目结构如下
1.项目结构如下
2. TestController.java
package com.goodluck;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
* Created by 76706 on 2020/6/18.
*/
@RestController
public class TestController {
@Autowired
private TestService testService;
@Autowired
HelloService helloService;
@RequestMapping("/test")
public String getId(){
return testService.getIdd()+"";
}
@RequestMapping(value="/hello",method = RequestMethod.GET)
public String hello(){
StringBuilder sb =new StringBuilder();
sb.append(helloService.hello()).append("\n");
return sb.toString();
}
@RequestMapping(value="/feign-consumer",method = RequestMethod.GET)
public String helloConsumer(){
StringBuilder sb =new StringBuilder();
//sb.append(helloService.hello()).append("\n");
//sb.append(helloService.hello("DIDI")).append("\n");
//sb.append(helloService.hello("DIDI",30)).append("\n");
sb.append(helloService.hello(new User("DIDI",30))).append("\n");
return sb.toString();
}
@RequestMapping(value = "/error",method = RequestMethod.POST)
public String error(){
// ServiceInstance instance = client.getLocalServiceInstance();
return "error";
}
}
3.TestDao.java
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
/**
* Created by 76706 on 2020/6/18.
*/
@Mapper
public interface TestDao {
/**
* 测试连接
*/
@Select(" select name from sstable ")
String testSqlConnent();
}
4.TestService.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* Created by 76706 on 2020/6/18.
*/
@Service
public class TestService {
@Autowired
private TestDao testDao;
public String getIdd(){
return testDao.testSqlConnent();
}
}
5.User.java
/**
* Created by Administrator on 2020-05-19.
*/
public class User {
private String name;
private Integer age;
public User() {
}
public User(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
6.HelloService.java
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;
/**
* Created by Administrator on 2020-05-19.
*/
@FeignClient(value = "outp-cis-service",fallback = HelloServiceFallback.class)
@Service
public interface HelloService {
@RequestMapping("/hello")
String hello();
@RequestMapping(value="/hello1",method = RequestMethod.GET)
String hello(@RequestParam("name") String name);
@RequestMapping(value="/hello2",method = RequestMethod.GET)
User hello(@RequestHeader("name") String name, @RequestHeader("age") Integer age);
@RequestMapping(value="/hello3",method = RequestMethod.POST)
String hello(@RequestBody User user);
}
7.HelloServiceFallback.java
import org.springframework.stereotype.Component;
/**
* Created by Administrator on 2020-05-26.
*/
@Component
public class HelloServiceFallback implements HelloService {
@Override
public String hello() {
return "nihao";
}
@Override
public String hello(String name) {
return "nihao";
}
@Override
public User hello(String name, Integer age) {
return new User("nihao",0);
}
@Override
public String hello(User user) {
return "nihao";
}
}
8.OutpCisApiApplication.java
package com.goodluck;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class OutpCisApiApplication {
public static void main(String[] args) {
SpringApplication.run(OutpCisApiApplication.class, args);
}
}
9.启动outp-cis-service和outp-cisa-api。两个项目。刷新http://localhost:1111/。可以看到两个服务已经注册成功
10. 浏览器中测试接口。输入:http://localhost:9001/feign-consumer。看到如下图表示成功完成项目搭建
11.将 outp-cis-service服务关闭。刷新http://localhost:9001/feign-consumer。可以看到Feign在本服务内做了容错。防止访问量堆积导致的系统瘫痪。