Springcloud整合Consul

x33g5p2x  于2021-10-16 转载在 Spring  
字(6.8k)|赞(0)|评价(0)|浏览(417)

Consul是什么

Consul是一套开源的分布式服务发现和配置管理系统,由HashiCorp 公司用Go语言开发.

提供了微服务系统中的服务治理、配置中心、控制总线等功能。这些功能中的每一个都可以根据需要单独使用,也可以一起使用以构建全方位的服务网格,总之Consul提供了一种完整的服务网格解决方案。

它具有很多优点。包括:基于raft协议,比较简洁;支持健康检查,同时支持HTTP和DNS协议支持跨数据中心的WAN集群提供图形界面跨平台,支持Linux、Mac、Windows。

Consul能干嘛

  • 服务发现 - 提供HTTP和DNS两种发现方式
  • 健康监测 - 支持多种方式,HTTP、TCP、Docker、Shell脚本定制化
  • KV存储 - Key、Value的存储方式
  • 多数据中心 - Consul支持多数据中心
  • 可视化Web界面

Consul操作指南

操作指南

Consul官网下载

Consul官网下载

Consul运行

windows版解压缩后,得consul.exe,打开cmd

  • 查看版本consul -v:

在当前Consul解压目录打开cmd窗口

执行命令

  • 开发模式启动consul agent -dev:

就在刚才打开的cmd界面输入即可

浏览器输入 - http://localhost:8500/ - 打开Consul控制页。

服务提供者注册进Consul

1.新建Module支付服务provider8006

2.POM

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <parent>
  4. <artifactId>cloud_Parent</artifactId>
  5. <groupId>dhy.xpy</groupId>
  6. <version>520.521.finally</version>
  7. </parent>
  8. <modelVersion>4.0.0</modelVersion>
  9. <artifactId>provider8006</artifactId>
  10. <properties>
  11. <maven.compiler.source>8</maven.compiler.source>
  12. <maven.compiler.target>8</maven.compiler.target>
  13. </properties>
  14. <dependencies>
  15. <!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
  16. <dependency>
  17. <groupId>dhy.xpy</groupId>
  18. <artifactId>cloud-api-commons</artifactId>
  19. <version>520.521.finally</version>
  20. </dependency>
  21. <!--SpringCloud consul-server -->
  22. <dependency>
  23. <groupId>org.springframework.cloud</groupId>
  24. <artifactId>spring-cloud-starter-consul-discovery</artifactId>
  25. </dependency>
  26. <!-- SpringBoot整合Web组件 -->
  27. <dependency>
  28. <groupId>org.springframework.boot</groupId>
  29. <artifactId>spring-boot-starter-web</artifactId>
  30. </dependency>
  31. <dependency>
  32. <groupId>org.springframework.boot</groupId>
  33. <artifactId>spring-boot-starter-actuator</artifactId>
  34. </dependency>
  35. <!--日常通用jar包配置-->
  36. <dependency>
  37. <groupId>org.springframework.boot</groupId>
  38. <artifactId>spring-boot-devtools</artifactId>
  39. <scope>runtime</scope>
  40. <optional>true</optional>
  41. </dependency>
  42. <dependency>
  43. <groupId>org.projectlombok</groupId>
  44. <artifactId>lombok</artifactId>
  45. <optional>true</optional>
  46. </dependency>
  47. <dependency>
  48. <groupId>org.springframework.boot</groupId>
  49. <artifactId>spring-boot-starter-test</artifactId>
  50. <scope>test</scope>
  51. </dependency>
  52. <dependency>
  53. <groupId>cn.hutool</groupId>
  54. <artifactId>hutool-all</artifactId>
  55. <version>RELEASE</version>
  56. <scope>test</scope>
  57. </dependency>
  58. </dependencies>
  59. </project>

3.YML

  1. ###consul服务端口号
  2. server:
  3. port: 8006
  4. spring:
  5. application:
  6. name: consul-provider-payment
  7. ####consul注册中心地址
  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. @EnableDiscoveryClient
  3. public class PaymentMain8006
  4. {
  5. public static void main(String[] args) {
  6. SpringApplication.run(PaymentMain8006.class, args);
  7. }
  8. }

5.业务类Controller

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

6.验证测试

启动项目,访问对应controller写的请求,加上端口号

http://localhost:8006/payment/consul

http://localhost:8500 - 会显示provider8006

服务消费者注册进Consul

1.新建Module消费服务order80 - cloud-consumerconsul-order80

2.POM

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <parent>
  4. <artifactId>cloud_Parent</artifactId>
  5. <groupId>dhy.xpy</groupId>
  6. <version>520.521.finally</version>
  7. </parent>
  8. <modelVersion>4.0.0</modelVersion>
  9. <artifactId>consumerconsul-order80</artifactId>
  10. <properties>
  11. <maven.compiler.source>8</maven.compiler.source>
  12. <maven.compiler.target>8</maven.compiler.target>
  13. </properties>
  14. <dependencies>
  15. <!--SpringCloud consul-server -->
  16. <dependency>
  17. <groupId>org.springframework.cloud</groupId>
  18. <artifactId>spring-cloud-starter-consul-discovery</artifactId>
  19. </dependency>
  20. <!-- SpringBoot整合Web组件 -->
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-web</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-actuator</artifactId>
  28. </dependency>
  29. <!--日常通用jar包配置-->
  30. <dependency>
  31. <groupId>org.springframework.boot</groupId>
  32. <artifactId>spring-boot-devtools</artifactId>
  33. <scope>runtime</scope>
  34. <optional>true</optional>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.projectlombok</groupId>
  38. <artifactId>lombok</artifactId>
  39. <optional>true</optional>
  40. </dependency>
  41. <dependency>
  42. <groupId>org.springframework.boot</groupId>
  43. <artifactId>spring-boot-starter-test</artifactId>
  44. <scope>test</scope>
  45. </dependency>
  46. </dependencies>
  47. </project>

3.YML

  1. ###consul服务端口号
  2. server:
  3. port: 80
  4. spring:
  5. application:
  6. name: cloud-consumer-order
  7. ####consul注册中心地址
  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. @EnableDiscoveryClient //该注解用于向使用consul或者zookeeper作为注册中心时注册服务
  3. public class OrderConsulMain80
  4. {
  5. public static void main(String[] args) {
  6. SpringApplication.run(OrderConsulMain80.class, args);
  7. }
  8. }

5.配置Bean

  1. @Configuration
  2. public class ApplicationContextConfig
  3. {
  4. @Bean
  5. @LoadBalanced
  6. public RestTemplate getRestTemplate()
  7. {
  8. return new RestTemplate();
  9. }
  10. }

6.Controller

  1. @RestController
  2. @Slf4j
  3. public class OrderConsulController
  4. {
  5. public static final String INVOKE_URL = "http://consul-provider-payment";
  6. @Resource
  7. private RestTemplate restTemplate;
  8. @GetMapping(value = "/consumer/payment/consul")
  9. public String paymentInfo()
  10. {
  11. String result = restTemplate.getForObject(INVOKE_URL+"/payment/consul",String.class);
  12. return result;
  13. }
  14. }

7.验证测试

运行consul,cloud-providerconsul-payment8006,cloud-consumerconsul-order80

http://localhost:8500/ 主页会显示出consul,cloud-providerconsul-payment8006,cloud-consumerconsul-order80三服务。

8.访问测试地址 - http://localhost/consumer/payment/consul

相关文章