一、继上一篇文章,以上一篇项目作为注册中心服务,新建客户端
二、pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.syf.study</groupId>
<artifactId>eureka-client-1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>eureka-client-1</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.14.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--全栈开发模式,包含嵌入Tomcaat和SpringMVC-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--包含JUnite,Hamcrest,Mockito-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--依赖模块 可不写-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--springcloud-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.3.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<!--springcloud-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
二、Controller
package com.syf.study;
import org.apache.log4j.Logger;
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.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
private final Logger logger= Logger.getLogger(HelloController.class);
@Autowired
private DiscoveryClient client;
@GetMapping("/hello")
public String index(){
ServiceInstance instance = client.getLocalServiceInstance();
logger.info("/hi,host"+instance.getHost()+",service_id"+instance.getServiceId());
return "hello world";
}
}
三、启动类启动注册中心客户端
package com.syf.study;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient //激活Eureka中DiscoveryClient实现自动化配置,创
//建DiscoveryClient接口针对Eureka客户端的EurekaDiscoveryClient实例
public class EurekaClient1Application {
public static void main(String[] args) {
SpringApplication.run(EurekaClient1Application.class, args);
}
}
四、application.properties文件配置服务名和注册中心客户端的注册服务地址【也可以用application.yml文件进行配置】
spring.application.name=eureka-client-1
eureka.client.service-url.defaultZone=http://localhost:1111/eureka/
###以下为application.yml配置
#spring:
# application:
# name: eureka-client-1
#eureka:
# client:
# serviceUrl:
# defaultZone: http://127.0.0.1:1111/eureka/
五、启动时看启动日志是否注册成功:
s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
.s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8080
com.netflix.discovery.DiscoveryClient : DiscoveryClient_EUREKA-CLIENT-1/7E2VOCK67ZCAOOE:eureka-client-1 - registration status: 204
com.syf.study.EurekaClient1Application : Started EurekaClient1Application in 4.724 seconds (JVM running for 5.733)
六、查看注册中心信息
七、访问http://localhost:8080/hello