SpringCloud学习笔记(五)_Consul注册中心

本章使用的Consul版本是 1.7.2

项目架构图如下:

搭建服务提供者

1、新建一个maven项目(test-springcloud-provider-payment8006)

结构如下:

2、引入依赖,编辑pom文件

复制代码
1 <!-- spring-cloud 整合 consul -->
2 <dependency>
3     <groupId>org.springframework.cloud</groupId>
4     <artifactId>spring-cloud-starter-consul-discovery</artifactId>
5 </dependency>

完整pom.xml文件如下:

复制代码
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <parent>
 6         <artifactId>test-springcloud</artifactId>
 7         <groupId>com.test</groupId>
 8         <version>1.0-SNAPSHOT</version>
 9     </parent>
10     <modelVersion>4.0.0</modelVersion>
11 
12     <artifactId>test-springcloud-provider-payment8006</artifactId>
13 
14     <dependencies>
15 
16         <!-- spring-cloud 整合 consul -->
17         <dependency>
18             <groupId>org.springframework.cloud</groupId>
19             <artifactId>spring-cloud-starter-consul-discovery</artifactId>
20         </dependency>
21 
22         <!-- spring boot -->
23         <dependency>
24             <groupId>org.springframework.boot</groupId>
25             <artifactId>spring-boot-starter-web</artifactId>
26         </dependency>
27         <dependency>
28             <groupId>org.springframework.boot</groupId>
29             <artifactId>spring-boot-starter-actuator</artifactId>
30         </dependency>
31 
32         <dependency>
33             <groupId>org.springframework.boot</groupId>
34             <artifactId>spring-boot-devtools</artifactId>
35             <scope>runtime</scope>
36             <optional>true</optional>
37         </dependency>
38         <dependency>
39             <groupId>org.projectlombok</groupId>
40             <artifactId>lombok</artifactId>
41             <optional>true</optional>
42         </dependency>
43 
44         <dependency>
45             <groupId>org.springframework.boot</groupId>
46             <artifactId>spring-boot-starter-test</artifactId>
47             <scope>test</scope>
48         </dependency>
49 
50     </dependencies>
51 
52     <build>
53         <finalName>test-springcloud-provider-payment8006</finalName>
54     </build>
55 
56 </project>

pom.xml

3、编辑配置文件application.yml

复制代码
 1 # 端口
 2 server:
 3   port: 8006
 4 
 5 spring:
 6   application:
 7     name: cloud-payment-service
 8   cloud:
 9     consul:
10       host: localhost
11       port: 8500
12       discovery:
13         # hostname: 127.0.0.1
14         service-name: ${spring.application.name}

4、编写主启动类

复制代码
1 @EnableDiscoveryClient
2 @SpringBootApplication
3 public class PaymentMain8006 {
4     public static void main(String[] args) {
5         SpringApplication.run(PaymentMain8006.class, args);
6     }
7 }

5、编写Controller

复制代码
 1 @RestController
 2 @Slf4j
 3 public class PaymentController {
 4 
 5     @Value("${server.port}")
 6     private String serverPort;
 7 
 8     @RequestMapping(value = "payment/consul")
 9     public String paymentconsul(){
10         return "springcloud with consul:" + serverPort + "\t" + UUID.randomUUID();
11     }
12 }

6、启动项目,测试项目

1)启动Consul服务,使用开发模式,命令:consul agent -dev

2)启动项目(test-springcloud-provider-payment8006)

3)使用地址:http://localhost:8006/payment/consul

4)打开Consul的界面,地址:http://localhost:8500/

搭建服务消费者

1、新建一个maven项目(test-springcloud-order7998)

项目结构如下:

2、引入pom依赖,同上(与服务提供者依赖相同)

3、编辑application.yml文件

复制代码
 1 # 端口
 2 server:
 3   port: 7998
 4 
 5 spring:
 6   application:
 7     name: cloud-order
 8   cloud:
 9     consul:
10       host: localhost
11       port: 8500
12       discovery:
13         # hostname: 127.0.0.1
14         service-name: ${spring.application.name}

4、编辑启动类

复制代码
1 @SpringBootApplication
2 public class OrderMain7998 {
3     public static void main(String[] args) {
4         SpringApplication.run(OrderMain7998.class, args);
5     }
6 }

5、编辑配置类,注入RestTemplate对象

复制代码
 1 @Configuration
 2 public class AppConfig {
 3 
 4     /**
 5      * 注入restTemplate,请用请求rest接口
 6      * @return
 7      */
 8     @Bean
 9     // 标注此注解后,RestTemplate就具有了客户端负载均衡能力
10     // 负载均衡技术依赖于的是Ribbon组件~
11     // RestTemplate都塞入一个loadBalancerInterceptor 让其具备有负载均衡的能力
12     @LoadBalanced
13     public RestTemplate restTemplate(){
14         return new RestTemplate();
15     }
16 }

6、编辑Controller

复制代码
 1 @RestController
 2 @Slf4j
 3 public class OrderController {
 4 
 5     public static final String PAYMENT_URL = "http://cloud-payment-service";
 6 
 7     @Autowired
 8     private RestTemplate restTemplate;
 9 
10     @GetMapping("/consumer/payment/consul")
11     public String paymentconsul(){
12         return restTemplate.getForObject(PAYMENT_URL + "/payment/consul", String.class);
13     }
14 
15 }

7、启动项目,测试

1)启动项目(test-springcloud-order7998)

2)使用地址:http://localhost:7998/consumer/payment/consul,进行访问

3)打开Consul的界面,地址:http://localhost:8500/

节点信息:

相关推荐
zyf1044161 分钟前
暑期实践日志 Day48:复盘大纲与知识点对照表,系统汇总整理成果
学习·计算机网络·剪辑·暑期实践·课题任务
夏天拐跑了西瓜39 分钟前
Spring Cloud 微服务实战(八):Hystrix熔断降级——从服务雪崩到断路器模式
spring cloud·hystrix·微服务
m4Rk_1 小时前
【论文阅读】Agent 记忆机制(61):CFGM——用粗到细的记忆落地贯通经验采集、知识蒸馏与在线纠错
论文阅读·人工智能·学习·开源·github
边境悍匪1 小时前
蜗牛学苑 Java 智能体学习 Day33|AOP 面向切面编程、日志技术思维导图复盘
学习
我是你的开心果7781 小时前
ai全栈软件开发day21(第四阶段喽)
人工智能·学习
网络工程小王2 小时前
【LLM开发实验】FastAPI 学习笔记
数据库·笔记·学习·mysql·fastapi
醉舞经阁半卷书12 小时前
量化交易基础之python库学习
学习
youm20033 小时前
【学习笔记】认识NoSQL——非关系型数据库
笔记·学习·nosql
传奇开心果编程3 小时前
【Rust入门知识点学与练】第11课:HashMap 键值对集合
开发语言·学习·rust
约克33 小时前
商水约克中央空调商用工程的行业洞察与落地要点
学习