在Server项目中
- 新建Controller
package com.basil.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
/**
* @Author: chuan.bai
* @Description
* @Date: Created on 10:29 2018/12/25
* @Modified By:
*/
@RestController
public class TestController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private DiscoveryClient discoveryClient;
/**
* 获取eureka-client服务信息
* @return
*/
@GetMapping(value = "getEurekaClientInstance")
public List<ServiceInstance> getEurekaClientInstance() {
List<ServiceInstance> list=discoveryClient.getInstances("eureka-client");
return list;
}
/**
* 获取所有的服务
* @return
*/
@GetMapping(value = "getServiceMetaData")
public String getServiceMetaData(){
StringBuilder buf = new StringBuilder();
List<String> serviceIds = discoveryClient.getServices();
System.out.println("serviceId"+serviceIds);
if(!CollectionUtils.isEmpty(serviceIds)){
for(String s : serviceIds){
System.out.println("serviceId:" + s);
List<ServiceInstance> serviceInstances = discoveryClient.getInstances(s);
if(!CollectionUtils.isEmpty(serviceInstances)){
for(ServiceInstance si:serviceInstances){
buf.append("["+si.getServiceId() +" host=" +si.getHost()+" port="+si.getPort()+" uri="+si.getUri()+"]");
}
}else{
buf.append("no service.");
}
}
}
return buf.toString();
}
}
- 启动项
package com.basil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/**
* @author Basil
*/
@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
@Autowired
private RestTemplateBuilder builder;
// 使用RestTemplateBuilder来实例化RestTemplate对象,spring默认已经注入了RestTemplateBuilder实例
@Bean
public RestTemplate restTemplate() {
return builder.build();
}
}
- 可以在配置文件中 增加自定义数据 eureka.instance.metadata-map.my-metadata
server:
port: 9092
spring:
application:
name: eureka-client
eureka:
instance:
hostname: localhost
# 使用IP注册
preferIpAddress: true
# 心跳间隔
lease-renewal-interval-in-seconds: 3
# 服务失效时间: 如果多久没有收到请求,则可以删除服务
lease-expiration-duration-in-seconds: 7
metadata-map :
my-metadata: aa:cc
client:
# 关闭eureka client
# enabled: false
# 注册自身到eureka服务器
register-with-eureka: true
# 表示是否从eureka服务器获取注册信息
fetch-registry: false
# 客户端从Eureka Server集群里更新Eureka Server信息的频率
eureka-service-url-poll-interval-seconds: 60
# 定义从注册中心获取注册服务的信息
registry-fetch-interval-seconds: 5
# 设置eureka服务器所在的地址,查询服务和注册服务都需要依赖这个地址
service-url:
defaultZone: http://server1:8771/eureka/,http://server2:8772/eureka/,http://server3:8773/eureka/
image.png
访问页面:http://localhost:8771/eureka/apps 查看metadata
网友评论