微服务(一)SpringCloud 简介
微服务(二)SpringCloud 版本选型
微服务(三)SpringCloud 工程构建
SpringCloud —— Eureka 注册中心
SpringCloud —— Eureka 集群
SpringCloud —— 服务注册进 Eureka 集群
SpringCloud —— Eureka 自我保护机制
Consul 是一套开源的分布式服务发现和配置管理系统,由 HashiCorp 公司 用 Go 语言开发
Consul 提供了微服务系统中的服务治理、配置中心、控制总线等功能。这些功能中的每一个都可以根据需要单独使用,也可以一起使用以构建全方位的服务网格,总之 Consul 提供了一种完整的服务网格解决方案
它具有很多优点。包括:基于 raft 协议,比较简洁;支持健康检查,同时支持 HTTP 和 DNS 协议支持跨数据中心的 WAN 集群提供图形界面跨平台,支持 Linux、Mac、Windows
Consul 的下载地址 https://www.consul.io/downloads.html ,根据版本下载即可,这里下载的是 Windows 版本的
下载之后解压就一个 exe 可执行文件
查看版本,用 cmd 打开,输入 consul --version 即可
输入 consul 还可以查看所有参数
输入以下命令启动 Consul
consul agent -dev
启动 Consul 之后在浏览器输入 http://localhost:8500/ 即可访问 Consul 的控制界面
这样 Consul 的下载安装并且运行成功了
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-consul-discoveryartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>cn.hutoolgroupId>
<artifactId>hutool-allartifactId>
<version>RELEASEversion>
<scope>testscope>
dependency>
<dependency>
<groupId>cn.hutoolgroupId>
<artifactId>hutool-allartifactId>
<version>RELEASEversion>
<scope>testscope>
dependency>
dependencies>
server:
port: 8006
spring:
application:
name: consul-provider-payment
# consul 注册中心地址
cloud:
consul:
host: localhost
port: 8500 # consul 的端口号
discovery:
# hostname:127.0.0.1
service-name: ${spring.application.name}
package com.java.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @author Woo_home
* @create 2020/3/26 16:02
*/
@SpringBootApplication
@EnableDiscoveryClient
public class ConsulPaymentMain {
public static void main(String[] args){
SpringApplication.run(ConsulPaymentMain.class,args);
}
}
package com.java.springcloud.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.UUID;
/**
* @author Woo_home
* @create 2020/3/26 16:03
*/
@RestController
@Slf4j
public class PaymentController {
@Value("${server.port}")
private String serverPort;
@RequestMapping(value = "/payment/consul")
public String paymentConsul() {
return "SpringCloud with Consul : " + serverPort + "\t" + UUID.randomUUID().toString();
}
}
8006 访问 OK
而且 8006 也成功注册到 Consul
新建的步骤跟上面一样
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-consul-discoveryartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
server:
port: 80
spring:
application:
name: consul-consumer-order
# consul 注册中心地址
cloud:
consul:
host: localhost
port: 8500 # consul 的端口号
discovery:
# hostname:127.0.0.1
service-name: ${spring.application.name}
package com.java.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @author Woo_home
* @create 2020/3/26 16:14
*/
@SpringBootApplication
@EnableDiscoveryClient
public class OrderConsulMain80 {
public static void main(String[] args){
SpringApplication.run(OrderConsulMain80.class,args);
}
}
这个配置类其实就加了个 @LoadBalance 实现负载均衡的能力
package com.java.springcloud.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* @author Woo_home
* @create 2020/3/26 16:15
*/
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate getResTemplate() {
return new RestTemplate();
}
}
package com.java.springcloud.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
/**
* @author Woo_home
* @create 2020/3/26 16:17
*/
@RestController
@Slf4j
public class OrderConsulController {
// 这里调的是 consul 界面提供者的实例名称
private static final String INVOCE_URL = "http://consul-provider-payment";
@Resource
private RestTemplate restTemplate;
@GetMapping(value = "consumer/payment/consul")
public String paymentInfo() {
String result = restTemplate.getForObject(INVOCE_URL + "/payment/consul", String.class);
return result;
}
}
访问 http://localhost:8500/ui/dc1/services 可以发现已经注册到 Consul 控制界面
可以正常访问 http://localhost:8006/payment/consul
也可以正常访问 http://localhost/consumer/payment/consul
在 Consul 的控制台可以看到一直在检测服务的状态