利用Eureka和Feign实现两个微服务之间的调用

发布时间:2024年01月15日

根据上篇文章(在Eureka中注册多个服务(根据本地主机端口号区分)),我们已经掌握了Eureka的注入方法。
本次我们需要实现微服务之间的调用,其实是将两个客户端注入Eureka,具体就是换一个注解。
目的:用SClient1中的controller调用SClient0中相应的方法。
在这里插入图片描述

项目结构

在这里插入图片描述
在这里插入图片描述

Pom依赖

在最外层Pom中写入

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

SServer不用单独写依赖,另外两个均需加入入下依赖

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

程序

SServer中的ServerApplication

package com.buka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class ServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServerApplication.class, args);
        System.out.println("注册中心启动!!!");
    }
}

application.yml

server:
  port: 6869
spring:
  application:
    name: eureka
eureka:
  instance:
    hostname: EurekaServer
  client:
    service-url:
      defaultZone: http://127.0.0.1:${server.port}/eureka/
    register-with-eureka: false

SClient0中的ClientApplication0

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class ClientApplication0 {
    public static void main(String[] args) {
        SpringApplication.run(ClientApplication0.class,args);
        System.out.println("学生端启动!!!");
    }
}

Student

public class Student {
    private Integer id;
    private String name;
    private Integer age;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

StudengService

@Service
public class StudentService {
    public List<Student> queryStudents(Integer id){
        System.out.println(id);
        Student s1=new Student();
        s1.setId(1);
        s1.setName("小明");
        s1.setAge(18);
        Student s2=new Student();
        s2.setId(2);
        s2.setName("小丽");
        s2.setAge(18);
        List<Student> studentList = new ArrayList<>();
        studentList.add(s1);
        studentList.add(s2);
        return studentList;
    }
}

StudentController

@RestController
public class studentController {
    @Autowired
    private StudentService studentService;

    @RequestMapping(value = "/querystudents/{id}")
    public List<Student> queryStudents(@PathVariable("id") Integer id){
        return studentService.queryStudents(id);
    }
}

application.yml

server:
  port: 6870
spring:
  application:
    name: eureka2
eureka:
  instance:
    hostname: EurekaServer2
  client:
    service-url:
      defaultZone: http://127.0.0.1:6869/eureka/

SClient1中的ClientApplication1

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class ClientApplication1 {
    public static void main(String[] args) {
        SpringApplication.run(ClientApplication1.class,args);
        System.out.println("用户端启动!!!");
    }
}

Student类如上
StudentClient接口

@Service
@FeignClient(value = "eureka2",url="http://127.0.0.1:6870")
public interface StudentClient {
    @RequestMapping(value = "/querystudents/{id}")
    @LoadBalanced
    public List<Student> queryStudents(@PathVariable("id") Integer id);
}

userController

@RestController
public class userController {
    @Autowired
    private StudentClient studentClient;
    @ResponseBody
    @RequestMapping(value = "/userstudents/{id}")
    public List<Student> userQueryStudents(@PathVariable("id") Integer id){
        return studentClient.queryStudents(id);
    }
}

注意:上面的方法中我传送了一个id,它没有实际的作用,只是给大家展示一下应该如何进行传参。

运行

http://localhost:6871/userstudents/1
在这里插入图片描述
这里可以显示在SClient0中实际编写的方法

文章来源:https://blog.csdn.net/snow_original/article/details/135559417
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。