1.server端
(1)properties配置
server.port=8761
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
(2)application.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class TestSpringCloudApplication {
public static void main(String[] args) {
SpringApplication.run(TestSpringCloudApplication.class, args);
}
}
2.client端
(1)properties配置
eureka.client.service-url.defaultZone= http://localhost:8761/eureka/
server.port=8762
spring.application.name=service-first
(2)application.java
package com.example.demo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
@EnableEurekaClient//表明自己是EurekaClient
public class TestSpringCloudClientApplication {
public static void main(String[] args) {
SpringApplication.run(TestSpringCloudClientApplication.class, args);
}
@Value("${server.port}")
private String port;
@GetMapping("/hi")
public String f(@RequestParam String name){
return "hi "+name+",i am from port:" +port;
}
}
3.测试
先开启server端,此时没有发现任何服务注册
再开启client端,此时发现有新的的服务注册到eureka中