1.创建模块
2.添加依赖 依赖对比
<dependencies>
<!-- eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- 引用自己定义的api通用包,可以使用Payment支付Entity -->
<dependency>
<groupId>com.zhao.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</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>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
3.编写yml文件
server:
port: 7001
eureka:
instance:
hostname: localhost #eureka服务端的实例名称
client:
#false表示不向注册中心注册自己(想注册也可以,不过没必要)
register-with-eureka: false
#false表示自己端就是注册中心,职责就是维护服务实例,并不需要去检索服务
fetch-registry: false
service-url:
#设置与eurekaServer交互的地址查询服务和注册服务都需要依赖这个地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
4.主启动类
@SpringBootApplication
@EnableEurekaServer
public class EurekaMain7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaMain7001.class);
}
}
5.测试
访问:http://localhost:7001/
client依赖对比:
1.引入依赖
<!-- eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2.修改yml,添加:
eureka:
client:
#true表示向注册中心注册自己,默认为true
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka
3.修改启动类
@SpringBootApplication
@EnableEurekaClient
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class);
}
}
4.启动项目,注册成功!
步骤i同 将EurekaClient端8001注册进EurekaServer成为服务提供者provider !!!
搭建Eureka注册中心集群,实现负载均衡+故障容错。
Eureka集群:相互注册,相互守望
C:\Windows\System32\drivers\etc
127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com
127.0.0.1 eureka7003.com
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com #eureka服务端的实例名称
client:
register-with-eureka: false
fetch-registry: false
service-url:
# 单机
# defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
#集群版 相互注册,相互守望
defaultZone: http://eureka7002.com:7002/eureka/
server:
port: 7002
eureka:
instance:
hostname: eureka7002.com #eureka服务端的实例名称
client:
register-with-eureka: false
fetch-registry: false
service-url:
#集群版 相互注册,相互守望
defaultZone: http://eureka7001.com:7001/eureka/ #相互注册,相互守望
#集群版
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
集群搭建完成!!
public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";
@LoadBalanced
,开启负载均衡功能。重启测试 !
修改8001服务和8002服务的yml
eureka:
client:
#true表示向注册中心注册自己,默认为true
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetch-registry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
# 新增
instance:
instance-id: payment8001
prefer-ip-address: true
# 8002 yml
instance:
instance-id: payment8002
prefer-ip-address: true
对于注册进eureka里面的微服务,可以通过服务发现来获得该服务的信息。
@Resource
private DiscoveryClient discoveryClient;
@GetMapping("/payment/discovery")
public Object discovery(){
List<String> services = discoveryClient.getServices();
for (String element : services) {
log.info("*******element:"+element);
}
List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
for (ServiceInstance instance : instances) {
log.info(instance.getServiceId()+"\t"+instance.getHost()+"\t"+instance.getPort()+"\t"+instance.getUri());
}
return discoveryClient;
}
@EnableDiscoveryClient
禁止自我保护机制
cloud-eureka-server7001的yml文件:
eureka:
......
server:
#关闭自我保护,默认为true
enable-self-preservation: false
#心跳的间隔时间,单位毫秒
eviction-interval-timer-in-ms: 2000
cloud-provider-payment8001的yml文件:
#Eureka客户端向服务端发送心跳的时间间隔,单位秒(默认30秒)
lease-renewal-interval-in-seconds: 1
#Eureka服务端在收到最后一次心跳后等待的时间上限,单位秒(默认90秒),超时剔除服务
lease-expiration-duration-in-seconds: 2
在服务器中部署Zookeeper服务,此处使用docker部署
#拉取Zookeeper镜像
docker pull zookeeper
#启动Zookeeper
docker run -d --name zk01 -p 2181:2181 --restart always zookeeper
关闭防火墙
systemctl stop firewalld
systemctl status firewalld
1.新建模块
2.修改POM
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>
3.写yml
server:
port: 8004
spring:
application:
name: cloud-provider-payment
cloud:
zookeeper:
connect-string: xxxxxx
4.主启动
@SpringBootApplication
@EnableDiscoveryClient // //该注解用于向使用consul或者Zookeeper作为注册中心时注册服务
public class PaymentMain8004 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8004.class);
}
}
5.controller
@RestController
@Slf4j
public class PaymentController {
@Value("${server.port}")
private String serverPort;
@GetMapping("/payment/zk")
public String paymentzk(){
return "springcloud with zookeeper: " +serverPort+"\t"+ UUID.randomUUID().toString();
}
}
6.启动zk容器
7.启动测试
8.进入Zookeeper容器(成功注册进注册中心)
json工具:https://tool.lu/json/
服务节点是临时节点还是持久节点? zookeeper也是有心跳机制,在一定时间能如果一直没心跳返回,Zookeeper就会把服务节点剔除掉。所以在Zookeeper上的服务节点是临时节点。
1.新建消费者模块cloud-consumerzk-order80。
2.pom和yml直接复制8004。(yml中端口号改为80,应用名改为cloud-consumer-order,其他都相同) 3.主启动类。(与8004相同) 4.创建配置类
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
6.controller
@RestController
@Slf4j
public class OrderZKController {
public static final String INVOKE_URL = "http://cloud-provider-payment";
@Resource
private RestTemplate restTemplate;
@GetMapping("/consumer/payment/zk")
public String paymentInfo(){
String result = restTemplate.getForObject(INVOKE_URL + "/payment/zk", String.class);
return result;
}
}
7.启动项目
Consul官网:https://www.consul.io/ Consul中文文档:https://www.springcloud.cc/spring-cloud-consul.html
Consul是一种服务网络解决方案,使团队能够管理服务之间以及跨多云环境和运行时的安全网络连接。Consul提供服务发现、基于身份的授权、L7流量管理和服务到服务加密。
Consul旨在对DevOps社区和应用程序开发人员友好,使其非常适合现代,灵活的基础架构。
docker安装Consul
#拉取consul镜像
docker pull consul
#启动consul
docker run -d -p 8500:8500/tcp --name myConsul consul agent -server -ui -bootstrap-expect=1 -client=0.0.0.0
测试访问
1.新建模块
2.修改POM,其他和之前一样,将zookeeper替换如下
<!--SpringCloud consul-server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
3.编写YML
server:
port: 8006
spring:
application:
name: consul-provider-payment
cloud:
consul:
host: 139.196.177.161 #用linux的ip地址(consul在本机就填localhost)
port: 8500
discovery:
service-name: ${spring.application.name}
prefer-ip-address: true
register-health-check: false
4.主启动
@SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain8006 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8006.class);
}
}
5.业务类
@RestController
@Slf4j
public class PaymentController {
@Value("${server.port}")
private String serverPort;
@GetMapping("/payment/consul")
public String paymentConsul(){
return "springcloud with zookeeper: " +serverPort+"\t"+ UUID.randomUUID().toString();
}
}
6.测试启动
1.新建模块cloud-consumer-consul-order80
3.yml(端口号为80,应用名为consul-consumer-order,其他和8006相同)
server:
port: 80
spring:
application:
name: consul-consumer-order
cloud:
consul:
host: 139.196.177.161 #用linux的ip地址(consul在本机就填localhost)
port: 8500
discovery:
service-name: ${spring.application.name}
prefer-ip-address: true
register-health-check: false
4.主启动类(与8006相同)
@SpringBootApplication
@EnableDiscoveryClient
public class OrderConsulMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderConsulMain80.class);
}
}
5.config(和zk的消费者相同) 6.controller
@RestController
@Slf4j
public class OrderConsulController {
public static final String INVOKE_URL = "http://consul-provider-payment";
@Resource
private RestTemplate restTemplate;
@GetMapping("/consumer/payment/consul")
public String paymentInfo(){
String result = restTemplate.getForObject(INVOKE_URL + "/payment/consul", String.class);
return result;
}
}
7.测试
AP:
CP: