在上一篇文章中我们已经成功的搭建了一个基于springcloud eureka的服务发现与注册中心,但是我们并没有向其中注入任何服务实例,接下来我将教大家如何将现有的服务注册到我们自己的eureka注册中心。
注:基于demo-springboot
1.打开pom.xml加入相关依赖:
<!--eureka client--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> <version>1.4.0.RELEASE</version> </dependency>
2.打开application-dev.properties并加入以下内容:
#实例名 spring.application.name=demo-springboot #注册中心 eureka.server.host=localhost eureka.server.port=8761 eureka.client.service-url.defaultZone=http://${eureka.server.host}:${eureka.server.port}/eureka/ #feign read timeout(10s) hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=10000 #feign read timout disable #hystrix.command.default.execution.timeout.enabled=false #开启eureka client健康检查 eureka.client.healthcheck=true # 续约到期时间(默认90秒) eureka.instance.lease-expiration-duration-in-seconds=30 # 续约更新时间间隔(默认30秒) eureka.instance.lease-renewal-interval-in-seconds=10
3.打开启动类加入相应注解@EnableEurekaClient:
@EnableEurekaClient @EnableAutoConfiguration @MapperScan({"com.example.demo.dao"}) @SpringBootApplication public class DemoSpringbootApplication { public static void main(String[] args) { SpringApplication.run(DemoSpringbootApplication.class, args); } }
右键run application,
浏览器查看我们的服务已经注册进去了。
注:如果我们想对同一个服务注册多个实例呢?(即服务实例集群),我们只需要复制一份项目修改一下端口重新打包发布即可,相信看到这里有的童鞋已经明白了我的意图:为以后的分布式服务做铺垫。