美文网首页
7、data-business

7、data-business

作者: knock | 来源:发表于2020-07-13 00:30 被阅读0次

模块结构

image.png

模块代码

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;
            }
        };
    }
}

相关文章

  • 7、data-business

    模块结构 模块代码 pom.xml javaRequestAgent.java RequestFactory.java

  • 【第三周检视】2017.06.04—2017.06.10

    一、一周好习惯 早起7∕7,早睡7∕7,走步6∕7,冥想7∕7,八段锦5∕7,群里打卡7∕7,英语7∕7,读书群7...

  • 易效能时间管理8组小组会议第十周

    打卡统计:许丽钦 7/7;刘佼佼7/7;佟创5/7;万森7/7;乐天6/7;绍斌4/7;黎钦财7/7; 本周学习收...

  • 7/7

    我不能夸口说精神分析从来不做琐碎的事情,事实恰恰相反,精神分析所观察的材料常被其他科学讥讽为是琐碎、平凡和不重要的...

  • 7、7

    改变能改变的 接受不能改变的 颓废也是一种精神状态 乐于接受也不能不说是一种享受 哈哈、还年轻、怕什么

  • 7/7

    周五了,明天终于不用早起了。明天要去看爸妈,要去图书馆还书,要去新家。

  • 7/7

    无人与我立黄昏 无人问我粥可温 无人与我捻熄灯 无人共我书半生 无人陪我夜已深 无人与我把酒分 无人拭我相思泪 无...

  • 7/7

    数学 看到多元函数 直接懵逼 计算量好大 要多练,之前的也需要总结,全书第一轮还是很吃力。 专业课 越来越喜欢了 没道理

  • 7/7

    高情商并不是左右逢源、圆滑客套,而是温暖、真诚,换位思考。找台阶下,让人舒适体面。 不随意评价他人,发个自拍,他说...

  • 7/7

    因为疫情今年的高考在今天拉开了大幕,作为经历过两次高考的人。此刻内心毫无波澜,眼前的生活早已把我蹂躏的不再...

网友评论

      本文标题:7、data-business

      本文链接:https://www.haomeiwen.com/subject/jwjocktx.html