赞
踩
前言:在上一章节中我们说明了一些关于服务信息的配置,在本章节则介绍一些关于Discovery的知识点及其使用
Discovery(服务发现)是eureka的功能和特性,有时候微服务可能需要对外提供一种功能,这个功能可以对外提供服务IP、服务名称、端口号等服务信息,而这时候Discovery就能实现这个功能,Discovery能对外暴露注册进Eureka里的服务信息
在provider-payment8001模块的启动类里加上@EnableDiscoveryClinet这一注解来启用服务发现
- package com.ken.springcloud;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
- import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
-
- @SpringBootApplication
- //标注为Eureka Client(服务注册中心)
- @EnableEurekaClient
- @EnableDiscoveryClient
- public class PaymentMain {
- public static void main(String[] args) {
- SpringApplication.run(PaymentMain.class, args);
- }
- }
效果图:
在provider-payment8001模块的PaymentController类里注入DiscoveryClient接口,然后利用DiscoveryClient接口的方法在PaymentController类里实现服务发现接口
- package com.ken.springcloud.controller;
-
- import com.ken.springcloud.entities.CommonResult;
- import com.ken.springcloud.entities.Payment;
- import com.ken.springcloud.service.PaymentService;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.cloud.client.ServiceInstance;
- import org.springframework.cloud.client.discovery.DiscoveryClient;
- import org.springframework.web.bind.annotation.*;
-
- import javax.annotation.Resource;
- import java.util.List;
-
- @RestController
- @Slf4j
- public class PaymentController {
-
- @Resource
- private PaymentService paymentService;
-
- @Value("${server.port}")
- private String serverPort;
-
- @Resource
- private DiscoveryClient discoveryClient;
-
- @PostMapping("/payment/insert")
- public CommonResult insert(@RequestBody Payment payment) {
- int result = paymentService.insert(payment);
- log.info("插入结果{}",result);
- if(result > 0) {
- return new CommonResult(200,"插入数据库成功,提供服务的端口号为" + serverPort,result);
- }else {
- return new CommonResult(500,"插入数据库失败",result);
- }
- }
-
- @GetMapping("/payment/get/{id}")
- public CommonResult insert(@PathVariable("id") Long id) {
- Payment payment = paymentService.getPaymentById(id);
- log.info("查询结果{}",payment);
- if(payment != null) {
- return new CommonResult(200,"查询成功,提供服务的端口号为" + serverPort,payment);
- }else {
- return new CommonResult(500,"没有对应的数据,查询失败,查询id" + id,payment);
- }
- }
-
- @GetMapping("/payment/discovery")
- public Object discovery() {
- //获取eureka内的服务
- List<String> services = discoveryClient.getServices();
- for (String service : services) {
- log.info("***service:" + service);
- }
- //获取服务名为CLOUD-PAYMENT-SERVICE下的实例
- List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
- for (ServiceInstance instance : instances) {
- log.info(instance.getServiceId()+"\t"+instance.getHost()+"\t"+instance.getPort()+"\t"+instance.getUri());
- }
- return this.discoveryClient;
- }
-
- }
效果图:
在浏览器地址栏输入http://localhost:8001/payment/discovery然后按回车调用服务发现接口,查看接口返回的信息以及控制台里打印的日志信息,如果接口能成功拿到eureka里的服务信息,则证明服务发现接口实现成功
效果图:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。