ServiceComb Pack 0.4.0 Integrated Discovery Service : Consul

1 minute read

ServiceComb Pack supports the registration of Alpha service instances to Consul from version 0.4.0, and Omega-side programs can visit Alpha with Consul

Run Consul

docker run -d -p 8500:8500 consul
  • Consul UI

Visit http://0.0.0.0:8500 in the browser, you can see the following page indicates that Consul started successfully

consul_main

Run Alpha

Enable registration to Consul with the parameter spring.cloud.consul.enabled = true

java -jar alpha-server-0.4.0-exec.jar \
  --server.port=8090 \
  --alpha.server.port=8080 \
  --spring.datasource.url="jdbc:postgresql://127.0.0.1:5432/saga?useSSL=false" \
  --spring.datasource.username=saga-user \
  --spring.datasource.password=saga-password \
  --spring.cloud.consul.enabled=true \
  --spring.cloud.consul.host=0.0.0.0 \
  --spring.cloud.consul.port=8500 \
  --spring.profiles.active=prd 

Note: Check out for Consul more details Spring Cloud Consul 2.x Spring Cloud Consul 1.x

After Alpha is launched, you can see that you have registered to Consul

alpha_register_to_consul

Use curl http://0.0.0.0:8500/v1/agent/services You can see that Alpha’s gRPC address and port have been registered in Consul’s Tags

{
    "servicecomb-alpha-server-0-0-0-0-336b06581fb5b92ed91c7ade3fdafa88": {
        "ID": "servicecomb-alpha-server-0-0-0-0-336b06581fb5b92ed91c7ade3fdafa88",
        "Service": "servicecomb-alpha-server",
        "Tags": [
            "alpha-server-host=0.0.0.0",
            "alpha-server-port=8080",
            "secure=false"
        ],
        "Meta": {},
        "Port": 8090,
        "Address": "192.168.1.116",
        "Weights": {
            "Passing": 1,
            "Warning": 1
        },
        "EnableTagOverride": false
    }
}

Configuring the Omega side

ServiceComb Pack 0.4.0 Omega defaults use Spring Boot 2.x Spring and Cloud Consul 2.x, If your Omega side project base on Spring Boot 1.x you can use -Pspring-boot-1 to rebuild ServiceComb Pack to Spring Boot 1.x and Spring Cloud Consul 1.x

Modify the original omega project

add dependencies

<dependency>
	<groupId>org.apache.servicecomb.pack</groupId>
	<artifactId>omega-spring-cloud-consul-starter</artifactId>
	<version>${pack.version}</version>
</dependency>

Add the following to application.yaml

spring:
  cloud:
    consul:
      discovery:
      	register: false
      host: 0.0.0.0
      port: 8500
      
alpha:
  cluster:
    register:
      type: consul
  • spring.cloud.consul.host property is set to the Consul server’s instance address, spring.cloud.consul.port property is set to the Consul server’s instance port, spring.cloud.consul.discovery.register=false property is not register yourself , check out Spring Boot’s  Spring Cloud Consul 2.x or Spring Cloud Consul 1.x for more details.

  • alpha.cluster.register.type=consul property is omega gets Alpha gRPC address from Consul

  • spring boot version compatible

If your project is not using spring boot 2.1.1, please refer to this list to add a compatible spring-cloud-starter-consul-discovery version

spring boot spring-cloud-starter-consul-discovery
2.1.x.RELEASE 2.1.1.RELEASE
2.0.x.RELEASE 2.0.2.RELEASE
   <dependencyManagement>
     <dependencies>
       <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-consul-discovery</artifactId>
         <version>2.0.2.RELEASE</version>
       </dependency>
     </dependencies>
   </dependencyManagement>

Note: If you define spring.application.name parameter when start Alpha, You need to specify this service name in Omega via the parameter alpha.cluster.serviceId

Leave a Comment

Your email address will not be published. Required fields are marked *

Loading...