Consul注册中心
Consul介绍
Consul 是 HashiCorp 公司推出的开源工具,用于实现分布式系统的服务发现与配置。与其它分布式 服务注册与发现的方案,Consul 的方案更“一站式”,内置了服务注册与发现框架、分布一致性协议实 现、健康检查、Key/Value 存储、多数据中心方案,不再需要依赖其它工具(比如 ZooKeeper 等),使 用起来也较为简单。
Consul 使用 Go 语言编写,因此具有天然可移植性(支持Linux、Windows 和 Mac OS);安装包仅 包含一个可执行文件,方便部署,与 Docker 等轻量级容器可无缝配合。
Consul特性
- Raft算法
- 服务发现
- 健康检查
- Key/Value储存
- 多数据中心
- 支持Http和Dns协议接口
- 官方提供Web管理界面
Consul角色
Client
客户端,无状态,将Htpp和DNS接口请求转发给局域网内的服务端集群。
Server
服务端,保存配置信息,高可用集群,每个书中心的server数量推荐3个或5个。
Consule安装
1、下载Consul
https://www.consul.io/downloads
2、安装
Consul的安装及其简单,下载后的Consul程序只有一个exe文件,只需要在cmd命令下执行相应命令即可。
3、启动Consul
Consul可执行文件同级目录下执行
1 2
| # -dev表示开发模式运行,也可以使用-server,表示服务模式运行 -client=0.0.0.0,绑定客户端接口的ip consul agent -dev -client=0.0.0.0
|
为了方便启动,避免每次启动都要执行上述命令,可以编写一个批处理脚本
run.bat
1 2 3
| :: -dev表示开发模式运行,也可以使用-server,表示服务模式运行 -client=0.0.0.0,绑定客户端接口的ip consul agent -dev -client=0.0.0.0 pause
|
访问管理页面:http://localhost:8500/,如下图表示启动成功
Consul的入门使用
1、创建Maven聚合项目
- Consul-Demo:父项目
- service-consumer:子项目-服务消费者
- service-provier:子项目-服务提供者
父项目:pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>cn.yanghuisen</groupId> <artifactId>Consul-Demo</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>service-provider</module> <module>service-consumer</module> </modules> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.0.RELEASE</version> </parent>
<properties> <spring-cloud.version>Hoxton.SR5</spring-cloud.version> </properties>
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
</project>
|
2、Service-Provider:服务提供者
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>Consul-Demo</artifactId> <groupId>cn.yanghuisen</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion>
<artifactId>service-provider</artifactId>
<name>service-provider</name> <url>http://www.example.com</url>
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties>
<dependencies> <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <scope>provided</scope> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> </project>
|
application.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| server: port: 7070
spring: application: name: service-provider cloud: consul: host: localhost port: 8500 discovery: register: true instance-id: ${spring.application.name}-01 service-name: ${spring.application.name} port: ${server.port} prefer-ip-address: true ip-address: ${spring.cloud.client.ip-address}
|
服务提供者启动类:ServiceProviderApplication.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package cn.yanghuisen;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication public class ServiceProviderApplication { public static void main(String[] args) { SpringApplication.run(ServiceProviderApplication.class); } }
|
实体类:Product.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package cn.yanghuisen.pojo;
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor;
import java.io.Serializable;
@Data @NoArgsConstructor @AllArgsConstructor public class Product implements Serializable {
private Integer id; private String productName; private Integer productNum; private Double productPrice;
}
|
服务接口:ProductService.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package cn.yanghuisen.service;
import cn.yanghuisen.pojo.Product;
import java.util.List;
public interface ProductService {
List<Product> selectProductList();
}
|
服务接口实现类:ProductServiceImpl.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| package cn.yanghuisen.service.impl;
import cn.yanghuisen.pojo.Product; import cn.yanghuisen.service.ProductService; import org.springframework.stereotype.Service;
import java.util.Arrays; import java.util.List;
@Service public class ProductServiceImpl implements ProductService {
@Override public List<Product> selectProductList() { return Arrays.asList( new Product(1, "华为手机", 1, 5800D), new Product(2, "联想笔记本", 1, 6888D), new Product(3, "小米平板", 5, 2020D) ); }
}
|
控制层:ProductController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| package cn.yanghuisen.controller;
import cn.yanghuisen.pojo.Product; import cn.yanghuisen.service.ProductService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController @RequestMapping("/product") public class ProductController {
@Autowired private ProductService productService;
@GetMapping("/list") public List<Product> selectProductList() { return productService.selectProductList(); }
}
|
启动项目,查看服务提供者是否成功注册到Consul注册中心。
访问:http://localhost:8500/
按照创建provider的方式创建provider02,并修改配置文件的端口和实例ID
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| server: port: 7071
spring: application: name: service-provider cloud: consul: host: localhost port: 8500 discovery: register: true instance-id: ${spring.application.name}-02 service-name: ${spring.application.name} port: ${server.port} prefer-ip-address: true ip-address: ${spring.cloud.client.ip-address}
|
启动provider02,访问:http://localhost:8500/
3、Service-Consumer:服务消费者
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>Consul-Demo</artifactId> <groupId>cn.yanghuisen</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion>
<artifactId>service-consumer</artifactId>
<name>service-consumer</name> <url>http://www.example.com</url>
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <scope>provided</scope> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> </project>
|
application.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| server: port: 9090
spring: application: name: service-consumer cloud: consul: host: localhost port: 8500 discovery: register: false instance-id: ${spring.application.name}-01 service-name: ${spring.application.name} port: ${server.port} prefer-ip-address: true ip-address: ${spring.cloud.client.ip-address}
|
实体类:Product.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package cn.yanghuisen.pojo;
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor;
import java.io.Serializable;
@Data @NoArgsConstructor @AllArgsConstructor public class Product implements Serializable {
private Integer id; private String productName; private Integer productNum; private Double productPrice;
}
|
实体类:Order.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package cn.yanghuisen.pojo;
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor;
import java.io.Serializable; import java.util.List;
@Data @NoArgsConstructor @AllArgsConstructor public class Order implements Serializable {
private Integer id; private String orderNo; private String orderAddress; private Double totalPrice; private List<Product> productList;
}
|
消费者服务接口:OrderService.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| package cn.yanghuisen.service;
import cn.yanghuisen.pojo.Order;
public interface OrderService {
Order selectOrderById(Integer id);
}
|
消费者服务接口:OrderServiceImpl.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| package cn.yanghuisen.service.impl;
import cn.yanghuisen.pojo.Order; import cn.yanghuisen.pojo.Product; import cn.yanghuisen.service.OrderService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.ParameterizedTypeReference; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate;
import java.util.List;
@Service public class OrderServiceImpl implements OrderService {
@Autowired private RestTemplate restTemplate;
@Override public Order selectOrderById(Integer id) { return new Order(id, "order-001", "中国", 22788D, selectProductListByLoadBalancerAnnotation()); }
private List<Product> selectProductListByLoadBalancerAnnotation() { ResponseEntity<List<Product>> response = restTemplate.exchange( "http://service-provider/product/list", HttpMethod.GET, null, new ParameterizedTypeReference<List<Product>>() { }); return response.getBody(); }
}
|
控制层:OrderController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| package cn.yanghuisen.controller;
import cn.yanghuisen.pojo.Order; import cn.yanghuisen.service.OrderService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
@RestController @RequestMapping("/order") public class OrderController {
@Autowired private OrderService orderService;
@GetMapping("/{id}") public Order selectOrderById(@PathVariable("id") Integer id) { return orderService.selectOrderById(id); }
}
|
消费者启动类:ServiceConsumerApplication.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| package cn.yanghuisen;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate;
@SpringBootApplication public class ServiceConsumerApplication {
@Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); }
public static void main(String[] args) { SpringApplication.run(ServiceConsumerApplication.class, args); }
}
|
启动消费者服务,访问:http://localhost:9090/order/1