[TOC]
1.1 what is Eureka
When developing large-scale projects, service providers and service callers will be very large, and the cost of managing services will increase exponentially. Eureka will be responsible for service registration, discovery and status monitoring.
1. Registration: Eureka is responsible for managing and recording the information of service providers
When the service is started, the Register method of the REST API of Eureka Server is called to Register the application instance information.
- Java application, encapsulating API call with Eureka Client of Netflix
- Spring Cloud reference. You can use Spring Cloud starter Netflix Eureka client to automatically configure and register services based on Spring Boot.
2. Discovery: Service callers don't need to find their own services, but tell Eureka their own needs, and Eureka will tell you the services that meet your needs
3. Monitoring: between the service provider and Eureka, it is monitored through the "heartbeat" mechanism. When a service provider has problems, Eureka will naturally remove it from the service list
1.2 schematic diagram
Basic structure:
- Eureka: it is the service registry (it can be a cluster) that exposes its address externally
- Provider: register your own information (address, what service to provide) with Eureka after startup
- Consumer: subscribe to Eureka service, Eureka will send the address list of all providers for the service to consumers, and update it regularly
- Heartbeat (renewal): the provider periodically refreshes its status to Eureka via http
2.1 introduction cases
1. Preparation of registration center: Eureka ﹣ demo
2. Register the service in the registration center: Eureka \
3. The caller obtains services from the registration center: Eureka ﹣ client
2.1.1 preparation of Registration Center
Core steps:
1. Add web initiator and eureka service dependency
2. Configure eureka in the YML file
3. Add development service annotation @ EnableEurekaServer to startup class
Step 1: create the Eureka? Demo module
Step 2: modify pom.xml file and add dependency
<dependencies> <!--web Start dependence--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Eureka Server side --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies>
Step 3: create application.yml and configure the registry
# Port number server: port: 10086 # Service name spring: application: name: eureka_demo # Configuration of eureka eureka: client: service-url: defaultZone: http://localhost:${server.port}/eureka/
Step 4: write startup class and add notes
package com.czxy; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args ); } }
Step 5: after starting the project, the console is abnormal, but the project can be used normally
Visit:
http://localhost:10086
Step 6: modify pom.xml to solve the problem of startup exception
-
Register with Eureka: false ා whether to register your own information to the registration center, the default is true
-
Fetch registry: false - whether to pull information of other services. The default value is true
-
Service URL: the address of the registration center. Now it is its own address. If it is a cluster, add the address of other servers.
# Port number server: port: 10086 # Service name spring: application: name: eureka_demo # Configuration of eureka eureka: client: service-url: defaultZone: http://localhost:${server.port}/eureka/ register-with-eureka: false fetch-registry: false
2.1.2. eurekaservic(8080) registered in Eureka Registration Center
Core steps
1. Add eureka.client client dependency
2. Determine the location of the registry in the YML file
3. Add the startup class and open the eureka client annotation @ EnableEurekaClient
Step 1: create the Eureka service module
Step 2: modify pom.xml file and add eureka client dependency
<dependencies> <!--web Start dependence--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Eureka Client --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!--spring boot Monitor--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies>
Step 3: create the yml file and determine the location of the eureka registry
server: port: 8080 spring: application: name: service eureka: client: service-url: #Registry location defaultZone: http://localhost:10086/eureka/ instance: #web page display effect and access path instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port} prefer-ip-address: true
On the Eureka monitoring page, view the service registration information:
In the status column, the following information is displayed:
• UP(1): the representative has now started an example without cluster
• descktop-2mvec12: User Service: 8081: is the name (instance ID) of the example,
- the default format is ${hostname} + ${spring.application.name} + ${server.port}
– instance ID is the only standard to distinguish different instances of the same service, so it cannot be repeated.
We can modify its composition through the instance ID attribute:
eureka: instance: *#web page display effect and access path * instance ID: ${spring. Application. Name}: ${spring. Cloud. Client. IP address}: ${server. Port}
Step 4: write the startup class
package com.czxy; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class ServiceApplication { public static void main(String[] args) { SpringApplication.run(ServiceApplication.class,args); } }
Test:
Step 6: provide test content
@RestController @RequestMapping("/test") public class TestController { @GetMapping public ResponseEntity<String> test(){ return ResponseEntity.ok("test data"); } }
2.1.3 consumer Eureka ABCD client (9090) obtains services from Eureka
Modify consumer demo to try to get services from EurekaServer.
Step 1: create the Eureka? Client module
Step 2: modify pom.xml file (same as Eureka service)
<dependencies> <!--web Start dependence--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Eureka Client --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!--spring boot Monitor--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies>
Step 3: create the yml file
server: port: 9090 spring: application: name: client eureka: client: service-url: #Registry location defaultZone: http://localhost:10086/eureka/ instance: #web page display effect and access path instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port} prefer-ip-address: true
Step 4: write the startup class
package com.czxy; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class ClientApplication { public static void main(String[] args) { SpringApplication.run(ClientApplication.class,args); } }
test
Step 6: write a test program and access the service directly through the IP address through the RestTemplate
package com.czxy.controller; import org.springframework.context.annotation.Bean; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import javax.annotation.Resource; @RestController @RequestMapping("/data") public class DataController { @Bean public RestTemplate restTemplate(){ return new RestTemplate(); } @Resource private RestTemplate restTemplate; @GetMapping public ResponseEntity<String> data(){ return restTemplate.getForEntity("http://localhost:8080/test",String.class); } }