你好,这里是codetrend专栏“SpringCloud2023实战”。欢迎点击关注查看往期文章。
注册中心在前文提到有很多选型,在这里以Spring Cloud Zookeeper为例说明注册中心的集成和使用。
选择Spring Cloud Zookeeper作为注册中心原因如下:
io.rainforest
banana
1.0
4.0.0
banana-client1
spring cloud banana-client1
jar
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-undertow
org.springframework.cloud
spring-cloud-starter-zookeeper-discovery
org.springframework.cloud
spring-cloud-starter-openfeign
org.springframework.cloud
spring-cloud-openfeign-core
true
io.rainforest
banana-common-core
cn.hutool
hutool-crypto
cn.hutool
hutool-http
com.github.xiaoymin
knife4j-openapi3-jakarta-spring-boot-starter
org.springframework.boot
spring-boot-maven-plugin
JAVA_HOME 环境变量。|-- LICENSE.txt
|-- NOTICE.txt
|-- README.md
|-- README_packaging.md
|-- bin
|-- conf
|-- data
|-- docs
|-- lib
conf/zoo.cfg。# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=E:/DevTool/apache-zookeeper-3.8.2-bin/data
# the port at which the clients will connect
clientPort=2181
# windows系统
binzkServer.cmd
# Linux系统
binzkServer.sh
package io.rainforest.banana.client1;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
spring.application.name: client1
spring:
cloud:
zookeeper:
connect-string: localhost:2181 ## 注册中心的配置
server:
port: 10101
servlet:
context-path: /client1
get /services/client1/b257e7f5-a451-4119-ba20-e7622f3aeaba
{"name":"client1","id":"b257e7f5-a451-4119-ba20-e7622f3aeaba","address":"cat","port":10101,"sslPort":null,"payload":{"@class":"org.springframework.cloud.zookeeper.discovery.ZookeeperInstance","id":"client1","name":"client1","metadata":{"instance_status":"UP"}},"registrationTimeUTC":1698715221404,"serviceType":"DYNAMIC","uriSpec":{"parts":[{"value":"scheme","variable":true},{"value":"://","variable":false},{"value":"address","variable":true},{"value":":","variable":false},{"value":"port","variable":true}]}}
默认的zookeeper注册中心的数据放在 /services/客户端名称/实例名称 。
和“SpringCloud实战”对应的源码信息如下:
来自一线全栈程序员nine的八年探索与实践,持续迭代中。欢迎关注公众号“雨林寻北”或添加个人卫星codetrend(备注技术)。
登录查看全部
参与评论
手机查看
返回顶部