简介
基于上一篇搭建好的eureka注册中心,详细查看 https://blog.csdn.net/zhangjikuan/article/details/109819812
本文将搭建微服务注册到注册中心
微服务搭建
1.nwe-module 创建微服务项目
2.pom中添加如下依赖
<dependencies>
<!--eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- spring boot test -->
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.11.RELEASE</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<configuration>
<delimiters>
<delimiter>@</delimiter>
</delimiters>
<useDefaultDelimiters>false</useDefaultDelimiters>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
<!-- 是否替换@xx@表示的maven properties属性值 -->
<filtering>true</filtering>
<includes>
<include>application.yml</include>
</includes>
</resource>
</resources>
</build>
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<profileActive>dev</profileActive>
</properties>
</profile>
<profile>
<id>pre</id>
<properties>
<profileActive>pre</profileActive>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<profileActive>prod</profileActive>
</properties>
</profile>
</profiles>
spring-boot-starter-web 是一个web微服务
3.添加配置文件 appication.yml
spring:
profiles:
active: @profileActive@
application:
name: zjk
server:
port: 8024
eureka:
instance:
preferIpAddress: true
client:
registryFetchIntervalSeconds: 30
4.添加环境相关配置文件 application-dev.yml
eureka:
client:
healthcheck:
enabled: true
serviceUrl:
defaultZone: http://localhost:8761/eureka
5.添加启动类 ZjkApplication
@EnableEurekaClient
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class ZjkApplication {
public static void main(String[] args) {
SpringApplication.run(ZjkApplication.class, args);
}
}
6.创建个controller 用来测试
@RestController
public class TestService {
@RequestMapping("zjkTest")
@ResponseBody
public String zjkTest() {
return "this is zjk test";
}
}
7.到此测试微服务已经搭建成功,运行微服务,在eureka页面查看是否注册成功
8.测试:http://localhost:8024/zjkTest
此测试微服务在后面搭建gateway会用到