发布时间:2022-08-19 14:30
zookeeper作为注册中心集群版
cloud-providerzk-payment8004
cloud-providerzk-payment8005
cloud-providerzk-payment8006
每个项目的依赖都是相同的
springboot与cloud的版本为
依赖
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-zookeeper-discovery
org.apache.zookeeper
zookeeper
org.apache.zookeeper
zookeeper
3.4.14
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-devtools
runtime
true
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
每一个项目的配置文件都大致相同,只有port不同 8004/8005/8006
server:
port: 8004
#服务别名 ---注册zookeeper到服务中心
spring:
application:
name: cloud-provider-payment
cloud:
zookeeper:
connect-string: 192.168.127.84:2181
新建com.lejia.springcloud包
主启动类 PaymentMain8005.java
@SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain8005 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8005.class, args);
}
}
controller.PaymentController.java
@RestController
@RequestMapping("/payment")
@Slf4j
public class PaymentController {
@Value("${server.port}")
private String serverPort;
//暴露给用户的接口
@GetMapping(value="/zk")
public String paymentzk(){
return "SpringCloud with zookeeper:"+serverPort+"\t"+ UUID.randomUUID().toString();
}
}
8004 / 8005 / 8006 (笔者这里8006端口被占用了,暂时使用8008)
cloud-consumerzk-order80
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-zookeeper-discovery
org.apache.zookeeper
zookeeper
org.apache.zookeeper
zookeeper
3.4.9
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-devtools
runtime
true
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
server:
port: 80
#服务别名 ---注册zookeeper到服务中心
spring:
application:
name: cloud-consumer-order
cloud:
zookeeper:
connect-string: 192.168.127.84:2181
@SpringBootApplication
@EnableDiscoveryClient
public class OrderZKMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderZKMain80.class, args);
}
}
新建config包
新建配置类ApplicationContextConfig.claas
创建RestTemplate对象进行远程服务的调用
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
新建controller包
新建服务消费者方法,并远程调用提供者的服务
@RestController
@Slf4j
@RequestMapping("/consumer")
public class OrderZKController {
private final String INVOCATION_URL = "http://cloud-provider-payment";
@Resource
private RestTemplate restTemplate;
@GetMapping("/payment/zk")
public String payment(){
return restTemplate.getForObject(INVOCATION_URL+"/payment/zk", String.class);
}
}
http://localhost/consumer/payment/zk
当我们调用消费者服务接口时,成功的访问到了服务端提供的集群服务
对于访问策略这方面,后续待我们学习了ribbon后可以再将其根据具体的情况来设置!
本周二晚19:00战码先锋第5期直播丨深入理解OpenHarmony系统启动,轻松踏上设备软件开发之旅
【云原生&微服务三】SpringCloud之Ribbon是这样实现负载均衡的(源码剖析@LoadBalanced原理)
物联网之MQTT3.1.1和MQTT5协议 (1) 术语部分
linux到底是个什么系统,完美的Linux发行版到底该是个什么样儿
姿态估算03-06:mmpose(Associative embedding)-源码无死角解析(2)-训练架构总览
Simulink的Variant Model和Variant Subsystem用法