• Consul 服务治理(注册中心)


    简介: 

    • Consul是由HashiCorp基于Go语言开发的,支持多数据中心,分布式高可用的服务发布和注册服务软件。
    • 用于实现分布式系统的服务发现与配置
    • 使用起来也比较简单。具有天然的可移植性(支持Linux、windows和Mac Os X);安装包仅包含一个可执行文件方便部署。
    • 官网地址:https://www.consul.io

    搭建conful 

     

     然后解压,打开后一个exe文件,按住shift按右键,打开powerShell界面,输入下面命令启动。

    ./consul agent -dev

    浏览器访问控制台 localhost:8500

     搭建consumer和producer

     consumer代码:

    1. @Configuration
    2. public class RestTemplateConfig {
    3. @Bean
    4. public RestTemplate restTemplate(){
    5. return new RestTemplate();
    6. }
    7. }
    8. @RestController
    9. @RequestMapping("/order")
    10. public class OrderController {
    11. @Autowired
    12. private RestTemplate restTemplate;
    13. @Autowired
    14. private DiscoveryClient discoveryClient;
    15. @GetMapping("/goods/{id}")
    16. public Goods findGoodsById(@PathVariable("id") int id){
    17. System.out.println("------------findGoodsById--------"+id);
    18. //远程调用Goods服务中的findOne接口
    19. /*
    20. 使用RestTemplate
    21. 1.定义Bean restTemplate
    22. 2.注入Bean
    23. 3.调用方法
    24. */
    25. /*
    26. 动态从Eureka Server中获取 provider的ip端口
    27. 1.注入 DiscoveryClient对象 激活
    28. 2.调用方法
    29. */
    30. //演示discoveryClient使用
    31. /**/
    32. List instances =discoveryClient.getInstances("consul-PROVIDER");
    33. if (instances == null || instances.size() == 0){
    34. //集合没有数据
    35. System.out.println("没有获取到端口和IP");
    36. return null;
    37. }
    38. ServiceInstance instance = instances.get(0);
    39. String host =instance.getHost();//获取IP
    40. int port = instance.getPort();//获取端口
    41. System.out.println("----------host"+host);
    42. System.out.println("----------port"+port);
    43. //String url = "http://localhost:8000/goods/findOne/"+id;
    44. String url = "http://"+host+":"+port+"/goods/findOne/"+id;
    45. //3.调用方法
    46. Goods goods = restTemplate.getForObject(url,Goods.class);
    47. return goods;
    48. }
    49. }
    50. public class Goods {
    51. private int id;
    52. private String title;
    53. private double price;
    54. private int count;
    55. public Goods() {
    56. }
    57. public Goods(int id, String title, double price, int count) {
    58. this.id = id;
    59. this.title = title;
    60. this.price = price;
    61. this.count = count;
    62. }
    63. public int getId() {
    64. return id;
    65. }
    66. public void setId(int id) {
    67. this.id = id;
    68. }
    69. public String getTitle() {
    70. return title;
    71. }
    72. public void setTitle(String title) {
    73. this.title = title;
    74. }
    75. public double getPrice() {
    76. return price;
    77. }
    78. public void setPrice(double price) {
    79. this.price = price;
    80. }
    81. public int getCount() {
    82. return count;
    83. }
    84. public void setCount(int count) {
    85. this.count = count;
    86. }
    87. @Override
    88. public String toString() {
    89. return "Goods{" +
    90. "id=" + id +
    91. ", title='" + title + '\'' +
    92. ", price=" + price +
    93. ", count=" + count +
    94. '}';
    95. }
    96. }
    97. @EnableDiscoveryClient //激活DiscoveryClient 可无
    98. //启动类
    99. @EnableEurekaClient //可无
    100. @SpringBootApplication
    101. public class ConsumerApp {
    102. public static void main(String[] args){
    103. SpringApplication.run(ConsumerApp.class,args);
    104. }
    105. }

     consumer和provider依赖:

    1. <dependencies>
    2. <dependency>
    3. <groupId>org.springframework.cloudgroupId>
    4. <artifactId>spring-cloud-starter-consul-discoveryartifactId>
    5. dependency>
    6. <dependency>
    7. <groupId>org.springframework.bootgroupId>
    8. <artifactId>spring-boot-starter-webartifactId>
    9. dependency>
    10. <dependency>
    11. <groupId>org.springframework.bootgroupId>
    12. <artifactId>spring-boot-starter-testartifactId>
    13. <scope>testscope>
    14. dependency>
    15. <dependency>
    16. <groupId>org.springframework.bootgroupId>
    17. <artifactId>spring-boot-starter-actuatorartifactId>
    18. dependency>
    19. dependencies>

     consumer配置: 

    1. server:
    2. port: 9000
    3. spring:
    4. cloud:
    5. consul:
    6. host: localhost
    7. port: 8500
    8. discovery:
    9. service-name: ${spring.application.name} #当前应用注册到consul的名称
    10. prefer-ip-address: true #注册ip
    11. application:
    12. name: consul-consumer

    provider配置:

    1. server:
    2. port: 8000
    3. spring:
    4. cloud:
    5. consul:
    6. host: localhost
    7. port: 8500
    8. discovery:
    9. service-name: ${spring.application.name} #当前应用注册到consul的名称
    10. prefer-ip-address: true #注册ip
    11. application:
    12. name: consul-provider

     provider代码:

    1. //服务提供方
    2. @RestController
    3. @RequestMapping("/goods")
    4. public class GoodsController {
    5. @Autowired
    6. private GoodsService goodsService;
    7. @GetMapping("/findOne/{id}")
    8. public Goods findOne(@PathVariable("id") int id){
    9. Goods goods = goodsService.findOne(id);
    10. return goods;
    11. }
    12. }
    13. @Repository
    14. public class GoodsDao {
    15. public Goods findOne(int id){
    16. return new Goods(1,"华为手机",3999,10000);
    17. }
    18. }
    19. public class Goods {
    20. private int id;
    21. private String title;
    22. private double price;
    23. private int count;
    24. public Goods() {
    25. }
    26. public Goods(int id, String title, double price, int count) {
    27. this.id = id;
    28. this.title = title;
    29. this.price = price;
    30. this.count = count;
    31. }
    32. public int getId() {
    33. return id;
    34. }
    35. public void setId(int id) {
    36. this.id = id;
    37. }
    38. public String getTitle() {
    39. return title;
    40. }
    41. public void setTitle(String title) {
    42. this.title = title;
    43. }
    44. public double getPrice() {
    45. return price;
    46. }
    47. public void setPrice(double price) {
    48. this.price = price;
    49. }
    50. public int getCount() {
    51. return count;
    52. }
    53. public void setCount(int count) {
    54. this.count = count;
    55. }
    56. @Override
    57. public String toString() {
    58. return "Goods{" +
    59. "id=" + id +
    60. ", title='" + title + '\'' +
    61. ", price=" + price +
    62. ", count=" + count +
    63. '}';
    64. }
    65. }
    66. @Service
    67. public class GoodsService {
    68. @Autowired
    69. private GoodsDao goodsDao;
    70. public Goods findOne(int id){
    71. return goodsDao.findOne(id);
    72. }
    73. }
    74. //启动类
    75. @EnableEurekaClient
    76. @SpringBootApplication
    77. public class ProviderApp {
    78. public static void main(String[] args){
    79. SpringApplication.run(ProviderApp.class,args);
    80. }
    81. }

    运行consumer和provider两个服务互通

     在consul上可以查到两个服务

  • 相关阅读:
    日常开发小汇总(5)数组克隆、伪数组转换为真数组、随机排序
    使用 Docker 部署 Next Terminal 轻量级堡垒机
    js基础笔记学习317练习3
    flask框架安装使用
    DES 加密算法的改进方案
    计算机网络--- 电子邮件
    vue3.0项目实战系列文章 - 登录页面
    兽医诊所温湿度失衡,该如何止损?
    Codeforces Round #790 (Div. 4) E. Eating Queries
    Spring MVC 中文文档
  • 原文地址:https://blog.csdn.net/qq_51497041/article/details/126190187