用户工具

站点工具


light4j:registry

Registry and Discovery

java -jar light-codegen/codegen-cli/target/codegen-cli.jar -f openapi -o light-example-4j/discovery/api_a/generated -m model-config/rest/openapi/aa/1.0.0/openapi.yaml -c model-config/rest/openapi/aa/1.0.0/config.json
java -jar light-codegen/codegen-cli/target/codegen-cli.jar -f openapi -o light-example-4j/discovery/api_b/generated -m model-config/rest/openapi/ab/1.0.0/openapi.yaml -c model-config/rest/openapi/ab/1.0.0/config.json
java -jar light-codegen/codegen-cli/target/codegen-cli.jar -f openapi -o light-example-4j/discovery/api_c/generated -m model-config/rest/openapi/ac/1.0.0/openapi.yaml -c model-config/rest/openapi/ac/1.0.0/config.json
java -jar light-codegen/codegen-cli/target/codegen-cli.jar -f openapi -o light-example-4j/discovery/api_d/generated -m model-config/rest/openapi/ad/1.0.0/openapi.yaml -c model-config/rest/openapi/ad/1.0.0/config.json
mvn clean install exec:exec
curl -k https://localhost:7441/v1/data #4个应用监听7441-7444端口
  • 直连配置:static、dynamic,multiple
    1. api_a:api_a.yml,a ⇒ b+c,/v1/data@get =》DataGetHandler
    2. api_c:c ⇒ d,Http2Client.connect(https://localhost:7444),ClientRequest(/v1/data@get)
    3. Cluster:SingletonServiceFactory.getBean(Cluster.class).serviceToUrl(“https”, serviceId, tag, null)
    4. parameters:serviceId ⇒ host+port,multiple时使用RoundRobin轮询调用
singletons:
  - com.networknt.registry.URL:
      - com.networknt.registry.URLImpl:
          parameters:
            com.networknt.ab-1.0.0: https://localhost:7442
            com.networknt.ac-1.0.0: https://localhost:7443
            #com.networknt.ad-1.0.0: https://localhost:7444,https://localhost:7445 #multiple示例
  - com.networknt.registry.Registry: #直连
      - com.networknt.registry.support.DirectRegistry
  - com.networknt.balance.LoadBalance:  #均衡和集群是类似的
      - com.networknt.balance.RoundRobinLoadBalance
  - com.networknt.cluster.Cluster:
      - com.networknt.cluster.LightCluster
  • 注册中心:consultoken,ServerConfig.enableRegistry=true
- com.networknt.registry.URL:
  - com.networknt.registry.URLImpl:
      protocol: light
      host: localhost
      port: 8080
      path: consul
      parameters:
        registryRetryPeriod: '30000'
- com.networknt.consul.client.ConsulClient:
  - com.networknt.consul.client.ConsulClientImpl
- com.networknt.registry.Registry:
  - com.networknt.consul.ConsulRegistry
#docker run -d -p 8400:8400 -p 8500:8500/tcp -p 8600:53/udp -e 'CONSUL_LOCAL_CONFIG={"acl_datacenter":"dc1","acl_default_policy":"allow","acl_down_policy":"extend-cache","acl_master_token":"the_one_ring","bootstrap_expect":1,"datacenter":"dc1","data_dir":"/usr/local/bin/consul.d/data","server":true}' consul agent -server -ui -bind=127.0.0.1 -client=0.0.0.0
#http://localhost:8500/ui #查看已注册服务,acl_default_policy=allow仅用于测试环境

docker run -d -p 8400:8400 -p 8500:8500/tcp -p 8600:53/udp -e 'CONSUL_LOCAL_CONFIG={"acl_datacenter":"dc1","acl_default_policy":"deny","acl_down_policy":"extend-cache","acl_master_token":"the_one_ring","bootstrap_expect":1,"datacenter":"dc1","data_dir":"/usr/local/bin/consul.d/data","server":true}' consul agent -server -ui -bind=127.0.0.1 -client=0.0.0.0

#create agent token,acl_default_policy=deny生产需要密钥,响应{"ID":"67ccac85-36a3-e912-d0b3-bce1194119a0"}
curl --request PUT --header "X-Consul-Token: the_one_ring" --data \
'{
  "Name": "Agent Token",
  "Type": "client",
  "Rules": "node \"\" { policy = \"write\" } service \"\" { policy = \"read\" }"
}' http://127.0.0.1:8500/v1/acl/create

#introduce token,有读写权限
curl --request PUT --header "X-Consul-Token: the_one_ring" --data \
'{
  "Token": "67ccac85-36a3-e912-d0b3-bce1194119a0"
}' http://127.0.0.1:8500/v1/agent/token/acl_agent_token

#anonymous,只读权限,响应{"ID":"anonymous"}
curl --request PUT --header "X-Consul-Token: the_one_ring" --data \
'{
  "ID": "anonymous",
  "Type": "client",
  "Rules": "node \"\" { policy = \"read\" } service \"\" { policy = \"read\" }"
}' http://127.0.0.1:8500/v1/acl/update

#密钥配置 consulToken: the_one_ring,consul.yml优先于server.yml
  • 容器部署:consuldockerconsul.consulUrl,修改localhost为本机ip
#修改consul.yml
consulUrl: http://192.168.1.144:8500
#docker/Dockerfile
FROM openjdk:11.0.3-slim
ADD /target/aa-1.0.0.jar server.jar
CMD ["/bin/sh","-c","java -Dlight-4j-config-dir=/config -Dlogback.configurationFile=/config/logback.xml -jar /server.jar"]
#构建build
cd ~/networknt/light-example-4j/discovery/api_a/consuldocker
mvn clean install -Prelease
docker build -t networknt/com.networknt.aa-1.0.0 -f docker/Dockerfile 
#git clone https://github.com/networknt/light-docker.git
cd light-docker
docker-compose -f docker-compose-consul.yml up -d
docker-compose -f docker-compose-discovery.yml up -d
#curl -k https://localhost:7441/v1/data
light4j/registry.txt · 最后更改: 2020/07/09 18:16 由 admin