(四)注册中心Consul

本节内容

使用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);
	}
}

运行结果:
(四)注册中心Consul
(四)注册中心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: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();
	}
}

运行结果:
(四)注册中心Consul
(四)注册中心Consul
成功!

上一篇:.Net Core微服务——Consul(1):服务发现


下一篇:微服务系列文章目录