本节内容
使用Consul作为注册中心
改造生产者
pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
配置文件
server:
port: ${port:9101}
spring:
application:
name: producer
cloud:
consul:
host: localhost
port: 8500
discovery:
service-name: ${spring.application.name}
health-check-url: http://localhost:${server.port}/actuator/health
启动类
@SpringBootApplication
@EnableDiscoveryClient
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class);
}
}
运行结果:
成功!
改造消费者
pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
配置文件
server:
port: ${port:9201}
spring:
application:
name: consumer
cloud:
consul:
host: localhost
port: 8500
discovery:
service-name: ${spring.application.name}
health-check-url: http://localhost:${server.port}/actuator/health
启动类
@SpringBootApplication
@EnableDiscoveryClient
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
运行结果:
成功!