idea上gradle與springcloud的簡單搭建(三)


生產與消費

項目github地址

生產者

1.需求

返回helloWorld
讀取相對路徑下的excel文件
2.分析
創建一個關於excel的服務,將接口暴露出去,供其它服務調用
3.接口
-- 在produce_api模塊,其下包含User實體類
不用接口模式獲取生產者實例時不需要(如helloWorld實例)

@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'
}

build.gradle講解地址
eureka與zuul地址
生產與消費地址


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM