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

根据上篇文章(在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中实际编写的方法

  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值