Registering and discovering services with spring Cloud consul

Source: Internet
Author: User
Tags bind tomcat

First install the consul environment, referring to the previous article: http://blog.csdn.net/mn960mn/article/details/51753893

Project planning, 2 service-side, 1 clients


First look at the service side,

One: server 1:

Project Dependencies

<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>
	</dependencies>
</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 for the project to have access to 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 () {
		System.out.println ("1111111111111");
		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

Read my previous article, should be very clear about these configurations. In this way, a service side is configured to write well.

Since we added @enablediscoveryclient annotations, when the system starts, it will register a service with Consul, with the name Tomcat and ID TOMCAT1

The HTTP api/v1/catalog/service/tomcat output for accessing consul is 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: Service Side 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 () {
		System.out.println ("2222222222222222");
		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 dependent, only need 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, or the system will be started by registering a service with consul

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 Loadbala
	
	Ncerclient LoadBalancer;
	
	@Autowired private discoveryclient discoveryclient; /** * Select a service from all services (polling) */@RequestMapping ("/discover") public Object Discover () {return loadbalancer.choose ("Tomc
	At "). 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);
 }
}

Once started, you can access the/discover,/services to see the effect.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.