01-service-hi微服务应用
01-eureka-server
service-hi的pom.xml
<parent>
<groupId>com.sky</groupId>
<artifactId>chapter1</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>chapter1-service-hi</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
application.yml
server:
port: 8762
spring:
application:
#注册eureka服务用到
name: service-hi
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
@SpringBootApplication
@EnableEurekaClient
@RestController
public class ServiceHiApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceHiApplication.class, args);
}
@Value("${server.port}")
String port;
@RequestMapping("/hi")
Object home(@RequestParam(value = "name", defaultValue = "test") String name) {
return "hi " + name + " ,i am from port:" + port;
}
}
测试
先启动chapter1-eureka-server
再启动chapter1-service-hi
访问:http://localhost:8761/
看到http://localhost:8762/的程序已经注册到eureka了
访问:http://ip:8762/hi?name=hello