赞
踩
1.不同Eureka Client之间如何通过OpenFeign实现服务之间互相调用?
步骤一:先创建两个项目,前提都需要注册到注册中心
如何创建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>
步骤二:如果要使用OpenFeign,需要在pom.xml中添加OpenFeign的依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
——》保存,然后点击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); }
步骤四:通过注册中心来调用,首先需要一个调用的接口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(); }
步骤五:修改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); } }
修改后的调用方式
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(); } }
重启,首先启动Eureka Server
然后启动Eureka Client
浏览器输入url:
localhost:8003/building/message
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。