第十节--consul服务注册与发现
上一节将zookeeper结合springcloud服务注册与发现,但是使用的公司比较少
一般都使用consul
官网下载consul,安装并运行Consul: 下载好之后,直接解压运行consul.exe即可,使用cmd进入consul加压目录查看版本:consul --version
启动consul, 进入consul.exe目录,cmd运行命令:consul agent -dev
启动成功之后:访问 http://localhost:8500 即可
服务提供者注册consul
第一步:新建工程,cloud-provider-consul-payment8006 ,pom.xml文件新增依赖
<dependencies> <!--整合consul服务注册--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <!-- actuator监控信息完善 这两个包必须有--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>com.hx.springcloud</groupId> <artifactId>cloud-api-commos</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <!--热部署 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> </dependencies>
第二步:yml文件
server:
port: 8006
spring:
application:
name: cloud-payment-service #在consul注册中心的名称
cloud:
consul:
host: localhost
port: 8500 #不能写错 否则启动错误
discovery:
service-name: ${spring.application.name}
第三步:主启动类
@EnableDiscoveryClient //向使用consol或zookeeper作为注册中心时注册服务 @SpringBootApplication public class PayMentApplication8006 { public static void main(String[] args) { SpringApplication.run(PayMentApplication8006.class,args); } }
其它的controller,service等和其它工程一致
服务消费者注册consul
第一步:新建工程cloud-consumer-consulorder80,pom.xml文件
<dependencies> <!--整合consul服务注册--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <!-- actuator监控信息完善 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>com.hx.springcloud</groupId> <artifactId>cloud-api-commos</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--热部署 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> </dependencies>
第二步:yml文件
server:
port: 80
spring:
application:
name: cloud-consumer-service #在consul注册中心的名称
cloud:
consul:
host: localhost
port: 8500
discovery:
service-name: ${spring.application.name}
第三步:主启动类
@EnableDiscoveryClient @SpringBootApplication public class ConsulOrder80 { public static void main(String[] args) { SpringApplication.run(ConsulOrder80.class,args); } }
config和controller和zookeeper调用时一致
启动项目后,查看consul: http://localhost:8500/ui/dc1/services
成功!