组件 | 版本 |
---|---|
spring-boot | 2.1.3.RELEASE |
spring-cloud-starter-eureka | 1.3.4.RELEASE |
spring-cloud-dependencies | Finchley.RELEASE |
Server端配置:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.3.4.RELEASE</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
server.port=12358
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/
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
运行之后:
访问http://localhost:12358
下面讲解客户端配置:
POM主要的配置如下:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.3.4.RELEASE</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
application.properties配置如下:
spring.application.name=hello-service
eureka.client.service-url.defaultZone=http://localhost:12358/eureka/
package com.sh.sctest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient
@SpringBootApplication
public class SctestApplication {
public static void main(String[] args) {
SpringApplication.run(SctestApplication.class, args);
}
}
package com.sh.sctest.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController("v1HelloController")
public class HelloController {
private final Logger logger = LoggerFactory.getLogger(HelloController.class);
@Autowired
private DiscoveryClient discoveryClient;
@PostMapping("/aaa")
public String index() {
for (String s : discoveryClient.getServices()) {
for (ServiceInstance instance : discoveryClient.getInstances(s)) {
logger.info("/hello, service:" + s + ",host:" + instance.getHost() + ", service_id:" + instance.getServiceId());
}
}
return "hello world !\n";
}
}
运行客户端:
访问 http://localhost:12356/aaa/
RdeMacBook-Pro:~ r$ curl http://localhost:12356/aaa/ -d ''
hello world !
RdeMacBook-Pro:~ r$
日志打印如下:
2019-03-31 21:50:01.916 INFO 17529 --- [io-12356-exec-1] c.sh.sctest.controller.HelloController : /hello, service:hello-service,host:192.168.2.156, service_id:HELLO-SERVICE
再次访问server的url:
服务端日志: