当前位置:   article > 正文

不同Eureka Client之间如何通过OpenFeign实现服务之间互相调用?_两个eureka服务互相访问

两个eureka服务互相访问

1.不同Eureka Client之间如何通过OpenFeign实现服务之间互相调用?

完成:第一遍

1.不同Eureka Client之间如何通过OpenFeign实现服务之间互相调用?

步骤一:先创建两个项目,前提都需要注册到注册中心

如何创建Eureka Client项目?

创建Eureka Client教程

创建Module时选择的左边点Spring Cloud Discovery
右边勾选Eureka Discovery Client

实际上就是在pom.xml中添加

 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
  • 1
  • 2
  • 3
  • 4

步骤二:如果要使用OpenFeign,需要在pom.xml中添加OpenFeign的依赖

   <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
  • 1
  • 2
  • 3
  • 4

——》保存,然后点击Import Changes

步骤三:打开启动类BuildingconsumerApplication
@EnableFeignClients作用是所有标注为FeignClient的类,系统都会自动去找到它并且使用它来调用远程

package com.itzhiya.building.consumer;

import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class BuildingconsumerApplication {

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

步骤四:通过注册中心来调用,首先需要一个调用的接口BuildingInterface,这里面会调用BuildingController里面的show()服务方法

package com.itzhiya.building.consumer;

import com.itzhiya.common.Product;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

@FeignClient(name="buildingservice/building", fallback = BuildingFallback.class)
public interface BuildingInterface {

    @GetMapping("message")
    public String show();

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

步骤五:修改BuildingController 调用的方式
原来版本:

package com.itzhiya.building.consumer;

import com.itzhiya.common.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
@RequestMapping("building")
public class BuildingController {

    private final String url = "http://localhost:8002/building/message";

    @GetMapping("message")
    public String show(){
       RestTemplate restTemplate = new RestTemplate();
        return restTemplate.getForObject(url, String.class);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

修改后的调用方式

package com.itzhiya.building.consumer;

import com.itzhiya.common.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
@RequestMapping("building")
public class BuildingController {

    @Autowired
    private BuildingInterface buildingInterface;

    @GetMapping("message")
    public String show(){
        return buildingInterface.show();
    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

重启,首先启动Eureka Server
然后启动Eureka Client
浏览器输入url:
localhost:8003/building/message

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/84565
推荐阅读
相关标签
  

闽ICP备14008679号