生产与消费

项目github地址

生产者

1.需求

@RequestMapping(value = "/excel")
public interface ExcelController {

    @GetMapping(value = "/test")
    List<User> testData(@RequestParam("path") String path);

    @GetMapping(value = "/hello")
    String helloWorld();

}

4.在生产中实现
-- 在produce模块

@RestController
@RequestMapping("/excel")
public class ExcelControllerImpl implements ExcelController {

    @Autowired
    private Excel excel;

    @GetMapping("/test")
    @Override
    public List<User> testData(@RequestParam("path") String path) {
        List<User> userList = excel.readExcelFile(path);
        return userList;
    }

    @GetMapping("/hello")
    @Override
    public String helloWorld(){
        return "helloWorld!!!";
    }

}

5.依赖

dependencies {
    compile project (':producer_api')
    compile 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client:2.1.2.RELEASE'
    compile 'org.springframework.cloud:spring-cloud-starter-config:2.1.1.RELEASE'
    compile 'mysql:mysql-connector-java:8.0.16'
    compile 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.0'
    compile 'org.springframework.boot:spring-boot-starter-web:2.1.4.RELEASE'
    compile 'org.springframework.boot:spring-boot-starter-jdbc:2.1.4.RELEASE'
    compile 'org.apache.poi:poi:3.17'
    compile 'org.apache.poi:poi-ooxml:3.17'
    compile 'org.apache.poi:poi-ooxml-schemas:3.17'
    compile 'com.google.code.gson:gson:2.8.5'
}

6.yml


server:
  port: 8001

mybatis:
  type-aliases-package: com.huoli.entity    # 所有Entity别名类所在包
  mapper-locations: classpath:mapper/*Mapper.xml    # mapper映射文件

spring:
  application:
    name: producer      #对外暴露的微服务名字
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver         # mysql驱动包
    url: jdbc:mysql://140.143.242.131:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC&serverTimezone=GMT%2B8
    username: test
    password: test

eureka:          #客户端注册进eureka服务列表内
  client:
    service-url:
      defaultZone: http://localhost:1111/eureka

7.主类

@SpringBootApplication
@EnableDiscoveryClient
@EnableEurekaClient
@MapperScan("com.huoli.mapper")
public class ProducerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProducerApplication.class, args);
    }
}

消费者

1.接口接收方式

@FeignClient(name = "producer")
public interface ExcelControllerClient extends ExcelController {
}

2.实例接收方式

@FeignClient(name = "producer")
public interface ProducerClient {

    @GetMapping(value = "/excel/hello")
    String helloWorld();
}

3.controller

@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private ProducerClient producerClient;

    @Autowired
    private ExcelControllerClient excelControllerClient;

    @GetMapping("/hello")
    public String helloWorld() {
        // return excelControllerClient.helloWorld();
       return producerClient.helloWorld();
    }

    @GetMapping("/read")
    public List<User> readExcel(@RequestParam("path") String path) {
        return excelControllerClient.testData(path);
    }
}

4.yml

server:
  port: 8080

spring:
  application:
    name: excel
  main:
    allow-bean-definition-overriding: true

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:1111/eureka/

5.主类

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ExcelApplication {

    public static void main(String[] args) {
        SpringApplication.run(ExcelApplication.class, args);
    }
}

6.依赖

dependencies {
    compile project (':producer_api')
    compile 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client:2.1.2.RELEASE'
    compile 'com.google.code.gson:gson:2.8.5'
    compile 'org.springframework.boot:spring-boot-starter-web:2.1.4.RELEASE'
    compile 'org.springframework.boot:spring-boot-starter:2.1.4.RELEASE'
    compile 'org.springframework.cloud:spring-cloud-starter-openfeign:2.1.0.RELEASE'
}
12-30 18:44
查看更多