Use Eureka to achieve service governance
Function: Realize 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 subprojects of Spring Cloud. Its main content is to package 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 commonly used modules in their applications and build a huge distributed system. Its main modules include: service discovery (Eureka), circuit breaker (Hystrix), intelligent routing (Zuul), client load balancing (Ribbon), etc.
Project Practical Practice:
Service Registration Center: eureka-server
Function: Service registration center provides service registration function
Service provider: eureka-client
Function: Register a service to the service registration center
Service Registration Center: eureka-server
Create a new springboot project: eureka-server, and 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>
It is very simple to implement the function of a service registration center. You only need to use the @EnableEurekaServer annotation on the project's 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 and just add the following information to 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, visit http://localhost:9001/ to see Eureka's page. From the location of the red box, you can see that there is no task service instance registered to the current service registration center.
Service provider: eureka-client
After each instance is registered, a heartbeat is needed to send a heartbeat to the registration center. When the client registers with the server, it will provide some metadata, such as the host and port, URL, home page, etc. Eureka server receives heartbeat messages from each client instance. If the heartbeat times out, the instance is usually removed from the registered server.
Create a new springboot project: eureka-client, and 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> </dependency> </dependency> </dependency> <dependencyManagement> <dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
It is also very simple to implement a service provider. Just use the @EnableEurekaClient annotation on the project's startup class EurekaClientApplication.
@EnableEurekaClient @SpringBootApplication public class EurekaClientApplication { public static void main(String[] args) { new SpringApplicationBuilder( EurekaClientApplication.class) .web(true).run(args); } }Configure the following in application.properties
spring.application.name=eureka-clientserver.port=9002eureka.client.serviceUrl.defaultZone=http://localhost:9001/eureka/
Through the spring.application.name property, we can specify the name of the microservice when calling it, so that the service can be accessed.
The eureka.client.serviceUrl.defaultZone property corresponds to the configuration content of the service registration center and specifies the location of the service registration center.
Use the server.port property to set different ports.
Start the EurekaClientApplication class
Refresh http://localhost:9001/, and you can see that our service provider has 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, let me explain the two annotations @EnableEurekaClient and @EnableDiscoveryClient
First of all, both annotations can realize the function of service discovery. There are many implementations of discovery service in spring cloud (eureka, consul, zookeeper, etc.)
@EnableEurekaClient is based on spring-cloud-netflix. The service uses eureka as the registration center, and the usage scenarios are relatively single.
@EnableDiscoveryClient is based on spring-cloud-commons. The service adopts other registration centers.
GitHub: https://github.com/mingyuHub/springcloud
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.