7.4服务发现Discovery(常用!)

(一)是什么

显示你注册进Eureka的服务的信息

(二)构建步骤(在8001中构建)

在这里插入图片描述

(2.1)修改Controller

 @Resource
    private DiscoveryClient discoveryClient; //服务发现 注意是org.springframework.cloud.client.discovery.DiscoveryClient下的

//显示自己的信息,服务发现
    @GetMapping(value = "/discovery")
    public Object discovery(){
        //这个是获得注册中心有哪几个微服务注册进来了,也就是消费者,服务提供者
        List<String> services = discoveryClient.getServices();
        for (String service : services) {
            log.info("****微服务:"+service);
        }

        //这个是一个具体的微服务下有那几个,也就是集群,(服务提供者有两个,但是只有一个名字,这个是根据名字将他下面的显示出来)
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        for (ServiceInstance instance : instances) {
            log.info("id"+instance.getServiceId()+"\t"+"主机名称"+instance.getHost()+"\t"+"端口号"+instance.getPort()+"\t"+"url"+instance.getUri());
        }
        return this.discoveryClient;//这里写完要去主启动类中加一个注解开启服务发现@EnableDiscoveryClient
    }

完整代码

package com.gy.springcloud.controller;

import com.gy.springcloud.pojo.CommonResult;
import com.gy.springcloud.pojo.Payment;
import com.gy.springcloud.service.PaymentService;
import com.sun.istack.internal.logging.Logger;
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; //服务发现 注意是org.springframework.cloud.client.discovery.DiscoveryClient下的

    //查询一个用户
    @GetMapping(value = "/select/{id}")
    public CommonResult select(@PathVariable("id") Long id){
        System.out.println("进来了!!!");
        Payment payment = paymentService.selectByID(id);
        log.info("查找结果:"+payment);
        if(payment!=null){
            return new CommonResult(200,"查询成功"+serverPort,payment);
        }else{
            return new CommonResult(404,"没有这个号:"+serverPort,id);
        }

    }

    //插入一个用户
    @PostMapping("/add")
    public CommonResult add(@RequestBody Payment payment){
        int i = paymentService.addUser(payment);

        log.info("插入结果:"+i);

        if(i>0){
            return new CommonResult(200,"插入成功"+serverPort,i);
        }else {
            return new CommonResult(404,"插入失败"+serverPort,null);
        }
    }

    //显示自己的信息,服务发现
    @GetMapping(value = "/discovery")
    public Object discovery(){
        //这个是获得注册中心有哪几个微服务注册进来了,也就是消费者,服务提供者
        List<String> services = discoveryClient.getServices();
        for (String service : services) {
            log.info("****微服务:"+service);
        }

        //这个是一个具体的微服务下有那几个,也就是集群,(服务提供者有两个,但是只有一个名字,这个是根据名字将他下面的显示出来)
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        for (ServiceInstance instance : instances) {
            log.info("id"+instance.getServiceId()+"\t"+"主机名称"+instance.getHost()+"\t"+"端口号"+instance.getPort()+"\t"+"url"+instance.getUri());
        }
        return this.discoveryClient;//这里写完要去主启动类中加一个注解开启服务发现@EnableDiscoveryClient
    }


}

(2.2)在主启动中加一个注解@EnableDiscoveryClient开启服务发现

@SpringBootApplication
@EnableEurekaClient//Eureka的两大组件之一 表示服务提供
@EnableDiscoveryClient//开启服务发现,注意这个以后会常用
public class PaymentMain8001 {
    public static void main(String[] args) {
        SpringApplication.run(PaymentMain8001.class,args);
    }
}

(2.3)测试 访问http://localhost:8001/discovery

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

8002配置同理

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值