1. 程式人生 > 其它 >spring cloud openFeign 服務呼叫

spring cloud openFeign 服務呼叫

技術標籤:spring cloudjava

spring cloud openFeign 服務呼叫

spring boot 2.3.5
spring cloud Hoxton.RELEASE

服務方

pom檔案

	<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.5.RELEASE<
/version> <relativePath/> </parent> <properties> <spring-cloud.version>Hoxton.RELEASE</spring-cloud.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.
cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies>
</dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>${spring-boot.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>${spring-boot.version}</version> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version>${spring-boot.version}</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <!-- 引入eureka客戶端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!-- open feign --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> </dependencies>

Application.yml檔案

server:
  port: 8090
spring:
  application:
    name: base-websocket
eureka:
  client:
    serviceUrl:
      #      eureka的註冊中心地址
#      defaultZone: http://eureka9999.com:9999/eureka/,http://eureka9998.com:9998/eureka/,http://eureka9997.com:9997/eureka/
      defaultZone: http://eureka9998.com:9998/eureka/
#設定feign客戶端超時時間(openFeign預設支援ribbon)
ribbon:
  #指的是建立連線所用的時間,適用於網路狀況iE常的情況下兩端連線所用的時間
  ReadTimeout: 5000
  #指的是建立連線後從伺服器讀取到可用資源所用的時間
  ConnectTimeout: 5000

啟動類

需要把服務註冊到註冊中心中,並且開啟openFeign

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

測試controller

@RequestMapping(value = "/demo")
@Controller
public class TestController {
	@Value("${server.port}")
    private String port;
    // 用於測試返回當前服務提供者的埠號
    @GetMapping("/payment/{id}")
    @ResponseBody
    public String  testCustomer(@PathVariable("id")int id){
        System.out.println(port + ":" + id);
        return port + ":" + id;
    }
  }

呼叫方

pom檔案

pom.xml

Application.yml檔案

Application.yml 修改埠

啟動類

啟動類

服務呼叫service

@Component
//value就是配置檔案中設定的名稱
@FeignClient(value = "BASE-OPENFEIGN")
public interface PaymentService {

    @RequestMapping(value = "/demo/payment/{id}",method = RequestMethod.GET)
    @ResponseBody
    public String testCustomer(@PathVariable("id")int id);

測試controller

@RequestMapping
@Controller
public class openFeignClientController {

    @Autowired
    private PaymentService paymentService;

    @RequestMapping(value = "/demo/payment/{id}",method = RequestMethod.GET)
    @ResponseBody
    public String fegin(@PathVariable("id")int id) {
        return paymentService.testCustomer(id);
    }

訪問呼叫方controller就可以呼叫服務方介面並返回服務方的埠和我們傳的ID