1. 程式人生 > >springCloud服務間呼叫的使用

springCloud服務間呼叫的使用

springCloud服務間呼叫的使用

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@FeignClient(name = "produ-data-system")
public interface DictionaryService {
	@GetMapping("/dicItem/getDicItem/{dictCode}")
	AssembleJSON getDicItem(@PathVariable("dictCode") String dictCode);
}

另一個專案的方法

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/dicItem")
public class DictionaryItemController extends BaseController<DictionaryItemService, DictionaryItem>{
	@GetMapping("/getDicItem/{dictCode}")
	public AssembleJSON getDicItemList(@PathVariable String dictCode) {
		return AssembleJSON.SUCCESS(service.getDicItemList(dictCode));
	}
}

yml配置檔案

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8180/eureka/
server: 
  port: 8802
spring:
    datasource:
        name: data-source
        url: jdbc:mysql://111.11.115.1:31338/lpg_sc?useSSL=false&useUnicode=true&characterEncoding=UTF-8
        username: lpg_sc 
        password: lpg_sc
        driver-class-name: com.mysql.jdbc.Driver
    application:
        name: produ-data-system
        
mybatis:
    xmlLocation: classpath:com/cn/**/mapping/*.xml
    mapper-locations: "classpath*:com/cn/**/mapping/*.xml"
    
mapper:
  identity: mysql
  before: false
  style: normal
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8180/eureka/
server:
  port: 8000
spring:
  application:
    name: produ-data-getwei
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    joda-date-time-format: yyyy-MM-dd HH:mm:ss
ribbon:
  ReadTimeout: 60000
  ConnectTimeout: 60000

zuul: 
  prefix: /cn
  strip-prefix: true
  routes: 
    cplugs:
      path: /plugs/**
      serviceId: produ-data-cplugs
    system:
      path: /system/**
      serviceId: produ-data-system

  host:
    connect-timeout-millis: 60000
    socket-timeout-millis: 60000

#zuul: 
#  routes:
#   users:  // 路由名稱,隨意,唯一即可
#      path: /ecom/**  // 路由路徑
#      url: http://localhost:9000



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

import tk.mybatis.spring.annotation.MapperScan;

@EnableEurekaClient
@MapperScan(basePackages = "com.cn.**.dao")
@SpringBootApplication(scanBasePackages = {"com.cn"})
@EnableFeignClients(basePackages = {"com.cn.cloud","com.cn.system"})
public class ProduDataSystemApplication {

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

請求路徑:

http://localhost:8000/cn/account/res/resource/getResByUserId?userId=3&

http://localhost:8000/cn/system//dicItem/getDicItem?dictCode=installAcceptProject

js通過路由呼叫不同服務

            $.get("/plugs/attachment/getByIds/" + value,
                function (result) {
                    if (result.data) {
                        $.each(result.data, function (i, n) {
                            if (n != null)
                                $htmlUpload.append(createFileItem(uploader,
                                    {
                                        id: n.attachmentId,
                                        name: n.fileName,
                                        size: n.fileSize,
                                        attachmentId: n.attachmentId,
                                        ext: n.attachmentType.replace(".", "")
                                    },
                                    opts));
                        });
                        //t.combo("setText", fileNames.join(","));
                    }
                    changevalue();
                });
@Controller
@RequestMapping("/attachment")
public class AttachmentController  extends BaseController<AttachmentService,Attachment> {
    @GetMapping("/getByIds/{attIds}")
    @ResponseBody
    public AssembleJSON getVOByIds(@PathVariable String attIds){
    	if("null".equals(attIds) || "".equals(attIds)){
    		return AssembleJSON.FAILURE("引數錯誤");
    	}
    	String[] strings = attIds.split(",");
    	List<AttachmentVO> list = new ArrayList<>();
    	for(String id : strings){
    		AttachmentVO attachmentVO = attachmentVOService.getById(Integer.parseInt(id));
    		list.add(attachmentVO);
    	}
    	return AssembleJSON.SUCCESS(list);
    }
}