Introduction to Eureka
Eureka provides REST-based services, which are mainly used in service management in the cluster. Eureka provides client components based on Java language, which implements load balancing functions, creating conditions for the cluster deployment of business components. Using this framework, business components can be registered in Eureka containers, which can be deployed in clusters, and Eureka mainly maintains a list of these services and automatically checks their status.
Program Structure
Create Eureka Server
maven dependency
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependency> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies>
Change the spring boot startup port in application.yml
server: port: 8761
Enable Eureka service annotation @EnableEurekaServer
@EnableEurekaServer@SpringBootApplicationpublic class EKServerApplication { public static void main(String[] args) { new SpringApplicationBuilder(EKServerApplication.class).run(args); }}Start springboot
[Thread-11] oscneserver.EurekaServerBootstrap: Initialized server context[main] sbcetTomcatEmbeddedServletContainer: Tomcat started on port(s): 8761 (http)[main] .scnesEurekaAutoServiceRegistration: Updating port to 8761[main] cbfirstEkServer.EKServerApplication: Started EKServerApplication in 8.594 seconds (JVM running for 9.523)
An exception that cannot be connected to the server will appear during startup. This is because Eureka treats itself as a client to grab registration information when starting.
The code copy is as follows:
com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
Add the following configuration and will not appear again when starting
eureka: client: registerWithEureka: false fetchRegistry: false
registerWithEureka declares whether to register its own information to the Eureka server, the default value is true.
fetchRegistry declares whether to crawl the registration information into the Eureka server, the default value is true.
Visit http://localhost:8761 in your browser to view the image description of the Eureka console input
Create a service provider
rely
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency>
Configure the port, Eureka instance name, and Eureka service address in application.yml
server: port: 8080spring: application: name: ek-providereureka: instance: hostname: localhost client: serviceUrl: defaultZone: http://localhost:8761/eureka/
Create a REST service
@RestControllerpublic class HelloController { @RequestMapping("/hello") public String hello(HttpServletRequest request) { return "hello:" + request.getRequestURL(); }}Enable Eureka client annotation @EnableEurekaServer
@EnableEurekaClient@SpringBootApplicationpublic class EkProviderApplication { public static void main(String[] args) { new SpringApplicationBuilder(EkProviderApplication.class).run(args); }}After startup, you can see in the Eureka console that the service provider has registered in Eureka
Create a service caller
rely
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency>
Configure the port, Eureka instance name, and Eureka service address in application.yml
server: port: 9000spring: application: name: ek-invokeeureka: instance: hostname: localhost client: serviceUrl: defaultZone: http://localhost:8761/eureka/
Write a REST service to call the service provider's "/hello"
@RestController@Configurationpublic class InvokeController { @Bean @LoadBalanced public RestTemplate getRestTemplate() { return new RestTemplate(); } @RequestMapping("/invoke") public String invoke() { RestTemplate restTemplate = getRestTemplate(); return restTemplate.getForObject("http://ek-provider/hello", String.class); }}In the traditional mode, we usually use Httpclient in Apache to call the REST service. Here we use Spring to provide the RestTemplate component that calls the REST service. RestTemplate itself does not have the ability to call distributed services, but after the RestTemplate bean is modified by the @LoadBalanced annotation, this RestTemplate instance has the ability to access distributed services, thanks to the various interceptors provided by Spring.
Enable Eureka client annotation @EnableEurekaServer
@EnableEurekaClient@SpringBootApplicationpublic class EkInvokeApplication { public static void main(String[] args) { new SpringApplicationBuilder(EkInvokeApplication.class).run(args); }}After startup, you can see in the Eureka console that the service caller has been registered in Eureka
Then access the "invoke" interface of the service caller in the browser. Return as follows
Summarize
The Eureka server maintains the latest registration information through the heartbeat link, which is stored in memory.
Eureka service providers mainly conduct:
Eureka service callers mainly conduct:
Source code address: https://github.com/xc564864894/springcloud/tree/master/Eureka(%E4%B8%80)
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.