手把手搭建Eureka服务(含源码)
Spring Boot 版本 2.5.4
Spring Cloud 版本 2020.0.3
(如果要修改版本,请注意Spring Boot和Spring Cloud的版本是否匹配)
本文档将依次介绍注册中心、服务端、客户端的搭建过程。所有源码可从https://github.com/ttyy1112/Spring-Cloud-In-Action.git的scia-eureka项目中获取。
1. 项目目录
register : 注册中心
server: 服务端
client: 客户端
service: 服务API定义,服务端和客户端都依赖于API定义
2. 注册中心
2.1 pom配置
详细配置请参考源码。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<exclusions>
<exclusion>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</exclusion>
</exclusions>
</dependency>
2.2 配置文件
# 当前生效的profile及include的配置文件
spring:
application:
name: Eureka-Register
# eureka
server:
port: 8080
eureka:
instance:
appname: Eureka-Register
hostname: localhost
# prefer-ip-address: true
client:
register-with-eureka: true
fetch-registry: true
dashboard:
path: /eurekaui
logging:
level.com.netflix.eureka: OFF
level.com.netflix.discovery: OFF
2.3 启动类
@SpringBootApplication
@EnableEurekaServer
public class RegisterApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(RegisterApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(RegisterApplication.class);
}
}
2.4 dashboard展示
3. 服务端
3.1 pom配置
<dependency>
<groupId>com.tm.scia</groupId>
<artifactId>scia-eureka-service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<exclusions>
<exclusion>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</exclusion>
</exclusions>
</dependency>
3.2 配置文件
# 当前生效的profile及include的配置文件
spring:
application:
name: Eureka-Server
# eureka
server:
port: 8081
eureka:
instance:
appname: Eureka-Server
hostname: localhost
# prefer-ip-address: true
client:
register-with-eureka: true
fetch-registry: true
serviceUrl:
defaultZone: http://localhost:8080/eureka/
logging:
level.com.netflix.eureka: OFF
level.com.netflix.discovery: OFF
3.3 启动类
@SpringBootApplication
@EnableEurekaClient
public class ServerApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(ServerApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(ServerApplication.class);
}
}
3.4 服务实现类
@RestController
@RequestMapping("/service/helloService")
public class HelloServiceImpl implements HelloService {
@Override
@ResponseBody
@RequestMapping(value = "/sayHello", method = RequestMethod.GET)
public String sayHello(@RequestParam("userName") String userName) {
return "Hello " + userName;
}
}
3.5 dashboard展示
添加了服务端之后的dashboard
4. 客户端实现
4.1 pom配置
<dependency>
<groupId>com.tm.scia</groupId>
<artifactId>scia-eureka-service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<exclusions>
<exclusion>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</exclusion>
</exclusions>
</dependency>
4.2 配置文件
# 当前生效的profile及include的配置文件
spring:
application:
name: Eureka-Client
# eureka
server:
port: 8082
eureka:
instance:
appname: Eureka-Client
hostname: localhost
# prefer-ip-address: true
client:
register-with-eureka: true
fetch-registry: true
serviceUrl:
defaultZone: http://localhost:8080/eureka/
logging:
level.com.netflix.eureka: OFF
level.com.netflix.discovery: OFF
4.3 启动类
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableScheduling
public class ClientApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(ClientApplication.class);
}
}
4.4 Feign类
客户端通过Feign框架对服务进行调用,默认支持robbin负载均衡。
@FeignClient(value = "Eureka-Server", contextId = "helloService")
public interface HelloFeignService extends HelloService {
@Override
@RequestMapping(value = "/service/helloService/sayHello", method = RequestMethod.GET)
@ResponseBody
String sayHello(@RequestParam("userName") String userName);
}
4.5 定时调度
通过定时调度打印日志,验证接口的可用性。
@Component
public class SayHelloScheduler {
private Logger logger = LoggerFactory.getLogger(SayHelloScheduler.class);
@Autowired
private HelloService helloService;
@Scheduled(cron = "*/1 * * * * ?")
public void sayHelloPerSecond() {
logger.info(helloService.sayHello("aTang"));
}
}
4.6 客户端运行效果
4.7 dashboard展示
添加了客户端的dashboard