模块结构

模块代码
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud</artifactId>
<groupId>com.yyd.cloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>data-business</artifactId>
<!-- feign 客户端 在data-consumer配置中开启降级服务-->
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
<!--拷贝依赖jar到lib-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<!--拷贝target下的jar到targets-->
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>copy-lib-target</id>
<phase>package</phase>
<configuration>
<tasks>
<copy todir="../targets">
<fileset dir="target">
<include name="*.jar"/>
</fileset>
</copy>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
java
RequestAgent.java
package com.yyd.bussiness.agent;
import com.yyd.bussiness.factory.RequestFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
/***
* @ClassName: RequestAgent
* @Description: TODO
* @author: yanyd
* @Date: 4:44 2020/5/31
* @version : V1.0
*/
@Component
@FeignClient(value = "data-provide",fallbackFactory = RequestFactory.class)
//@FeignClient(name = "MyClientService",url = "localhost:8080")//name 为注册提供者的服务名或者客户端名称
public interface RequestAgent {
@PostMapping(value = "/request/data", produces = {"application/json;charset=utf-8"})
Map requestPost(@RequestBody Map map);
@GetMapping(value = "/request/data/{param}")
Map requestGet(@PathVariable("param") String param);
}
RequestFactory.java
package com.yyd.bussiness.factory;
import com.yyd.bussiness.agent.RequestAgent;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
/***
* @ClassName: RequestFactory
* @Description: TODO
* @author: yanyd
* @Date: 4:45 2020/5/31
* @version : V1.0
*/
@Component
public class RequestFactory implements FallbackFactory<RequestAgent> {
@Override
public RequestAgent create(Throwable throwable) {
throwable.printStackTrace();
return new RequestAgent() {
@Override
public Map requestPost(Map map) {
map.put("msg","fegin 降级服务,post请求失败...");
return map;
}
@Override
public Map requestGet(String param) {
HashMap<String,Object> map=new HashMap<>();
map.put("msg","fegin 降级服务,get请求失败...");
map.put("param",param);
return map;
}
};
}
}
网友评论