SpringCloud – 1 – Service Governance Eureka

1. Overview of Eureka

1, Eureka Features

  • Simply pass Simple introduction of dependency and annotation configuration, the microservice application built by SpringBoot can be easily integrated with the Eureka service governance system.
  • Eureka is responsible for service governance, namely: automatic registration and discovery of microservice instances. Service registration and discovery are implemented in the form of application service name.
  • Follow the AP principle (high availability, partition fault tolerance), and use a self-protection mechanism to ensure high availability. Implement service discovery and failover.

2. Two major components of Eureka and three major roles

  • Two major components: Eureka Server (providing registration services), Eureka Client (production and consumption service).
  • Three roles: Service Provider and Service Consumer are not strict concepts. Service Consumer can also register with Eureka Server at any time to allow Become a Service Provider. After the Eureka Client is started, it immediately registers with the service registry. At the same time, the registration information of all instances, including IP address, port, etc., will be obtained from Eureka Server and cached locally. This information is updated every 30 seconds by default. If the communication with the Eureka Server is interrupted, the Service Consumer can still communicate with the Service Provider through the local cache.
    • Eureka Server: Service registry, responsible for maintaining the list of registered services.
    • Service Provider: The service provider, as an Eureka Client, does service registration, contract renewal, and offline operations with Eureka Server. The main registration data includes service name, machine ip, port number, domain name, etc. Wait.
    • Service Consumer: The service consumer, as an Eureka Client, obtains the registration information of the Service Provider from the Eureka Server (obtains the instance list of all services and caches it locally), and communicates with the Service Provider through remote calls.
  • Eureka Client sends a heartbeat request Realize service registration and contract renewal. In addition, Eureka Server also needs to monitor whether the services in the service list are available in the form of heartbeats. If they are not available, they need to be removed from the service list to achieve the effect of troubleshooting services.

share picture

Second, build a service registration center-Eureka Server

1, pom.xml

  First , Create a basic Spring Boot project, and introduce the necessary pom dependencies:

xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/ xsd/maven-4.0.0.xsd">
<modelVersion >4.0.0modelVersion>
<parent >
<groupId >org.springframework.bootgroupId>
<artifactId >spring-boot-starter-parentartifactId>
2.0.2.RELEASE
<relativePath />
parent>

<groupId >com.github.gavincodergroupId>
<artifactId >eureka-serverartifactId>
<version >0.0.1-SNAPSHOTversion>
<name >eureka-servername>

<properties >
<java.version>1.8java.version>
properties>

<dependencies >
<dependency >
<groupId >org.springframework.bootgroupId>
<artifactId >spring-boot-starterartifactId>
dependency>

<dependency >
<groupId >org.springframework.cloudgroupId>
<artifactId >spring-cloud-starter-netflix-eureka-server artifactId>
dependency>
dependencies>

<dependencyManagement >
<dependencies >
<dependency >
<groupId >org.springframework.cloudgroupId>
<artifactId >spring-cloud-dependenciesartifactId>
<version >Finchley.SR2version>
<type >pomtype>
<scope >importscope>
dependency>
dependencies>
dependencyManagement>

<build >
<plugins >
<plugin >
<groupId >org.springframework.bootgroupId>
<artifactId >spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>

project>

2, application.yml

#server

server: port: 8888

#eureka
eureka: instance: hostname: localhost client: #Declare yourself as a server
register-with-eureka: false #false means not to register yourself with the registration center span>
fetch-registry: false #false means that you are the registration center and your responsibility is to maintain the list of services , Do not participate in the search
service-url: #Set the exposed address of eureka server
default-zone: http://${eureka.instance.hostname}:${server.port}/eureka/

3. Startup class

  Enable the service registry function through the annotation of @EnableEurekaServer.

package< span style="color: #000000;"> com.github.gavincoder.server; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure .SpringBootApplication; import org. springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class ServerApplication {public static void main(String[] args) {SpringApplication.run(ServerApplication.class, args);} }

Note:

  • springboot and springcloud versions have strict constraints
  • Eureka artifactid changed: spring-cloud-starter-netflix-eureka-server
  • For detailed experience sharing, see my other blog post: https://www.cnblogs.com/gavincoder/p/11032015.html

< /p>

4. Running effect

   visit Eureka Server The address of the registry center exposed to the outside: http://${eureka.instance.hostname}:${server.port}

Share a picture

Third, build Eureka-Client—service provider

1, pom.xml

xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/ xsd/maven-4.0.0.xsd">
<modelVersion >4.0.0modelVersion>
<parent >
<groupId >org.springframework.bootgroupId>
<artifactId >spring-boot-starter-parentartifactId>
2.0.2.RELEASE
<relativePath />
parent>
<groupId >com.github.gavincodergroupId>
<artifactId >productartifactId>
<version >0.0.1-SNAPSHOTversion>
<name >productname>
<description >Demo project for Spring Bootdescription>

<properties >
<java.version>1.8java.version>
properties>

<dependencies >
<dependency >
<groupId >org.springframework.bootgroupId>
<artifactId >spring-boot-starterartifactId>
dependency>
org.springframework.boot spring-boot-starter-web < dependency> org.springframework.cloud spring-cloud-starter-netflix-eureka-client
dependencies>

<dependencyManagement >
<dependencies >
<dependency >
<groupId >org.springframework.cloudgroupId>
<artifactId >spring-cloud-dependenciesartifactId>
Finchley.SR2
<type >pomtype>
<scope >importscope>
dependency>
dependencies>
dependencyManagement>

<build >
<plugins >
<plugin >
<groupId >org.springframework.bootgroupId>
<artifactId >spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>

project>

Note:

  • Web dependency must be added, otherwise Eureka Client automatically stops when it starts, and an error is reported: “Shutting down DiscoveryClient …”
 <dependency> 

<groupId >org.springframework.bootgroupId>
<artifactId >spring-boot-starter-webartifactId>
dependency>

2, application.yml

#server

server: port: 8886

#spring
spring: application: name: eureka-client-product-service < span style="color: #008000;">#
eureka
eureka: client: serviceUrl: defaultZone: http://localhost: 8888/eureka/
#eureka: # instance: # service-url: # default-zone: http://localhost: 8888/eureka/

Note:< /p>

  • yml attribute configuration file must check whether the necessary attributes are all valid, and invalid status cannot read the attribute value.
  • Example: The following configuration causes eureka client to fail to find the registration center address for registration, and registration fails.

share picture

Startup error: com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect p>

share picture

3. Startup class

  Add a comment: @EnableEurekaClient

< pre>package com.github.gavincoder. product; import org.springfram ework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; < /span>import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @EnableEurekaClient @SpringBootApplication public class span> ProductApplication {public static void main(String[] args) {SpringApplication.run(ProductApplication.class, args);}} } div>

Fourth, build Eureka-Client– -Service consumer

1, pom.xml

  —same service provider

2, application.yml p>

#server

server: port: 8885

#spring
spring: application: name: eureka-client-order-service #eureka
eureka: client: serviceUrl: defaultZone: http://localhost:8888/eureka/

 

3、启动类

package com.github.gavincoder.order; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @EnableEurekaClient @SpringBootApplication public class OrderApplication { public static void main(String[] args) { SpringApplication.run(OrderApplication.class, args); } }

 

四、Eureka运行效果

分享图片

xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
2.0.2.RELEASE
<relativePath/>
parent>

<groupId>com.github.gavincodergroupId>
<artifactId>eureka-serverartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>eureka-servername>

<properties>
<java.version>1.8java.version>
properties>

<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>

<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
dependency>
dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>Finchley.SR2version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>

project>

#server

server: port: 8888

#eureka
eureka: instance: hostname: localhost client: #声明自己是个服务端
register-with-eureka: false #false表示不向注册中心注册自己
fetch-registry: false #false表示自己就是注册中心,职责是维护服务清单,不参与检索
service-url: #设置eureka server对外暴露的地址
default-zone: http://${eureka.instance.hostname}:${server.port}/eureka/

package com.github.gavincoder.server; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class ServerApplication { public static void main(String[] args) { SpringApplication.run(ServerApplication.class, args); } }

xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
2.0.2.RELEASE
<relativePath/>
parent>
<groupId>com.github.gavincodergroupId>
<artifactId>productartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>productname>
<description>Demo project for Spring Bootdescription>

<properties>
<java.version>1.8java.version>
properties>

<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-netflix-eureka-client
dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
Finchley.SR2
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>

project>

        <dependency>  

<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>

#server

server: port: 8886

#spring
spring: application: name: eureka-client-product-service #eureka
eureka: client: serviceUrl: defaultZone: http://localhost:8888/eureka/
#eureka: # instance: # service-url: # default-zone: http://localhost:8888/eureka/

package com.github.gavincoder.product; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient;  @EnableEurekaClient @SpringBootApplication public class ProductApplication { public static void main(String[] args) { SpringApplication.run(ProductApplication.class, args); } }

#server

server: port: 8885

#spring
spring: application: name: eureka-client-order-service #eureka
eureka: client: serviceUrl: defaultZone: http://localhost:8888/eureka/

package com.github.gavincoder.order; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @EnableEurekaClient @SpringBootApplication public class OrderApplication { public static void main(String[] args) { SpringApplication.run(OrderApplication.class, args); } }

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 2230 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.