一、创建eurekaServer
1、创建第一个server,创建一个springboot工程
1)java代码如下,使用端口7070:
@EnableEurekaServer
@SpringBootApplication
@RestController
public class EurekaserverApplication {
@RequestMapping("/exec")
public String execInfo() {
System.out.println("server01"); //danielinbiti
return "server01";
}
public static void main(String[] args) {
SpringApplication.run(EurekaserverApplication.class, args);
}
}
2)application.properties配置如下
server.port=7070
spring.application.name=server-01
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://localhost:7070/eureka/,http://localhost:7020/eureka/
eureka.instance.metadataMap.instanceId=${spring.application.name}:${spring.application.instance_id:${random.value}}
1)java代码如下,使用端口7020
@EnableEurekaServer
@SpringBootApplication
@RestController
public class EurekaserverApplication {
@RequestMapping("/exec")
public String execInfo() {
System.out.println("server02");
return "server02";
}
public static void main(String[] args) {
SpringApplication.run(EurekaserverApplication.class, args);
}
}
server.port=7020
spring.application.name=server-01
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://localhost:7070/eureka/,http://localhost:7020/eureka/
eureka.instance.metadataMap.instanceId=${spring.application.name}:${spring.application.instance_id:${random.value}}
1、java代码
@SpringBootApplication
@EnableEurekaClient
@RestController
@EnableAutoConfiguration
public class EurekaClientApplication {
@Autowired
private DiscoveryClient discoveryClient;
@RequestMapping("/getInfo")
public String getInfo() {
return "ekClient2";
}
@Bean
@LoadBalanced
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/exec")
public String execInfo() {
return restTemplate.getForObject("http://server-01/exec/", String.class);
}
@RequestMapping("/registered")
public String getRegistered(){
List list = discoveryClient.getInstances("STORES");
System.out.println(discoveryClient.getLocalServiceInstance());
System.out.println("discoveryClient.getServices().size() = " + discoveryClient.getServices().size());
//comment by danielinbiti
for( String s : discoveryClient.getServices()){
System.out.println("services " + s);
List serviceInstances = discoveryClient.getInstances(s);
for(ServiceInstance si : serviceInstances){
System.out.println(" services:" + s + ":getHost()=" + si.getHost());
System.out.println(" services:" + s + ":getPort()=" + si.getPort());
System.out.println(" services:" + s + ":getServiceId()=" + si.getServiceId());
System.out.println(" services:" + s + ":getUri()=" + si.getUri());
System.out.println(" services:" + s + ":getMetadata()=" + si.getMetadata());
}
}
System.out.println(list.size());
if (list != null && list.size() > 0 ) {
System.out.println( list.get(0).getUri() );
}
return "success";
}
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
}
server.port=6060
spring.application.name=client-02
eureka.client.serviceUrl.defaultZone=http://localhost:7070/eureka/
eureka.instance.metadataMap.instanceId=${PRO_TEST:5555}
http://localhost:6060/exec 就会来回出现server01,server02
四、注意点:
RestTemplate创建是需要加上负载策略,@LoadBalanced