Hrm-人力资源系统开发笔记10

1.提供一个Agent代理类做中间服务

该模块用来专门从文件系统下载静态化页面
pom:

<?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>hrm-page-agent-parent</artifactId>
        <groupId>com.penny</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>hrm-page-agent-service-2040</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- Eureka 客户端依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <!--配置中心支持-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

        <!-- 通过公共rabbitmq的模块引入mq的jar包-->
        <dependency>
            <groupId>com.penny</groupId>
            <artifactId>hrm-basic-rabbitmq</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!--        fastdfsclient支持-->
        <dependency>
            <groupId>com.penny</groupId>
            <artifactId>hrm-common-client</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <!-- 给调用的模块来转换json-->
        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson  调用者需要转换-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.58</version>
        </dependency>
    </dependencies>


</project>

bootstarp配置类:

spring:
  profiles:
    active: dev
  cloud:
    config:
      name: application-page-agent #github上面名称
      profile: ${spring.profiles.active} #环境 java -jar -D xxx jar
      label: master #分支
      discovery:
        enabled: true #从eureka上面找配置服务
        service-id: hrm-config-server #指定服务名
      #uri: http://127.0.0.1:1299 #配置服务器 单机配置
eureka: #eureka不能放到远程配置中
  client:
    service-url:
      defaultZone: http://localhost:1011/eureka  #告诉服务提供者要把服务注册到哪儿 #单机环境
  instance:
    prefer-ip-address: true #显示客户端真实ip
feign:
  hystrix:
    enabled: true #开启熔断支持
  client:
    config:
      remote-service:           #服务名,填写default为所有服务
        connectTimeout: 3000
        readTimeout: 3000
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 3000

application.yml托底类:

spring:
  application:
    name: hrm-page-agent

创建主入口:

package com.penny;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class PageAgentApp2040 {
    public static void main(String[] args) {
        SpringApplication.run(PageAgentApp2040.class, args);
    }
}

配置MQ:

package com.penny.config;

import com.penny.util.RabbitMqConstants;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMqConfig {
    //队列
    public static final String QUEUE_PAGE_STATIC = "queue_page_static";
    //交换机名字
    public static final String EXCHANGE_TOPICS_PAGE = RabbitMqConstants.EXCHANGE_TOPICS_PAGE;

    @Value("${rabbitmq.routingKey}")
    private String routingKey;

    /**
     * 交换机配置
     * ExchangeBuilder提供了fanout、direct、topic、header交换机类型的配置
     *
     * @return the exchange
     */
    @Bean(EXCHANGE_TOPICS_PAGE) //spring中bean
    public Exchange EXCHANGE_TOPICS_INFORM() {
        //durable(true)持久化,消息队列重启后交换机仍然存在
        return ExchangeBuilder.topicExchange(EXCHANGE_TOPICS_PAGE).durable(true).build();
    }


    //声明队列
    @Bean(QUEUE_PAGE_STATIC)
    public Queue QUEUE_INFORM_SMS() {
        Queue queue = new Queue(QUEUE_PAGE_STATIC);
        return queue;
    }



    /**
     *
     * @param queue    the queue
     * @param exchange the exchange
     * @return the binding
     */
    @Bean
    public Binding BINDING_QUEUE_INFORM_SMS(@Qualifier(QUEUE_PAGE_STATIC) Queue queue, //通过名字从spring获取bean
                                            @Qualifier(EXCHANGE_TOPICS_PAGE) Exchange exchange) {
        //每个站点的routing可以是不一样的
        System.out.println("KEY是:"+routingKey);
        return BindingBuilder.bind(queue).to(exchange).with(routingKey).noargs();
    }

}

配置静态化处理类(fds):

package com.penny.handle;

import com.alibaba.fastjson.JSONObject;
import com.penny.client.FastDfsClient;
import com.penny.util.RabbitMqConstants;
import com.rabbitmq.client.Channel;
import feign.Response;
import org.apache.commons.io.IOUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

@Component
public class StaticPageHandler {
    @Autowired
    private FastDfsClient fastDfsClient;

    @RabbitListener
    public void handle(String msg, Message message, Channel channel){
        System.out.println("系统开始接收消息:"+msg);
        Map map = JSONObject.parseObject(msg, Map.class);
        //获取文件类型
        Integer fileSysType = (Integer) map.get(RabbitMqConstants.FILE_SYS_TYPE);
        //获取文件地址
        String pageUrl = (String) map.get(RabbitMqConstants.PAGE_URL);
        //获取真实地址
        String physicalPath = (String) map.get(RabbitMqConstants.PHYSICAL_PATH);
        //判断文件系统类型,分别作处理
        switch (fileSysType){
            case 0:
                //使用fdfs做处理
                downloadAndCopyOfFastDfs(pageUrl,physicalPath);
                break;
            case 1:
                //使用hdfs做处理
                downloadAndCopyOfHdfs(pageUrl,physicalPath);
                break;
        }
    }

    private void downloadAndCopyOfHdfs(String pageUrl, String physicalPath) {

    }

    private void downloadAndCopyOfFastDfs(String pageUrl, String physicalPath) {
        InputStream is =null;
        FileOutputStream fos = null;
        try {
            Response download = fastDfsClient.download(pageUrl);
            //转换成输入流
            is = download.body().asInputStream();
            //放入特定文件
            System.out.println(physicalPath);
            fos = new FileOutputStream(physicalPath);
            IOUtils.copy(is,fos);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值