1.pom.xml依赖添加
<!-- Spring Boot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>eureka-client</artifactId> <dependencies> <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-web</artifactId> </dependency> </dependencies> <!-- Spring Cloud --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.SR2</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
2.配置文件
spring: application: name: eureka-client-user-service server: port: 8081 eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ instance: preferIpAddress: true #采用IP注册 instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port} #定义实例ID格式
3.创建启动类
@SpringBootApplication @EnableDiscoveryClient public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } }
4.服务提供类
@RestController @RequestMapping("/user") public class UserController { @GetMapping("/hello") public String hello(){ return "hello , I'm eureka client!"; } }
来源:https://www.cnblogs.com/gxloong/p/12364141.html