农民 发表于 2024-11-17 08:13:38

利用OpenFeign+Eureka实现HTTP调用的简单示例


[*]由于RPC调用须要利用注册中心,以是首先须要创建eureka服务,创建SpringBoot项目后导入spring-cloud-starter-netflix-eureka-server,留意SpringBoot和SpringCloud版本同等性,然后举行配置,启动类添加注解@EnableEurekaServer
<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
server:
port: 8761

spring:
application:
    name: eureka

eureka:
client:
    register-with-eureka: false # 不向Eureka注册自己
    fetch-registry: false       # 不从Eureka获取注册信息
service-url:
    defaultZone: http://localhost:8761/eureka/
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {

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

}

[*]创建服务提供者SpringBoot应用,关键是导入spring-cloud-starter-netflix-eureka-client依靠,举行eureka的配置,编写controller接入层代码,启动类不须要加@EnableEurekaClient注解,因为在新版本中已经被移除
<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>
server:
port: 8082

spring:
application:
    name: producer

eureka:
client:
    register-with-eureka: true
    fetch-registry: true
service-url:
    defaultZone: http://localhost:8761/eureka/
@RestController
@RequestMapping("/api")
public class DataController {

    @GetMapping("/data")
    public String getData() {

      System.out.println("服务提供者被调用");
      return "Hello from Producer!";
    }
}

[*]创建服务消费者SpringBoot应用,引入如下三个关键依靠,举行eureka配置,编写接口,编写接入层代码调用该接口,启动类须要加上@EnableFeignClients
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</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-netflix-eureka-client</artifactId>
</dependency>
server:
port: 8083

spring:
application:
    name: consumer

eureka:
client:
    register-with-eureka: true
    fetch-registry: true
service-url:
    defaultZone: http://localhost:8761/eureka/
@FeignClient(name = "producer") // 此处不需要写url = "http://localhost:8082",因为通过注册中心调用
public interface ProducerClient {

    @GetMapping("/api/data")
    String getData();
}
@RestController
@RequestMapping("/consumer")
public class ConsumerController {

    @Autowired
    private ProducerClient producerClient;

    @GetMapping("/data")
    public String getDataFromProducer() {
      return producerClient.getData();
    }
}
@SpringBootApplication
@EnableFeignClients
public class ConsumerApplication {

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

}

[*]总结


[*]通过引入eureka注册中心后,如果服务提供者有多个节点,那么哀求就会被发送到存活的节点上,实现了动态路由,避免因为固定写url但是该节点宕机导致调用失败的题目

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: 利用OpenFeign+Eureka实现HTTP调用的简单示例