(一)是什么
显示你注册进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