其他分享
首页 > 其他分享> > 服务注册中心 Eureka 服务发现Discovery

服务注册中心 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.测试

 

标签:服务,com,springframework,Eureka,org,import,CommonResult,payment,Discovery
来源: https://www.cnblogs.com/ckfuture/p/15415045.html