01 基礎環境準備
02 一文讀懂Eureka
03 Zookeeper註冊中心
04 Consule註冊中心
05 Ribbon
06 OpenFegin
07 Hystrix全面解析
08 Gateway全面解析
09 Config配置中心
10 Bus消息總線
1 簡介
Consul是一套開源的分佈式服務發現和配置管理系統,由 Hashicorp公司用Go語言開發。提供了微服務系統中的服務治理、配置中心、控制總線等功能。這些功能中的每個都可以根據需要單獨使用,也可以一起使用以構建全方位的服務網格,總之 Consul提供了一種完整的服務網格解決方案。
它具有很多優點。包括:基於raft協議,比較簡潔;支持健康檢查,同時支持HTTP和DNS協議支持跨數據中心的WAN集羣提供圖形界面跨平臺,支持 Linux、Mac、 Windows。
2 下載 安裝
(1) 下載
#地址
https://www.consul.io/downloads.html
(2) 安裝
#查看版本命令
consul --version
#啓動命令
consul agent -dev
3 建立工程 consul-provider
3.1 pom
<dependencies>
<!-- consul server-->
<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>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
3.2 application.yml
server:
port: 7003
spring:
application:
name: consul-provider
cloud:
consul:
host: localhost
port: 8500
discovery:
service-name: consul-provider
3.3 主啓動類
@SpringBootApplication
@EnableDiscoveryClient
public class ConsulProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ConsulProviderApplication.class);
}
}
3.4 controller
@RestController
public class ProviderController {
@Value("${server.port}")
private String serverport;
@RequestMapping("/consul")
public String zk(){
return "springcloud with consul: "+serverport;
}
}
3.5 啓動
4 建立工程 consul-customer
4.1 pom
<dependencies>
<!-- consul server-->
<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>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
4.2 application.yml
server:
port: 8002
spring:
application:
name: consul-customer
cloud:
consul:
host: localhost
port: 8500
discovery:
service-name: consul-customer
4.3 主程序
@SpringBootApplication
@EnableDiscoveryClient
public class ConsulCustomerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsulCustomerApplication.class);
}
}
4.4 配置文件
@Configuration
public class ApplicatrionConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
4.5 controller
@RestController
@RequestMapping("/consul")
public class CustomerController {
public static final String PROVIDER_URL="http://consul-provider";
@Autowired
private RestTemplate restTemplate;
@GetMapping("/customer")
public String customer(){
ResponseEntity<String> forEntity = restTemplate.getForEntity(PROVIDER_URL+"/consul", String.class);
return forEntity.getBody();
}
}
4.6 啓動
5 consul 高可用
5.1 環境準備
192.168.0.39
192.168.0.40
192.168.0.41
5.2 解壓
#3臺
unzip consul_1.5.0_linux_amd64.zip
5.3 啓動命令
(1) 命令解釋
server: 以server身份啓動。默認是client
bootstrap-expect:集羣要求的最少server數量,當低於這個數量,集羣即失效。
data-dir:data存放的目錄,更多信息請參閱consul數據同步機制
node:節點id,集羣中的每個node必須有一個唯一的名稱。默認情況下,Consul使用機器的hostname
bind:監聽的ip地址。默認綁定0.0.0.0,可以不指定。表示Consul監聽的地址,而且它必須能夠被集羣中的其他節點訪問。Consul默認會監聽第一個private IP,但最好還是提供一個。生產設備上的服務器通常有好幾個網卡,所以指定一個不會出錯
client: 客戶端的ip地址,0.0.0.0是指誰都可以訪問(不加這個,下面的ui :8500無法訪問)
ui: 可以訪問UI界面
-config-dir指定配置文件夾,Consul會加載其中的所有文件
-datacenter 指定數據中心名稱,默認是dc1
(2) 啓動集羣
#第一臺
nohup consul agent -server -bootstrap-expect=3 -data-dir=/usr/data/consul -node=192.168.0.39 -bind=192.168.0.39 -client=0.0.0.0 -datacenter=shenzhen -ui >consul.log 2>&1 &
#第二臺
nohup consul agent -server -bootstrap-expect=3 -data-dir=/usr/data/consul -node=192.168.0.40 -bind=192.168.0.40 -client=0.0.0.0 -datacenter=shenzhen -ui >consul.log 2>&1 &
#第三臺
nohup consul agent -server -bootstrap-expect=3 -data-dir=/usr/data/consul -node=192.168.0.41 -bind=192.168.0.41 -client=0.0.0.0 -datacenter=shenzhen -ui >consul.log 2>&1 &
(3) 加入節點
#第二臺,第三臺
consul join 192.168.0.39
consul join 192.168.0.39
(4) 查看集羣狀態
./consul members
(5) 訪問UI
GITHUB
#分支 consul-enviroment-release-v1.0
https://github.com/zhurongsheng666/spring-cloud-hoxton