一、Pom依赖内容
与spring cloud(一)相比多了以下依赖五、访问localhost:8761,已将服务注册到eureka-server上,效果如下
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
二、配置文件application.properties内容
server.port=8762
#此处为此微服务暴露的服务名
spring.application.name=hello-service
eureka.port=8761eureka.instance.hostname=localhost
#在此指定服务注册中心地址(将此服务注册到上篇文章的eureka-server中)
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${eureka.port}/eureka/
三、启动项内容
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient
@SpringBootApplication
public class CloudClientApplication {
public static void main(String[] args) { SpringApplication.run(CloudClientApplication.class, args); }}
四、自定义一个小的服务
@RestController
public class HelloController {
@Value("${server.port}") String port;
@RequestMapping(value = "/helloClient")
public String index(String id) { return "Hello World:"+port+",id is"+id; }}
五、访问localhost:8761,已将服务注册到eureka-server上,效果如下