本章使用的Consul版本是 1.7.2
项目架构图如下:
1、新建一个maven项目(test-springcloud-provider-payment8006)
结构如下:
2、引入依赖,编辑pom文件
1
2
3 org.springframework.cloud
4 spring-cloud-starter-consul-discovery
5
完整pom.xml文件如下:
1
2
5
6 test-springcloud
7 com.test
8 1.0-SNAPSHOT
9
10 4.0.0
11
12 test-springcloud-provider-payment8006
13
14
15
16
17
18 org.springframework.cloud
19 spring-cloud-starter-consul-discovery
20
21
22
23
24 org.springframework.boot
25 spring-boot-starter-web
26
27
28 org.springframework.boot
29 spring-boot-starter-actuator
30
31
32
33 org.springframework.boot
34 spring-boot-devtools
35 runtime
36 true
37
38
39 org.projectlombok
40 lombok
41 true
42
43
44
45 org.springframework.boot
46 spring-boot-starter-test
47 test
48
49
50
51
52
53 test-springcloud-provider-payment8006
54
55
56
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/
节点信息: