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

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

项目结构

利用Eureka和Feign实现两个微服务之间的调用_第2张图片
利用Eureka和Feign实现两个微服务之间的调用_第3张图片

Pom依赖

在最外层Pom中写入

<parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.1.4.RELEASEversion>
    parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
            <version>2.1.0.RELEASEversion>
        dependency>
    dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>Greenwich.SR1version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

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

<dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-openfeignartifactId>
        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
利用Eureka和Feign实现两个微服务之间的调用_第4张图片
这里可以显示在SClient0中实际编写的方法

你可能感兴趣的:(Spring,eureka,微服务,云原生,java,spring)