栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringCloud 初始化父子项目并集成 Consul 服务发现

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringCloud 初始化父子项目并集成 Consul 服务发现

SpringCloud 初始化父子项目并集成 Consul 服务发现 准备工作

IDEA2020.1JDK1.8Spring Cloud版本:Hoxton.SR5Spring Boot 版本:2.3.0.RELEASE 安装Consul

windows安装:https://www.yuque.com/ekko/app/go38voMac安装:https://www.yuque.com/ekko/app/nzw3gi 开始

简介:consul的功能

服务发现Key/Value存储健康检查

今天我们主要来学习实践服务发现功能
先到 https://start.spring.io/ 初始化一个父项目

生成之后解压,先用IDE编辑文件修改pom.xml,如下图,在底部 标签下切换国内阿里源


        
        
            aliyun
            aliyun
            https://maven.aliyun.com/repository/public
            
                true
            
            
                false
            
        
        
        
            spring-milestones
            Spring Milestones
            https://maven.aliyun.com/repository/spring
            
                true
            
            
                false
            
        
    

保存,然后使用 IDEA 打开,等Maven加载完毕

新建子项目

右击项目名称,New -> Module
Module" />
包名自定义,不一定非要相同,这里的包名或许后面也要改掉

下一步选择包,先跳过,点击Finish 完成

关联父子项目

在父 Module的pom.xml中的标签上部新增

	
		pro-service
	

如图示

在子项目 pro-service 中的 4.0.0 标签后追加

    
        com.github.springtools
        SpringCloudPro
        0.0.1
    

到这里会发现Maven找不到一些Spring Boot的依赖
在父项目增加管理依赖版本号的定义

最终的父项目pom.xml



    4.0.0

    com.github.springtools
    SpringCloudPro
    0.0.1
    SpringCloudPro
    父项目
    pom

    
        1.8
        Hoxton.SR5
        2.3.0.RELEASE
    

    
        pro-service
    

    
        
            org.springframework.cloud
            spring-cloud-starter-consul-config
        
        
            org.springframework.cloud
            spring-cloud-starter-consul-discovery
        
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
            
            
                org.springframework.boot
                spring-boot-dependencies
                ${spring-boot.version}
                pom
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

    
        
        
            aliyun
            aliyun
            https://maven.aliyun.com/repository/public
            
                true
            
            
                false
            
        
        
        
            spring-milestones
            Spring Milestones
            https://maven.aliyun.com/repository/spring
            
                true
            
            
                false
            
        
    

子项目pom.xml



    4.0.0
    
        com.github.springtools
        SpringCloudPro
        0.0.1
    

    pro-service
    0.0.1
    pro-service
    业务模块

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

项目结构图

集成服务发现 Consul

父项目添加如下依赖,在上述的最终父项目pom.xml中已经加过了,这里简要说明


在pro-service中的resources目录下,删除自带的application.properties,新增bootstrap.yml

因为后续的config要用到,bootstrap比application.properties加载优先级别高

完善子项目并注册到 Consul

bootstrap.yml

spring:
  application:
    name: pro-service
  cloud:
    consul:
      discovery:
        health-check-path: /actuator/health  # 检测实例健康
        health-check-interval: 10s   # 每隔10s检查
        hostname: localhost    # 配置实例地址
        register: true  # 自动注册
        service-name: ${spring.application.name} # 实例名称
      host: localhost
      port: 8500

ps:这里的spring.application.name与spring.cloud.consul.discovery.service-name一定要加,不然会抛出异常

Caused by: java.lang.IllegalArgumentException: Consul service ids must not be empty, must start with a letter, end with a letter or digit, and have as interior characters only letters, digits
-- 参考:https://www.jianshu.com/p/83d3a8105620

定义启动类 ProServiceApplication.java

package com.github.springtools.proservice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@EnableDiscoveryClient
@SpringBootApplication
public class ProServiceApplication {

    @GetMapping("/")
    public String hello() {
        return "Hello world";
    }

    public static void main(String[] args) {
        SpringApplication.run(ProServiceApplication.class, args);
    }

}

启动

打开Consul Web 控制台 http://localhost:8500

好了,今天就先写到这里了

源代码地址

https://github.com/Gleans/SpringCloudPro

ps:写着写着又凌晨一点了,头发又少了,一天天儿~的过得也太快了,淦

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/778514.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号