下载启动
1、下载地址
Consul Versions | HashiCorp Releaseshttps://releases.hashicorp.com/consul/
2、启动
consul agent -dev
3、默认地址
localhost:8500
服务提供者
pom
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
yml
server: port: 8006 spring: application: name: consul-provider-payment cloud: consul: discovery: service-name: ${spring.application.name} host: localhost port: 8500
主启动
@SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain8006 {
public static void main(String[] args){
SpringApplication.run(PaymentMain8006.class,args);
}
}
controller
@RestController
@Slf4j
public class PaymentController {
@Value("${server.port}")
private String serverPort;
@RequestMapping("/payment/consul")
public String paymentConsul(){
return "cloud with consul"+ serverPort+ UUID.randomUUID();
}
}
服务消费者类似于提供者
pom和yml内容大致相同
ap(Eureka):高可用、分区容错性
cp(zookeeper、Consul):强一致性,分区容错性