First install the consul environment, refer to the previous article: https://www.VeVB.COM/article/141789.htm
Project planning, 2 servers, 1 client
First look at the server side.
One: Server 1:
Project dependency
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-consul-dependencies</artifactId> <version>1.0.1.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependency> </dependency> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-actuator</artifactId> <version>1.3.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> </dependencies>
Note that adding spring-boot-actuator is so that the project can access the /health path to determine whether the service is healthy.
package com.pp.consul1; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @EnableDiscoveryClient @RestController public class ConsulApp { @RequestMapping("/home") public Object home() { System.out.println("11111111111111"); return "OK11"; } public static void main( String[] args ) { SpringApplication.run(ConsulApp.class, args); } } application.properties configuration content
server.port=9955 spring.application.name=Consul-Server-1 spring.cloud.consul.host=192.168.1.100 spring.cloud.consul.port=8500 spring.cloud.consul.enabled=true spring.cloud.consul.discovery.enabled=true spring.cloud.consul.discovery.instanceId=tomcat1 spring.cloud.consul.discovery.serviceName=tomcat spring.cloud.consul.discovery.hostname=192.168.2.95 spring.cloud.consul.discovery.port=${server.port} spring.cloud.consul.discovery.healthCheckUrl=http://192.168.2.95:9955/health spring.cloud.consul.discovery.healthCheckInterval=10s spring.cloud.consul.discovery.tags=dev I have read my previous article and I should be very clear about these configurations. In this way, a server will be configured and written.
Since we have added the @EnableDiscoveryClient annotation, when the system starts, a service will be registered with the consul. The service name is tomcat and the ID is tomcat1
The HTTP API of the access consul is output as follows:
{ "Node":"192.168.1.100", "Address":"192.168.1.100", "ServiceID":"tomcat1", "ServiceName":"tomcat", "ServiceTags":["dev"], "ServiceAddress":"192.168.2.95", "ServicePort":9955, "ServiceEnableTagOverride":false, "CreateIndex":993, "ModifyIndex":1057 }Two: Server 2
Project dependencies are the same as above
package com.pp.consul2; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @EnableDiscoveryClient @RestController public class ConsulApp { @RequestMapping("/home") public Object home() { System.out.println("222222222222222222222"); return "OK22"; } public static void main( String[] args ) { SpringApplication.run(ConsulApp.class, args); } }application.properties configuration content:
server.port=9966 spring.application.name=Consul-Server-2 spring.cloud.consul.host=192.168.1.100 spring.cloud.consul.port=8500 spring.cloud.consul.enabled=true spring.cloud.consul.discovery.enabled=true spring.cloud.consul.discovery.instanceId=tomcat2 spring.cloud.consul.discovery.serviceName=tomcat spring.cloud.consul.discovery.hostname=192.168.2.95 spring.cloud.consul.discovery.port=${server.port} spring.cloud.consul.discovery.healthCheckUrl=http://192.168.2.95:9966/health spring.cloud.consul.discovery.healthCheckInterval=10s spring.cloud.consul.discovery.tags=test Three: Client
Project dependencies, just spring-cloud-starter-consul-discovery
application.properties configuration content:
server.port=9977 spring.application.name=Consul-Client spring.cloud.consul.host=192.168.1.100 spring.cloud.consul.port=8500 spring.cloud.consul.discovery.register=false
Note that the spring.cloud.consul.discovery.register here needs to be configured to false, otherwise a service will be registered with consul when the system starts.
package com.pp.client; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @EnableDiscoveryClient @RestController public class ConsulClient { @Autowired private LoadBalancerClient loadBalancer; @Autowired private DiscoveryClient discoveryClient; /** * Select a service from all services (polling) */ @RequestMapping("/discover") public Object discover() { return loadBalancer.choose("tomcat").getUri().toString(); } /** * Get all services*/ @RequestMapping("/services") public Object services() { return discoveryClient.getInstances("tomcat"); } public static void main( String[] args ) { SpringApplication.run(ConsulClient.class, args); } } After startup, you can access /discover and /services to view the effect.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.