一、EurekaProvider—服务提供者
二、实战
1. 使用IDEA的Spring Assistan创建工程,选择Default,单击next
2.填写项目信息,命名工程名称为eureka_provider,单击next
3.选择SpringCloudDisicovery中的Eureka Discovery Client和Web中的SpringWeb,单击next,然后finish完成。
4.创建完后的工程的pom.xml文件自带依赖spring-boot-starter-web、spring-cloud-starter-netflix-eureka-client 和spring-boot-starter-test如下:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
5.给EurekaProviderApplication主程序添加@EnableEurekaClient注解
@EnableEurekaClient
@SpringBootApplication
public class EurekaProviderApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaProviderApplication.class, args);
}
}
6.在resources文件夹下创建application.yml文件,内容如下。并删除掉application.properties。
server:
port: 8771
enable-self-preservation: false
spring:
application:
name: eureka-provider
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
7. 新建controller文件夹,创建HelloController类
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello()
{
return "hello World";
}
}
8.运行程序,重新刷新http://localhost:8761 ,界面如下,可以看到eureka_provider已经在到注册中心了: