spring cloud(一) 服务注册与发现 Eureka||Consul
本系列文章均参考:朝雨忆轻尘,感谢博主!
这里还有他的技术交流群:429854222,欢迎大家支持博主
若有侵权,还请告知,一定删除
Spring Cloud Eureka
server:
pom.xml:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
通过@EnableEurekaServer
注解启动一个服务注册中心
@EnableEurekaServer
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
application.yml:
server:
port: 8888
eureka:
server:
#开启自我保护
enable-self-preservation: true
instance:
#以IP地址注册
preferIpAddress: true
hostname: ${spring.cloud.client.ipAddress}
instanceId: ${spring.cloud.client.ipAddress}:${server.port}
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
client:
pom.xml:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
应用主类中通过加上@EnableEurekaClient注解,启用服务发现(@EnableDiscoveryClient亦可):
@EnableEurekaClient
//@EnableDiscoveryClient
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
application.yml:
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8888/eureka/
地址对应即可。
Spring Cloud Consul
server:
consul服务器安装详见我其他博客:传送门
client:
pom.xml:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
应用主类中通过加上@EnableDiscoveryClient注解,启用服务发现:
@EnableDiscoveryClient
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
application.yml:
spring:
application:
name: applicationName
cloud:
consul:
host: localhost
port: 8500
discovery:
serviceName: ${spring.application.name} # 注册到consul的服务名称
集群:
Eureka集群就是开启多个Eureka微服务,之间互相注册即可。然后客户端的配置defaultZone指定一个到多个都行:传送门
consul集群也类似,开启多个consul服务(当然与单机开启命令不一样),然后服务之间通过join命令来开启一个集群:传送门
好,告辞~
还没有评论,来说两句吧...