Update: 集成 Feign Client 和 Ribbon
This commit is contained in:
@@ -43,7 +43,15 @@
|
||||
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
+4
@@ -2,8 +2,12 @@ package cn.hezhaohui.serviceconsumer;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableFeignClients
|
||||
@EnableDiscoveryClient
|
||||
public class ServiceConsumerApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ServiceConsumerApplication.class, args);
|
||||
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package cn.hezhaohui.serviceconsumer.client;
|
||||
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
@FeignClient(name = "service-producer")
|
||||
public interface ServiceProducerClient {
|
||||
|
||||
@GetMapping("/api/number")
|
||||
String getNumber();
|
||||
}
|
||||
+3
-2
@@ -1,5 +1,6 @@
|
||||
package cn.hezhaohui.serviceconsumer.controller;
|
||||
|
||||
import cn.hezhaohui.serviceconsumer.client.ServiceProducerClient;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
@@ -11,11 +12,11 @@ import org.springframework.web.client.RestTemplate;
|
||||
public class ConsumeController {
|
||||
|
||||
@Resource
|
||||
private RestTemplate restTemplate;
|
||||
private ServiceProducerClient serviceProducerClient;
|
||||
|
||||
@GetMapping("/print")
|
||||
public String print() {
|
||||
String number = restTemplate.getForObject("http://service-producer/api/number", String.class);
|
||||
String number = serviceProducerClient.getNumber();
|
||||
return "[Generate]: " + number;
|
||||
}
|
||||
|
||||
|
||||
@@ -8,4 +8,4 @@ spring:
|
||||
eureka:
|
||||
client:
|
||||
service-url:
|
||||
defaultZone: http://localhost:8761/eureka
|
||||
defaultZone: http://localhost:8761/eureka/
|
||||
-1
@@ -4,7 +4,6 @@ import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
|
||||
public class ServiceProducerApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ServiceProducerApplication.class, args);
|
||||
|
||||
@@ -8,4 +8,4 @@ spring:
|
||||
eureka:
|
||||
client:
|
||||
service-url:
|
||||
defaultZone: http://localhost:8761/eureka
|
||||
defaultZone: http://localhost:8761/eureka/
|
||||
Reference in New Issue
Block a user