1 Star 0 Fork 0

大胃王 / SpringCloud自定义服务发现

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

快速开始

文档地址

导入初始化表结构

执行 client-implement/client-mysql/sql/schema.sql 数据库脚本导入初始化表结构。

加入Maven依赖

Maven central

分别在consumer和producer服务中加入依赖

<dependency>
    <groupId>cn.codeforfun</groupId>
    <artifactId>discovery-client-mysql</artifactId>
    <version>{version}</version>
</dependency>

编写配置文件

consumer配置

application.yml

server:
  port: 50021
spring:
  application:
    name: client-consumer
  datasource:
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/codeforfun-discovery

producer配置

application.yml

server:
  port: 50011
spring:
  application:
    name: client-producer
  datasource:
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/codeforfun-discovery

编写服务调用代码

consumer允许FeignClient:

@EnableFeignClients(basePackages = "com.example.feign")

consumer FeignClient:

com.example.feign.TestClient.java

@FeignClient("client-producer")
public interface TestClient {
    @GetMapping("/test1")
    String test1();
}

consumer接口

com.example.test.TestController.java

@RestController
public class TestController {
    @Resource
    TestClient testClient;

    @GetMapping("/test")
    public String test() {
        return testClient.test1();
    }
}

producer接口

TestController.java

@RestController
public class TestController {
    @GetMapping("/test1")
    public String test1() {
        return "producer client test1";
    }
}

启动服务

分别启动producer和consumer服务,等待程序启动。

访问 http://localhost:50011/test1 后显示 producer client test1 则表示成功。

访问 http://localhost:50021/test 后同样显示 producer client test1 则表示成功。

示例代码

示例代码在 example 目录下,example-client-producer为被调用方示例代码,example-client-consumer为调用方示例代码。

目前支持的数据存储

  • MySQL
  • ElasticSearch

配置项

key 说明 默认值
discovery.service.service-active-interval 服务激活间隔,单位:秒。 每隔多长时间激活一次自身服务 10
discovery.service.refresh-service-list-interval 刷新服务列表间隔,单位:秒。 每隔多长时间刷新一次服务列表 20
discovery.service.service-active-timeout 服务激活超时时间,单位:秒。 超过这个时间后该服务就会被认为是有故障的,之后就不会调用这个服务 60
discovery.service.name 服务名,如果没设置的话会取(spring.application.name)的值,如果都没有则报错
discovery.service.port 服务端口,如果没设置的话会取(server.port)的值,如果还是没有则默认8080 8080
discovery.service.host 服务地址,如果没设置会取本地ip 本地ip

MySQL配置项

由于MySQL是使用 mysql-connector-javaspring-boot-starter-jdbc 实现的,所以配置项也是直接继承这两个。没用过的话,正常配置数据源就行了。

ElasticSearch配置项

由于ElasticSearch是使用 spring-boot-starter-data-elasticsearch 实现的,所以配置项也是直接继承下来的。

源码地址

GitEE仓库Github仓库。 以GitEE为主.

MIT License Copyright (c) 2021 大胃王 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

微服务体系中基于数据库的服务发现,可以灵活的拓展为自己需要的数据存储方式 展开 收起
Java 等 3 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/consolelog/codeforfun-discovery.git
git@gitee.com:consolelog/codeforfun-discovery.git
consolelog
codeforfun-discovery
SpringCloud自定义服务发现
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891