k8s consul 服务发现_SpringCloud 初始化父子项目并集成 Consul 服务发现

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

准备工作

  • IDEA2020.1
  • JDK1.8
  • Spring Cloud 版本:Hoxton.SR5
  • Spring Boot 版本:2.3.0.RELEASE

安装 Consul

  • windows 安装:https://www.yuque.com/ekko/app/go38vo
  • Mac 安装:https://www.yuque.com/ekko/app/nzw3gi

开始

简介:consul 的功能

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

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

e975a881e9ff50d8751b78fd37a836a3.png


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

aliyunaliyunhttps://maven.aliyun.com/repository/publictruefalsespring-milestonesSpring Milestoneshttps://maven.aliyun.com/repository/springtruefalse

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

新建子项目

右击项目名称,New -> Module

d4aab7c00532ae36e7c43b892df768f3.png


包名自定义,不一定非要相同,这里的包名或许后面也要改掉

8d15fe3c63eb30b3520f0a7b93a3cb61.png


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

关联父子项目

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

pro-service

如图示

bb2dd7af65a9eafd4dbc758c777cec3a.png


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

com.github.springtools        SpringCloudPro        0.0.1

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

26da87b7f67531f49ab50e31618cf361.png


最终的父项目pom.xml

<?xml version="1.0" encoding="UTF-8"?>4.0.0com.github.springtools    SpringCloudPro    0.0.1SpringCloudPro父项目pom1.8Hoxton.SR52.3.0.RELEASEpro-serviceorg.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            testorg.junit.vintage                    junit-vintage-engine                org.springframework.cloud                spring-cloud-dependencies                ${spring-cloud.version}pomimportorg.springframework.boot                spring-boot-dependencies                ${spring-boot.version}pomimportorg.springframework.boot                spring-boot-maven-plugin            aliyunaliyunhttps://maven.aliyun.com/repository/publictruefalsespring-milestonesSpring Milestoneshttps://maven.aliyun.com/repository/springtruefalse

子项目pom.xml

<?xml version="1.0" encoding="UTF-8"?>4.0.0com.github.springtools        SpringCloudPro        0.0.1    pro-service    0.0.1pro-service业务模块1.8org.springframework.boot            spring-boot-starter-web        org.springframework.boot                spring-boot-maven-plugin            

项目结构图

f90bdac6258273a0e13af3d243fff821.png

集成服务发现 Consul

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

82b2a7589543edb4e1e2b770bdf0dcb6.png


在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@SpringBootApplicationpublic class ProServiceApplication {    @GetMapping("/")    public String hello() {        return "Hello world";    }    public static void main(String[] args) {        SpringApplication.run(ProServiceApplication.class, args);    }}

启动

bc24705e09bdbebd58e3733a5f2e9542.png


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

50c1e1a44eaa9bd83a22ccfddc0842b4.png


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

源代码地址

https://github.com/Gleans/SpringCloudPro

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

全文完

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值