目录
1.创建maven项目
2.idea打开maven项目
3.修改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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.lizz</groupId>
<artifactId>eureka-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-provider</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.RC2</spring-cloud.version>
</properties>
<dependencies>
<!--web项目 默认使用内置tomcat-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--eureka client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--test 相关jar包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<!--spring cloud版本依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<!--打包生成的文件名及路径-->
<finalName>../eureka-provider</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<!--maven远程仓库-->
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
</project>
4.创建yml配置文件
在src/main/resources目录下创建application.yml文件(使用application.properties也可以)
spring:
application:
name: eureka-provider #服务名
server:
port: 8601 #服务访问端口
servlet:
context-path: / #访问路径
eureka:
instance: #Eureka 实例
prefer-ip-address: true #是否使用IP地址进行访问
#实例显示名
instanceId: ${spring.application.name}-@project.version@:${spring.cloud.client.ip-address}:${server.port}
client:
serviceUrl:
#Eureka Server服务集群
defaultZone: http://192.168.180.15:9991/eureka/,http://192.168.180.15:9992/eureka/,http://192.168.180.15:9993/eureka/
5.创建RESTful API
创建文件.../controller/ProviderController.java
package com.lizz.eurekaprovider.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* ProviderController
*
* @author lizz
*/
@RestController
@RequestMapping(value = "/provider/", produces = "application/json; charset=utf-8")
public class ProviderController {
private static final Logger logger = LoggerFactory.getLogger(ProviderController.class);
@RequestMapping("{name}/hi")
public String hi(@PathVariable("name") String name) {
logger.info("******************hi {}", name);
return "hi " + name;
}
}
6.开启Eureka 生成者服务
在启动文件中增加@EnableDiscoveryClient或@EnableEurekaClient标签,执行main方法,启动程序。
EnableDiscoveryClient:Spring Cloud内置通用服务发现客户端
EnableEurekaClient:Eureka特定服务发现客户端
package com.lizz.eurekaprovider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class EurekaProviderApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaProviderApplication.class, args);
}
}
7.查看Eureka控制台
注册成功,其他eureka服务信息一致。