将一个SpringBoot项目注册为微服务,需要作如下修改。
pom.xml添加
<!--使用SpringBoot1.5.3版本-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--开启eureka服务发现功能-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!--开启ribbon功能-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
application.yml文件加入以下配置
eureka:
client:
serviceUrl:
#指定SpringCloud服务端地址
defaultZone: http://localhost:8000/eureka/
spring:
application:
name: db-service #指定服务名,服务消费者通过该服务名调用该服务
启动类中添加注册服务注解:@EnableDiscoveryClient 、@ServletComponentScan
如下所示:
@EnableDiscoveryClient //将自己注册到服务中心
@SpringBootApplication
@ServletComponentScan
public class SpringcloudtestApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudtestApplication.class, args);
}
}
启动项目,该项目就会被注册为一个服务。