当前位置: 移动技术网 > IT编程>开发语言>Java > springcloud的Eureka服务间的调用实例

springcloud的Eureka服务间的调用实例

2020年08月17日  | 移动技术网IT编程  | 我要评论
本节就来模拟多个服务之间的调用,看官请准备,好戏就在下方

小编本文主要涉及模拟多个服务之间的调用

1 搭建订单服务工程

1. 创建一个order的maven子项目
2. 添加的pom内容如下:
注意删除子maven的pom中的relativePath,不然会出现很可怕的事情哦

<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> </dependency> </dependencies> 

3.在resource下创建application.yaml,相关代码如下:

server: port: 7900 # 指定该Eureka的实例端口 eureka: instance: prefer-ip-address: true # 是否显示主机IP instance-id: ${spring.cloud.client.ipAddress}:${server.port} # 格式“IP:端口号” client: service-url: defaultZone: http://localhost:8761/eureka/ # 指定eureka的服务端地址 spring: application: name: microservice-eureka-order #指定应用的名称 

4 创建订单实体类

package com.pixiu.po; import org.springframework.web.bind.annotation.Mapping; public class Order { private String id; private Double price; private String receiverName; private String receiverAddress; private String receiverPhone; /*
*1.生成get和set方法,这里理就省略不贴上来了
*2.生成toString	(),同理
*/ } 

5.创建订单控制类

package com.pixiu.controller; import com.pixiu.po.Order; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController public class OrderController { //    @Autowired //    private Order order; @GetMapping("/order/{id}") public String findOrderById(@PathVariable String id){ Order order = new Order(); order.setId("123"); order.setPrice(23.5); order.setReceiverAddress("beijing"); order.setReceiverName("big Grill"); order.setReceiverPhone("123345678990"); return order.toString(); } } 

6 在引导类(OrderEurekaApplication)上加上注解@EnableEurekaServer,具体代码就不展示了,前面创建客户工程中已经操作过了,流程一样

2 用户服务功能

1.这里需要就绪回顾到咱们的user客户端工程的项目里
2.在引导类中创建RestTemplate的spring的实例,为了读者方便这里就把代码重复贴出来

package com.pixiu; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.context.annotation.Bean; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @SpringBootApplication @RestController @EnableEurekaClient public class UserEurekaApplication { @RequestMapping("/hello") public String home(){ return "hello world!!!"; } public static void main(String[] args) { SpringApplication.run(UserEurekaApplication.class,args); } /**
     * RestTemplate的spring的实例
     * @return
     */ @Bean public RestTemplate restTemplate(){ return new RestTemplate(); } } 

3.创建用户控制类

package com.pixiu.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController public class UserController { @Autowired RestTemplate restTemplate; /**
     * 查找用户相关的订单
     * @param id
     * @return
     */ @GetMapping("/findOrderByUser/{id}") public String findOrderByUser(@PathVariable String  id){ //假设只有有个id为“123”的订单 int oid =123; return this.restTemplate.getForObject("http://localhost:7900/order/"+oid,String.class); } } 

3 启动服务应用,测试服务调用

1.分别启动服务注册中心,订单服务应用和用户服务应用此时Eureka信息页面的显示
在这里插入图片描述
2.通过浏览器访问地址http://www.lhsxpumps.com/_localhost:8000/findOrderByUser/1,浏览器显示结果如下:

在这里插入图片描述

到这里已经完成Eureka的注册,服务以及服务之间调用

本文地址:https://blog.csdn.net/weixin_42949808/article/details/108018471

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网