spring cloud搭建服务注册中心并注册服务
搭建服务注册中心
添加依赖
<?xml version=”1.0” encoding=”UTF-8”?>
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.0.1.RELEASE
com.zyl
eureka-server
0.0.1-SNAPSHOT
eureka-server
Demo project for Spring Boot
UTF-8
UTF-8
1.8
Finchley SR1
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
2.0.1.RELEASE
org.springframework.boot
spring-boot-autoconfigure
2.0.4.RELEASE
compile
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
启动服务注册中心 @EnableEurekaServer
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
配置文件 (禁用客户端行为)
server.port=1111
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
由于这是一个服务注册中心,则不应该像注册中心注册自己 eureka.client.register-with-eureka=false
注册中心的职责是维护服务实例,并不需要去检索服务 eureka.client.fetch-registry=false
注册服务提供者
添加依赖
<?xml version=”1.0” encoding=”UTF-8”?>
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.0.1.RELEASE
com.zyl
demoserver
0.0.1-SNAPSHOT
demoserver
Demo project for Spring Boot
UTF-8
UTF-8
1.8
Finchley SR2
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
2.0.1.RELEASE
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
创建一个访问接口
@RestController
public class UserController {@RequestMapping(value = "/index")
public String index(){
return "Hello World";
}
}
在主服务上添加注解
@EnableEurekaClient
@SpringBootApplication
public class DemoserverApplication {public static void main(String[] args) {
SpringApplication.run(DemoserverApplication.class, args);
}
}
配置文件
spring.application.name=demo-server
eureka.client.service-url.defaultZone=http://localhost:1111/eureka/
还没有评论,来说两句吧...