1、支付服务注册进zookeeper
zookeeper是一个分布式协调工具,可以实现注册中心功能
关闭Linux服务器防火墙后,启动zookeeper服务器
用到的Linux命令行:
-
systemctl stop firewalld
关闭防火墙 -
systemctl status firewalld
查看防火墙状态
-ifconfig
查看IP地址 -
ping
查验结果
zookeeper服务器取代Eureka服务器,zk作为服务注册中心
1.1、服务提供者
1.新建名为cloud-provider-payment8004的Maven工程
2.POM
<?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>springcloud</artifactId>
<groupId>com.lian.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-provider-payment8004</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!-- SpringBoot整合Web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency><!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
<groupId>com.lun.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<!-- SpringBoot整合zookeeper客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
<!--先排除自带的zookeeper3.5.3 防止与3.4.9起冲突-->
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--添加zookeeper3.4.9版本-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.9</version>
</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>
</project>
3.yaml
#8004表示注册到zookeeper服务器的支付服务提供者端口号
server:
port: 8004
#服务别名----注册zookeeper到注册中心名称
spring:
application:
name: cloud-provider-payment
cloud:
zookeeper:
#zookeeper的默认端口是2181
connect-string: 127.0.0.1:2181
4、controller层
package com.lian.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;
@RestController
@Slf4j
public class PaymentController {
//可以获得端口号8004
@Value("${server.port}")
private String serverPort;
//使用uuid这样每次访问都会显示是端口号8004带着不同的主键id
@RequestMapping(value = "/payment/zk")
public String paymentzk(){
//UUID是javaJDK提供的一个自动生成主键的方法
return "springcloud with zookeeper: "+serverPort+"\t"+ UUID.randomUUID().toString();
}
}
5、启动8004注册进zookeeper(要先启动zookeeper的server)
#zookeeper所在目录
[root@iZ2ze5v2vdwv6veyksylhxZ conf]# cd /opt/software/
[root@iZ2ze5v2vdwv6veyksylhxZ software]# ls
zookeeper-3.4.6 zookeeper-3.4.6.tar.gz
[root@iZ2ze5v2vdwv6veyksylhxZ software]# cd zookeeper-3.4.6
[root@iZ2ze5v2vdwv6veyksylhxZ zookeeper-3.4.6]# ls
bin dist-maven LICENSE.txt src zookeeper-3.4.6.jar.sha1
build.xml docs NOTICE.txt zkData
CHANGES.txt ivysettings.xml README_packaging.txt zookeeper-3.4.6.jar
conf ivy.xml README.txt zookeeper-3.4.6.jar.asc
contrib lib recipes zookeeper-3.4.6.jar.md5
[root@iZ2ze5v2vdwv6veyksylhxZ zookeeper-3.4.6]# cd bin/
[root@iZ2ze5v2vdwv6veyksylhxZ bin]# ls
README.txt zkCli.cmd zkEnv.cmd zkServer.cmd zookeeper.out
zkCleanup.sh zkCli.sh zkEnv.sh zkServer.sh
#启动服务端
[root@iZ2ze5v2vdwv6veyksylhxZ bin]# ./zkServer.sh start
JMX enabled by default
Using config: /opt/software/zookeeper-3.4.6/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
#查看进程是否启动
[root@iZ2ze5v2vdwv6veyksylhxZ bin]# jps
9555 QuorumPeerMain
10612 Jps
3909
9593 ZooKeeperMain
#查看状态,显示standalone表示正常
[root@iZ2ze5v2vdwv6veyksylhxZ bin]# ./zkServer.sh status
JMX enabled by default
Using config: /opt/software/zookeeper-3.4.6/bin/../conf/zoo.cfg
Mode: standalone
#客户端启动,端口号提示2181
[root@iZ2ze5v2vdwv6veyksylhxZ bin]# ./zkCli.sh
Connecting to localhost:2181
2021-03-11 14:58:51,777 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.6-1569965, built on 02/20/2014 09:09 GMT
2021-03-11 14:58:51,781 [myid:] - INFO [main:Environment@100] - Client environment:host.name=iZ2ze5v2vdwv6veyksylhxZ
2021-03-11 14:58:51,781 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.8.0_141
2021-03-11 14:58:51,783 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2021-03-11 14:58:51,783 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/usr/local/jdk/jdk1.8.0_141/jre
2021-03-11 14:58:51,783 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/opt/software/zookeeper-3.4.6/bin/../build/classes:/opt/software/zookeeper-3.4.6/bin/../build/lib/*.jar:/opt/software/zookeeper-3.4.6/bin/../lib/slf4j-log4j12-1.6.1.jar:/opt/software/zookeeper-3.4.6/bin/../lib/slf4j-api-1.6.1.jar:/opt/software/zookeeper-3.4.6/bin/../lib/netty-3.7.0.Final.jar:/opt/software/zookeeper-3.4.6/bin/../lib/log4j-1.2.16.jar:/opt/software/zookeeper-3.4.6/bin/../lib/jline-0.9.94.jar:/opt/software/zookeeper-3.4.6/bin/../zookeeper-3.4.6.jar:/opt/software/zookeeper-3.4.6/bin/../src/java/lib/*.jar:/opt/software/zookeeper-3.4.6/bin/../conf:.:/usr/local/jdk/jdk1.8.0_141/lib:/usr/local/jdk/jdk1.8.0_141/jre/lib:
2021-03-11 14:58:51,783 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/opt/rh/devtoolset-9/root/usr/lib64:/opt/rh/devtoolset-9/root/usr/lib:/opt/rh/devtoolset-9/root/usr/lib64/dyninst:/opt/rh/devtoolset-9/root/usr/lib/dyninst:/opt/rh/devtoolset-9/root/usr/lib64:/opt/rh/devtoolset-9/root/usr/lib:/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2021-03-11 14:58:51,783 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2021-03-11 14:58:51,783 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=<NA>
2021-03-11 14:58:51,783 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux
2021-03-11 14:58:51,784 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=amd64
2021-03-11 14:58:51,784 [myid:] - INFO [main:Environment@100] - Client environment:os.version=3.10.0-1160.11.1.el7.x86_64
2021-03-11 14:58:51,784 [myid:] - INFO [main:Environment@100] - Client environment:user.name=root
2021-03-11 14:58:51,784 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/root
2021-03-11 14:58:51,784 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/opt/software/zookeeper-3.4.6/bin
2021-03-11 14:58:51,785 [myid:] - INFO [main:ZooKeeper@438] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@506c589e
2021-03-11 14:58:51,820 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@975] - Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
Welcome to ZooKeeper!
JLine support is enabled
2021-03-11 14:58:51,932 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@852] - Socket connection established to localhost/127.0.0.1:2181, initiating session
2021-03-11 14:58:51,955 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1235] - Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x178201471b80000, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
#查看
[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper]
[zk: localhost:2181(CONNECTED) 1]
6、验证测试1:浏览器 - http://localhost:8004/payment/zk
7、验证测试2 :接着用zookeeper客户端操作
#idea启动服务8004后,再次在zookeeper查询
[zk: localhost:2181(CONNECTED) 2] ls /
[services, zookeeper]
#查询启动注册在zookeeper里的服务的id
[zk: localhost:2181(CONNECTED) 3] ls /services/cloud-provider-payment
[151b3558-b766-4b08-b847-d911364ffacc]
#查询启动注册在zookeeper里的服务的详细信息
[zk: localhost:2181(CONNECTED) 4] get /services/cloud-provider-payment/151b3558-b766-4b08-b847-d911364ffacc
#生成了json字符串
{"name":"cloud-provider-payment","id":"151b3558-b766-4b08-b847-d911364ffacc","address":"localhost","port":8004,"sslPort":null,"payload":{"@class":"org.springframework.cloud.zookeeper.discovery.ZookeeperInstance","id":"application-1","name":"cloud-provider-payment","metadata":{}},"registrationTimeUTC":1615446314041,"serviceType":"DYNAMIC","uriSpec":{"parts":[{"value":"scheme","variable":true},{"value":"://","variable":false},{"value":"address","variable":true},{"value":":","variable":false},{"value":"port","variable":true}]}}
cZxid = 0x8
ctime = Thu Mar 11 15:01:08 CST 2021
mZxid = 0x8
mtime = Thu Mar 11 15:01:08 CST 2021
pZxid = 0x8
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x178201471b80001
dataLength = 530
numChildren = 0
[zk: localhost:2181(CONNECTED) 6]
json格式化后的效果
{
"name": "cloud-provider-payment",
"id": "151b3558-b766-4b08-b847-d911364ffacc",
"address": "localhost",
"port": 8004,
"sslPort": null,
"payload": {
"@class": "org.springframework.cloud.zookeeper.discovery.ZookeeperInstance",
"id": "application-1",
"name": "cloud-provider-payment",
"metadata": {}
},
"registrationTimeUTC": 1615446314041,
"serviceType": "DYNAMIC",
"uriSpec": {
"parts": [{
"value": "scheme",
"variable": true
}, {
"value": "://",
"variable": false
}, {
"value": "address",
"variable": true
}, {
"value": ":",
"variable": false
}, {
"value": "port",
"variable": true
}]
}
}
小结
先启动zookeeper,再启动服务8004,这样服务就注册到了zookeeper注册中心,从zookeeper里就可以查询到注册服务的详细信息。
注:ZooKeeper的服务节点是临时节点
1.2、订单服务注册进zookeeper
1.新建cloud-consumerzk-order80模块
2.POM
<?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>springcloud</artifactId>
<groupId>com.lian.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-consumerzk-order80</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!-- SpringBoot整合Web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- SpringBoot整合zookeeper客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
<!--先排除自带的zookeeper-->
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--添加zookeeper3.4.9版本-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.9</version>
</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>
</project>
3.YML
server:
port: 80
#服务别名----注册zookeeper到注册中心名称
spring:
application:
name: cloud-consumer-order #服务名称下可能有n个具体实例
cloud:
zookeeper:
connect-string: 8.131.84.120:2181 #公网id+zookeeper端口
4.主启动
package com.lian.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient //开启服务发现
public class OrderZKMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderZKMain80.class,args);
}
}
5.config配置类
package com.lian.springcloud.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
//将restTemplate注入到spring容器里
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced //客户端负载均衡,客户端统一访问入口是服务名,然后会轮询访问到服务下的具体实例
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
6.controller层
package com.lian.springcloud.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class OrderZKController {
//此处写的是服务名,服务名下可能有n个具体服务的实例
private static final String INVOKE_URL = "http://cloud-provider-payment";
@Autowired
RestTemplate restTemplate;
@GetMapping(value = "/consumer/payment/zk")
public String paymentInfo(){
return restTemplate.getForObject(INVOKE_URL+"/payment/zk",String.class);
}
}
7.验证测试
运行ZooKeeper服务端,cloud-consumerzk-order80,cloud-provider-payment8004
打开ZooKeeper客户端:
[zk: localhost:2181(CONNECTED) 6] ls /
[services, zookeeper]
#有两个服务注入zookeeper注册中心
[zk: localhost:2181(CONNECTED) 7] ls /services
[cloud-provider-payment, cloud-consumer-order]
[zk: localhost:2181(CONNECTED) 8]
#想知道某个服务更清晰的信息,参考上面zookeeper查询提供者详细信息的例子
2、Consul
2.1、介绍
Consul是一套开源的分布式服务发现和配置管理系统,由HashiCorp 公司用Go语言开发。
提供了微服务系统中的服务治理、配置中心、控制总线等功能。这些功能中的每一个都可以根据需要单独使用,也可以一起使用以构建全方位的服务网格,总之Consul提供了一种完整的服务网格解决方案。
Consul有什么用?
- 服务发现 - 提供HTTP和DNS两种发现方式。
- 健康监测 - 支持多种方式,HTTP、TCP、Docker、Shell脚本定制化
- KV存储 - Key、Value的存储方式
- 多数据中心 - Consul支持多数据中心
- 可视化Web界面
2.2、安装Consul
windows版解压缩后,得consul.exe,打开cmd
- 查看版本consul -v
- 开发模式启动consul agent -dev
浏览器输入 - http://localhost:8500/ - 打开Consul控制页
2.3、服务提供者注册进Consul
1.新建Module支付服务cloud-providerconsul-payment8006
2.POM
<?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>springcloud</artifactId>
<groupId>com.lian.springcloud.OrderZKMain80</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-providerconsul-payment8006</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
<dependency>
<groupId>com.lian.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<!--SpringCloud consul-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<!-- SpringBoot整合Web组件 -->
<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>
<!--日常通用jar包配置-->
<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>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
3.yaml
server:
port: 8006
#服务名
spring:
application:
name: consul-provider-payment
#consul注册中心地址
cloud:
consul:
host: localhost
port: 8500
discovery:
#hostname: 127.0.0.1
service-name: ${spring.application.name}
4.主启动
package com.lian.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain8006 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8006.class,args);
}
}
5.业务类controller
package com.lian.springcloud.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.UUID;
@RestController
public class PaymentController {
@Value("${server.port}")
private String serverPort;
@GetMapping(value = "/payment/consul")
public String paymentConsul(){
return "springcloud with consul: "+serverPort+"\t "+ UUID.randomUUID().toString();
}
}
6.测试
http://localhost:8006/payment/consul
http://localhost:8500 - 会显示provider8006
2.4、服务消费者注册进Consul
1、新建Module消费服务cloud-consumerconsul-order80
2、pom
<?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>springcloud</artifactId>
<groupId>com.lian.springcloud.OrderZKMain80</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-consumerconsul-order80</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!--SpringCloud consul-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<!-- SpringBoot整合Web组件 -->
<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>
<!--日常通用jar包配置-->
<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>
</project>
3、yaml
#consul服务端口号
server:
port: 80
spring:
application:
name: cloud-consumer-order
#consul注册中心地址
cloud:
consul:
host: localhost
port: 8500
discovery:
service-name: ${spring.application.name}
4、主启动
package com.lian.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class OrderConsulMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderConsulMain80.class,args);
}
}
5、业务类config
package com.lian.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;
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
6、controller层
package com.lian.springcloud.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class OrderConsulController {
private static final String INVOKE_URL = "http://consul-provider-payment";
@Autowired
RestTemplate restTemplate;
@GetMapping(value = "/consumer/payment/consul")
public String paymentInfo(){
return restTemplate.getForObject(INVOKE_URL+"/payment/consul",String.class);
}
}
7、测试
运行consul,cloud-providerconsul-payment8006,cloud-consumerconsul-order80
http://localhost:8500/ 主页会显示出consul,cloud-providerconsul-payment8006,cloud-consumerconsul-order80三服务
3、三个注册中心异同点
3.1、CAP:
- C:Consistency (强一致性)
- A:Availability (可用性)
- P:Partition tolerance (分区容错性)
最多只能同时较好的满足两个
CAP理论的核心是:一个分布式系统不可能同时很好的满足一致性,可用性和分区容错性这三个需求。
3.2、根据CAP原理将NoSQL数据库分成了满足CA原则、满足CP原则和满足AP原则三大类:
- CA - 单点集群,满足—致性,可用性的系统,通常在可扩展性上不太强大
- CP - 满足一致性,分区容忍必的系统,通常性能不是特别高
- AP - 满足可用性,分区容忍性的系统,通常可能对一致性要求低一些
3.3、AP架构(Eureka)
当网络分区出现后,为了保证可用性,系统B可以返回旧值,保证系统的可用性。
结论:违背了一致性C的要求,只满足可用性和分区容错,即AP
3.4、CP架构(ZooKeeper/Consul)
当网络分区出现后,为了保证一致性,就必须拒接请求,否则无法保证一致性。
结论:违背了可用性A的要求,只满足一致性和分区容错,即CP。