目录
一、Eureka
(一)搭建单机版Eureka
1、建立子模块
<artifactId>cloud-eureka-server7001</artifactId>
2、pom文件
<dependencies>
<!-- eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- 引入自己定义的api通用包 -->
<dependency>
<groupId>com.fx.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<!-- boot web actuator -->
<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>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
3、application.yml
server:
port: 7001
eureka:
instance:
hostname: localhost # eureka服务器的实例名称
client:
# false表示不注册自己
register-with-eureka: false
# false表示自己就是注册中心,不需要去检索服务
fetch-registry: false
service-url:
# 设置与Eureka Server交互的地址查询服务和注册服务地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
4、创建启动项
@SpringBootApplication
// Eureka服务端
@EnableEurekaServer
public class EurekaMain7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaMain7001.class, args);
}
}
5、将支付模块8001加入Eureka
在8001的pom中引入:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
在8001中的yml文件添加:
eureka:
client:
# 将自身注册到EurekaServer
register-with-eureka: true
# true,表示从EurekaServer抓取已有的注册信息
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka/
在8001的主启动添加注解:
6、将订单模块80加入Eureka
操作同上
注:添加application名称
spring:
application:
name:
(二)Eureka群集
1、新建cloud-eureka-server7002模块(同7001一致,修改端口为7002)
2、修改映射配置文件(C:\Windows\System32\drivers\etc)
在hosts文件结尾添加:
127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com
3、7001与7002相互映射(修改yml文件)
# 7001配置
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com # eureka服务器的实例名称
client:
# false表示不注册自己
register-with-eureka: false
# false表示自己就是注册中心,不需要去检索服务
fetch-registry: false
service-url:
# 设置与Eureka Server交互的地址查询服务和注册服务地址,多个以逗号隔开
defaultZone: http://eureka7002.com:7002/eureka/
# 7002配置
server:
port: 7002
eureka:
instance:
hostname: eureka7002.com # eureka服务器的实例名称
client:
# false表示不注册自己
register-with-eureka: false
# false表示自己就是注册中心,不需要去检索服务
fetch-registry: false
service-url:
# 设置与Eureka Server交互的地址查询服务和注册服务地址,多个以逗号隔开
defaultZone: http://eureka7001.com:7001/eureka/
4、将支付模块、订单模块注册到群集
修改个模块中的yml文件,修改defaultZone项为:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
5、新建cloud-provider-payment8002模块,与8001一致
修改yml文件的端口为8002
6、修改cloud-consumer-order80的Controller文件
修改远程访问地址为:
public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";
7、开启负载均衡
在order80模块中,在ApplicationContextConfig中的RestTemplate添加注解:
@LoadBalanced
(三)actuator信息完善(可选)
修改yml文件(8001、8002都修改)
eureka:
client:
# 将自身注册到EurekaServer
register-with-eureka: true
# true,表示从EurekaServer抓取已有的注册信息
fetch-registry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
instance:
instance-id: payment8001
prefer-ip-address: true # 访问路径可显示IP地址
(四)服务发现Discovery
在启动类添加注解:
@EnableDiscoveryClient
测试Discovery:
@Resource
private DiscoveryClient discoveryClient;
@GetMapping(value = "/payment/discovery")
public Object discovery() {
List<String> services = discoveryClient.getServices();
for (String service : services) {
log.info("service: " + service);
}
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 this.discoveryClient;
}
二、Zookeeper
(一)支付模块注册到zookeeper
1、添加pom
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 引入自定义API通用包 -->
<dependency>
<groupId>com.fx.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<!-- 整合zookeeper客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</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>
2、添加application.yml
server:
port: 8004
# 服务别名
spring:
application:
name: cloud-provider-payment
cloud:
zookeeper:
connect-string: docker2:2181,docker3:2181 # 群集以逗号隔开
3、测试controller
public class PaymentController {
@Value("${server.port}")
private String serverPort;
@GetMapping("/payment/zk")
public String paymentzk() {
return "springcloud with zookeeper: " + serverPort + "\t" + UUID.randomUUID().toString();
}
}
4、在zookeeper客户端获取注册信息
[zk: localhost:2181(CONNECTED) 10] get /services/cloud-provider-payment/5804fd52-a3d5-4e1f-81ba-8c0b66b7c13b
{"name":"cloud-provider-payment","id":"5804fd52-a3d5-4e1f-81ba-8c0b66b7c13b","address":"192.168.10.101","port":8004,"sslPort":null,"payload":{"@class":"org.springframework.cloud.zookeeper.discovery.ZookeeperInstance","id":"application-1","name":"cloud-provider-payment","metadata":{}},"registrationTimeUTC":1617676586598,"serviceType":"DYNAMIC","uriSpec":{"parts":[{"value":"scheme","variable":true},{"value":"://","variable":false},{"value":"address","variable":true},{"value":":","variable":false},{"value":"port","variable":true}]}}
(二)订单模块注册到zookeeper
与支付模块类似,这里不再重复累赘
三、Consul
(一)下载并启动Consul
1、下载地址
https://www.consul.io/downloads
2、安装并启动(Linux版本)
解压压缩包,并执行命令:
cp consul /usr/local/bin/
启动:
consul agent -dev -client=0.0.0.0
访问页面:
http://192.168.3.12:8500/ui/dc1/services
(二)服务提供者注册到Consul
1、pom文件
<dependencies>
<!-- consul -->
<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>
<!-- 引入自定义API通用包 -->
<dependency>
<groupId>com.fx.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</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>
2、application.yml文件
server:
port: 8006
spring:
application:
name: consul-provider-payment
cloud:
consul:
host: 192.168.3.12
port: 8500
discovery:
service-name: ${spring.application.name}
# 默认关闭了心跳检测
heartbeat:
enabled: true
其他测试方法同上,这里不再重复。