服务注册中心 Eureka 服务发现Discovery
定义:对于注册进eureka里面的微服务,可以通过服务发现来获得该服务的信息。
1.修改cloud-provider-payment8001和cloud-provider-payment8002的Controller
1.1 引入DiscoveryClient
注意要导入的是:import org.springframework.cloud.client.discovery.DiscoveryClient;
1.2 修改8001 controller
package com.ckfuture.springcloud.controller; import com.ckfuture.springcloud.entities.CommonResult; import com.ckfuture.springcloud.entities.Payment; import com.ckfuture.springcloud.service.PaymentService; import com.netflix.appinfo.InstanceInfo; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; 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; @Api(tags={"支付接口"}) @RestController @Slf4j public class PaymentController { @Resource private PaymentService paymentService; @Value("${server.port}") private String serverPort; @Resource private DiscoveryClient discoveryClient; @PostMapping(value = "/payment/create") public CommonResult create(@RequestBody Payment payment) { int result = paymentService.create(payment); if (result > 0) { return new CommonResult(200, "插入数据库成功,serverPort:" + serverPort, result); } else { return new CommonResult(301, "插入数据库失败", null); } } @GetMapping(value = "/payment/get/{id}") public CommonResult getPaymentById(@PathVariable("id") Long id) { Payment payment = paymentService.getPaymentById(id); if (payment != null) { return new CommonResult(200, "查询成功,serverPort:" + serverPort, payment); } else { return new CommonResult(301, "没有对应记录,查询Id:" + id, null); } } @GetMapping(value = "/payment/discovery") public Object discovery() { 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; } }
8002Controller:
package com.ckfuture.springcloud.controller; import com.ckfuture.springcloud.entities.CommonResult; import com.ckfuture.springcloud.entities.Payment; import com.ckfuture.springcloud.service.PaymentService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; 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; @Api(tags={"支付接口"}) @RestController @Slf4j public class PaymentController { @Resource private PaymentService paymentService; @Value("${server.port}") private String serverPort; @Resource private DiscoveryClient discoveryClient; @PostMapping(value = "/payment/create") public CommonResult create(@RequestBody Payment payment){ int result = paymentService.create(payment); log.info("*****插入结果:"+result); if(result > 0){ return new CommonResult(200,"插入数据库成功,serverPort:"+serverPort,result); }else { return new CommonResult(301,"插入数据库失败",null); } } @GetMapping(value = "/payment/get/{id}") public CommonResult getPaymentById(@PathVariable("id") Long id){ Payment payment = paymentService.getPaymentById(id); if(payment != null){ return new CommonResult(200,"查询成功,serverPort:"+serverPort,payment); }else { return new CommonResult(301,"没有对应记录,查询Id:"+id,null); } } @GetMapping(value = "/payment/discovery") public Object discovery() { 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; } }
2.修改8001启动类
主启动类添加@EnableDiscoveryClient注解
8001主启动类:
package com.ckfuture.springcloud; import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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; /** * @descrption: 支付生产者微服务主启动类 * @author: CKFuture * @since: 2021-10-09 21:18 * @version: v1.0 * @LastEditTime: * @LastEditors: * @copyright: hrbckfuture.com */ @SpringBootApplication @EnableEurekaClient @EnableDiscoveryClient public class PaymentMain8001 { private static final Logger logger = LoggerFactory.getLogger(PaymentMain8001.class); public static void main(String[] args) { SpringApplication.run(PaymentMain8001.class,args); logger.info("------------ API Service8001 Start Running--------------"); } }
8002主启动类:
package com.ckfuture.springcloud; import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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; /** * @descrption: 支付生产者微服务主启动类 * @author: CKFuture * @since: 2021-10-09 21:18 * @version: v1.0 * @LastEditTime: * @LastEditors: * @copyright: hrbckfuture.com */ @SpringBootApplication @EnableEurekaClient @EnableDiscoveryClient public class PaymentMain8002 { private static final Logger logger = LoggerFactory.getLogger(PaymentMain8002.class); public static void main(String[] args) { SpringApplication.run(PaymentMain8002.class,args); logger.info("------------ API Service8002 Start Running--------------"); } }
3.测试
分类:
SpringCloud
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!