Springcloud Service Registration and Discovery (Eureka) and springcloudeureka

Source: Internet
Author: User

Springcloud Service Registration and Discovery (Eureka) and springcloudeureka

Use Eureka for service governance

Role: Achieve service governance (Service Registration and discovery)

Introduction: Spring Cloud Eureka is a service governance module under the Spring Cloud Netflix project. The Spring Cloud Netflix project is one of the sub-projects of Spring Cloud. The main content is the packaging of a series of open-source products of Netflix. It provides self-configured Netflix OSS integration for Spring Boot applications. With some simple annotations, developers can quickly configure common modules in applications and build a large distributed system. It provides the following modules: Eureka, Hystrix, Zuul, and Ribbon.

Project Practice:

Service Registration Center: eureka-server

Purpose: The Service Registration Center provides the service registration function.

Service provider: eureka-client

Role: register the service to the Service Registration Center

Service Registration Center: eureka-server

Create a springboot project: eureka-server. Its pom. xml configuration is as follows:

 <properties>   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>   <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>   <java.version>1.8</java.version>  </properties>  <dependencies>   <dependency>    <groupId>org.springframework.cloud</groupId>    <artifactId>spring-cloud-starter-eureka-server</artifactId>   </dependency>  </dependencies>  <dependencyManagement>   <dependencies>    <dependency>     <groupId>org.springframework.cloud</groupId>     <artifactId>spring-cloud-dependencies</artifactId>     <version>Dalston.SR1</version>     <type>pom</type>     <scope>import</scope>    </dependency>   </dependencies>  </dependencyManagement>

To implement a service registration center, you only need to use the @ EnableEurekaServer Annotation on the project startup class EurekaServerApplication.

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

By default, the Service Registration Center will also try to register itself as a client, so we need to disable its client registration behavior, only in application. add the following information to the 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 view the Eureka page. From the red box, you can see that no task service instance is registered with the current service registration center.

 

Service provider: eureka-client

After an instance is registered, it needs to send a heartbeat message to the Registration Center. When the client registers with the server, it provides metadata such as host and port, URL, and homepage. The Eureka server receives heartbeat messages from each client instance. If the heartbeat times out, the instance is usually deleted from the registered server.

Create a springboot project: eureka-client. Its pom. xml configuration is as follows:

 <properties>   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>   <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>   <java.version>1.8</java.version>  </properties>  <dependencies>   <dependency>    <groupId>org.springframework.cloud</groupId>    <artifactId>spring-cloud-starter-eureka</artifactId>   </dependency>   <dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-web</artifactId>   </dependency>  </dependencies>  <dependencyManagement>   <dependencies>    <dependency>     <groupId>org.springframework.cloud</groupId>     <artifactId>spring-cloud-dependencies</artifactId>     <version>Dalston.SR1</version>     <type>pom</type>     <scope>import</scope>    </dependency>   </dependencies>  </dependencyManagement>

To implement a service provider, you only need to use the @ EnableEurekaClient Annotation on the project startup class EurekaClientApplication.

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

Configure application. properties as follows:

spring.application.name=eureka-clientserver.port=9002eureka.client.serviceUrl.defaultZone=http://localhost:9001/eureka/

By using the spring. application. name attribute, we can specify the microservice name for subsequent calls. You only need to use this name for service access.

The eureka. client. serviceUrl. defaultZone attribute corresponds to the configuration content of the Service Registration Center and specifies the location of the service registration center.

Use the server. port attribute to set different ports.

Start EurekaClientApplication class

Refresh http: // localhost: 9001/. You can see that our service provider has registered with the service registration center.

 

Create a DiscoveryController

Use discoveryClient. getServices () to obtain the registered service name, and use @ value to assign the information in the configuration file to the ip address.

@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; }}

Access: http: // localhost: 9002/client

 

Finally, the annotations @ EnableEurekaClient and @ EnableDiscoveryClient are described.

First, both annotations can implement service discovery. In spring cloud, the discovery service has many implementations (eureka, consul, zookeeper, and so on)

@ EnableEurekaClient is based on spring-cloud-netflix. The Service uses eureka as the Registration Center and has a single application scenario.

@ EnableDiscoveryClient is based on spring-cloud-commons. Other registration centers are used for the service.

GitHub: https://github.com/mingyuHub/springcloud

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.