STM32程式設計
阿新 • • 發佈:2020-10-09
pom
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <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-openfeign</artifactId> </dependency>
application.yml
server: port: 9002 spring: application: name: consumerfeign eureka: client: # 表示是否將自己註冊進入EurekaServer預設為true register-with-eureka: false # 是否從EurekaServer抓取已有的註冊資訊,預設為true # 單節點不會有影響,叢集環境必須設定為true才能配合ribbon使用負載均衡 fetch-registry: true # 註冊到的EurekaServer路徑 service-url: defaultZone: http://localhost:7001/eureka
編寫介面
FeignClient的value為微服務名字
方法上加上路徑
package com.springcloud.service; import com.springcloud.domain.Empt; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.GetMapping; import java.util.List; @Component @FeignClient(value = "PROVIDER") public interface EmptFeignService { @GetMapping("empt") List<Empt> getAll(); }
啟用註解
@SpringBootApplication
@EnableFeignClients
public class FeignApplication {
public static void main(String[] args) {
SpringApplication.run(FeignApplication.class, args);
}
}
使用
@RestController
public class AppController {
@Resource
private EmptFeignService service;
@GetMapping
public List<Empt> getAll() {
return service.getAll();
}
}