前言: 本文中的注册中心基于Spring Cloud(1)——服务注册中心,请先了解注册中心的相关知识后再阅读本文。
以用户服务为例,创建Maven工程microservice-provider-user
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR5</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
2、创建应用启动类UserProviderApplication.java
package com.baibei.provider.user; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; /** * @author: 会跳舞的机器人 * @email:2268549298@qq.com * @date: 17/2/17 上午9:55 * @description:用户服务提供者 */ @SpringBootApplication @EnableDiscoveryClient public class UserProviderApplication { public static void main(String[] args) { SpringApplication.run(UserProviderApplication.class, args); } }
开启服务注册功能很简单,只需要在启动类上加@EnableDiscoveryClient注解即可开启
#应用名称 spring.application.name=microservice-provider-user #服务端口 server.port=8003 #注册中心地址 eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/ #服务续租频率。默认是30秒,意思是服务提供者需要多久发送一次心跳检测至Eureka Server来确保Eureka Server知道提供者还存活着, #如果超过指定时间没有发送,Eureka Server则会从服务提供者列表中将其剔除 eureka.instance.lease-renewal-interval-in-seconds=30 #服务失效时间。默认是90秒,也就是如果Eureka Server在90秒内没有接收到来自服务提供者的Renew操作,就会把服务提供者剔除 eureka.instance.leaseExpirationDurationInSeconds=90
package com.baibei.provider.user.controller; import com.baibei.provider.user.entity.User; import com.baibei.provider.user.service.IUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author: 会跳舞的机器人 * @email:2268549298@qq.com * @date: 17/2/17 上午10:01 * @description:用户相关rest API */ @RestController @RequestMapping("/user") public class UserProviderController { @Autowired private IUserService userService; /** * 查找用户 * * @param id * @return */ @GetMapping(value = "/{id}") public User findById(@PathVariable Integer id) { User user = userService.getUser(id); return user; } }
运行UserProviderApplication的main方法,启动成功后,访问http://localhost:8003/user/5
打开 http://localhost:8761/ Eureka Server注册中心地址,可以看到microservice-provider-user服务已经注册至注册中心
附项目的完整代码包结构
本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。
我来说两句