Springcloud Service Registration and Discovery (ii)

Source: Internet
Author: User

Spring Cloud Eureka is the Service governance module under the spring Cloud Netflix project. The spring cloud Netflix project is one of Spring cloud's sub-projects, primarily packaged with Netflix's range of open source products, which provides a self-configuring Netflix OSS integration for spring boot applications. With some simple annotations, developers can quickly configure common modules in the application and build large, distributed systems. It mainly provides modules including: Service Discovery (Eureka), Circuit breakers (Hystrix), intelligent routing (Zuul), Client load Balancing (Ribbon), etc.

Service Registration Center: Eureka-server

Create a new Springboot project: Eureka-server, whose pom.xml is configured as follows

<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <projec T.reporting.outputencoding>utf-8</project.reporting.outputencoding> <java.version>1.8</ java.version> </properties> <dependencies> <dependency> <groupid>org.sp        Ringframework.cloud</groupid> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> &L T;dependency> <groupId>org.springframework.cloud</groupId> <artifactId> Spring-cloud-dependencies</artifactid> <version>Dalston.SR1</version> < type>pom</type> <scope>import</scope> </dependency> </depen Dencies> </dependencymanaGement> 

The ability to implement a service registry is simple, just use @enableeurekaserver annotations on the project's startup class Eurekaserverapplication

@EnableEurekaServerbr/> @SpringBootApplication
< p="">

public static void main(String[] args) {    new SpringApplicationBuilder(EurekaServerApplication.class)                .web(true).run(args);}

}
By default, the service registry will also try to register itself as a client, so we need to disable its client registration behavior by adding the following information in the Application.properties configuration file:

spring.application.name=eureka-serverserver.port=1001eureka.instance.hostname=localhosteureka.client.register-with-eureka=falseeureka.client.fetch-registry=false

Start Eurekaserverapplication and visit http://localhost:9001/to see the Eureka page.

Service Providers: Eureka-client

Each instance is registered to send a heartbeat to the registry, and when the client registers with the server, it provides some metadata, such as the host and port, the URL, the home page, and so on. Eureka server receives heartbeat messages from each client instance. If the heartbeat times out, the instance is typically removed from the registration server.

Create a new Springboot project: Eureka-client, whose pom.xml configuration is as follows:

<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <projec T.reporting.outputencoding>utf-8</project.reporting.outputencoding> <java.version>1.8</ java.version> </properties> <dependencies> <dependency> <groupid>org.sp Ringframework.cloud</groupid> <artifactId>spring-cloud-starter-eureka</artifactId> &lt ;/dependency> <dependency> <groupId>org.springframework.boot</groupId> & lt;artifactid>spring-boot-starter-web</artifactid> </dependency> </dependencies> &LT;DEP endencymanagement> <dependencies> <dependency> &LT;GROUPID&GT;ORG.SPRINGFR                Amework.cloud</groupid> <artifactId>spring-cloud-dependencies</artifactId> <version>dalston.sr1</version> <type>pom</type> <scope>import</scope> &LT;/DEP Endency> </dependencies> </dependencyManagement>

It is also easy to implement a service provider, as long as you use @enableeurekaclient annotations on the project's startup class Eurekaclientapplication

@EnableEurekaClient@SpringBootApplicationpublic class EurekaClientApplication {     public static void main(String[] args) {            new SpringApplicationBuilder(                    EurekaClientApplication.class)                .web(true).run(args);        }}

The following configuration is available in Application.properties friends Penguin 2042849237

Spring.application.name=eureka-client
server.port=9002
eureka.client.serviceurl.defaultzone=http://localhost:9001/eureka/
Start the Eurekaclientapplication class

To refresh the http://localhost:9001/, we can see that our service provider has already registered with the Service Registration center.

Create a new Discoverycontroller use Discoveryclient.getservices () to get the registered service name and use @value to assign the information in the configuration file to the IP

@RestControllerpublic class DiscoveryController {    @Autowired    private DiscoveryClient discoveryClient;    @Value("${server.port}")    private String ip;    @GetMapping("/client")    public String client() {        String services = "Services: " + discoveryClient.getServices()+" ip :"+ip;        System.out.println(services);        return services;    }}

Visit: http://localhost:9002/client

Finally, explain the two annotations of @enableeurekaclient and @enablediscoveryclient

First of all, this two annotations can realize the function of service discovery, there are many implementations of discovery service in spring Cloud (Eureka, consul, zookeeper, etc.)

@EnableEurekaClient based on Spring-cloud-netflix. The service uses Eureka as the registration center, the use scene is relatively single.

@EnableDiscoveryClient based on Spring-cloud-commons. The service uses other registries.

Springcloud Service Registration and Discovery (ii)

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.