feign starter 使用实例

This commit is contained in:
Administrator
2020-10-30 08:46:35 +08:00
parent 980a0fc2f1
commit 599d6bc70e
40 changed files with 976 additions and 0 deletions

View File

@ -0,0 +1,13 @@
package com.chinaunicom.mall.ebtp.cloud.feign.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableFeignClients
@SpringBootApplication
public class FeignExampleApplication {
public static void main(String[] args) {
SpringApplication.run(FeignExampleApplication.class, args);
}
}

View File

@ -0,0 +1,23 @@
package com.chinaunicom.mall.ebtp.cloud.feign.example.client;
import java.util.List;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.chinaunicom.mall.ebtp.cloud.feign.example.config.ClientConfiguration;
import com.chinaunicom.mall.ebtp.cloud.feign.example.hystrix.JSONPlaceHolderFallback;
import com.chinaunicom.mall.ebtp.cloud.feign.example.model.Post;
@FeignClient(value = "jplaceholder", url = "https://jsonplaceholder.typicode.com/", configuration = ClientConfiguration.class, fallback = JSONPlaceHolderFallback.class)
public interface JSONPlaceHolderClient {
@RequestMapping(method = RequestMethod.GET, value = "/posts")
List<Post> getPosts();
@RequestMapping(method = RequestMethod.GET, value = "/posts/{postId}", produces = "application/json")
Post getPostById(@PathVariable("postId") Long postId);
}

View File

@ -0,0 +1,22 @@
package com.chinaunicom.mall.ebtp.cloud.feign.example.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import feign.Logger;
import feign.codec.ErrorDecoder;
@Configuration
public class ClientConfiguration {
@Bean
public Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
@Bean
public ErrorDecoder errorDecoder() {
return new CustomErrorDecoder();
}
}

View File

@ -0,0 +1,23 @@
package com.chinaunicom.mall.ebtp.cloud.feign.example.config;
import com.chinaunicom.mall.ebtp.cloud.feign.example.exception.BadRequestException;
import com.chinaunicom.mall.ebtp.cloud.feign.example.exception.NotFoundException;
import feign.Response;
import feign.codec.ErrorDecoder;
public class CustomErrorDecoder implements ErrorDecoder {
@Override
public Exception decode(String methodKey, Response response) {
switch (response.status()) {
case 400:
return new BadRequestException();
case 404:
return new NotFoundException();
default:
return new Exception("Generic error");
}
}
}

View File

@ -0,0 +1,22 @@
package com.chinaunicom.mall.ebtp.cloud.feign.example.exception;
import lombok.NoArgsConstructor;
@NoArgsConstructor
public class BadRequestException extends Exception {
private static final long serialVersionUID = 1L;
public BadRequestException(String message) {
super(message);
}
public BadRequestException(Throwable cause) {
super(cause);
}
@Override
public String toString() {
return "BadRequestException: " + getMessage();
}
}

View File

@ -0,0 +1,23 @@
package com.chinaunicom.mall.ebtp.cloud.feign.example.exception;
import lombok.NoArgsConstructor;
@NoArgsConstructor
public class NotFoundException extends Exception {
private static final long serialVersionUID = 1L;
public NotFoundException(String message) {
super(message);
}
public NotFoundException(Throwable cause) {
super(cause);
}
@Override
public String toString() {
return "NotFoundException: " + getMessage();
}
}

View File

@ -0,0 +1,24 @@
package com.chinaunicom.mall.ebtp.cloud.feign.example.hystrix;
import java.util.Collections;
import java.util.List;
import org.springframework.stereotype.Component;
import com.chinaunicom.mall.ebtp.cloud.feign.example.client.JSONPlaceHolderClient;
import com.chinaunicom.mall.ebtp.cloud.feign.example.model.Post;
@Component
public class JSONPlaceHolderFallback implements JSONPlaceHolderClient {
@Override
public List<Post> getPosts() {
return Collections.emptyList();
}
@Override
public Post getPostById(Long postId) {
return null;
}
}

View File

@ -0,0 +1,13 @@
package com.chinaunicom.mall.ebtp.cloud.feign.example.model;
import lombok.Data;
@Data
public class Post {
private String userId;
private Long id;
private String title;
private String body;
}

View File

@ -0,0 +1,13 @@
package com.chinaunicom.mall.ebtp.cloud.feign.example.service;
import java.util.List;
import com.chinaunicom.mall.ebtp.cloud.feign.example.model.Post;
public interface JSONPlaceHolderService {
List<Post> getPosts();
Post getPostById(Long id);
}

View File

@ -0,0 +1,27 @@
package com.chinaunicom.mall.ebtp.cloud.feign.example.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.chinaunicom.mall.ebtp.cloud.feign.example.client.JSONPlaceHolderClient;
import com.chinaunicom.mall.ebtp.cloud.feign.example.model.Post;
import com.chinaunicom.mall.ebtp.cloud.feign.example.service.JSONPlaceHolderService;
@Service
public class JSONPlaceHolderServiceImpl implements JSONPlaceHolderService {
private @Autowired JSONPlaceHolderClient jsonPlaceHolderClient;
@Override
public List<Post> getPosts() {
return jsonPlaceHolderClient.getPosts();
}
@Override
public Post getPostById(Long id) {
return jsonPlaceHolderClient.getPostById(id);
}
}

View File

@ -0,0 +1,6 @@
server:
port: 8762
max-http-header-size: 1000000
app:
id: mall-ebtp-cloud-demo

View File

@ -0,0 +1,10 @@
spring:
profiles:
active: feign
application:
name: mall-ebtp-cloud-demo
logging:
level:
root: info