Overview
Use a simple example to demonstrate the basic usage of EureKa and Ribbon in Spring Cloud.
Version and environment
Build eureka server
In Spring Cloud, you can use eureka to manage microservices, and microservices can be registered with eureka.
First, you can use IDEA's Spring Initialzr to create the eureka server registration center.
Modify the application.properties file and add the following content
spring.application.name=eureka-server eureka.instance.hostname=localhost eureka.client.register-with-eureka=false eureka.client.fetch-registry=false server.port=8881
Add @EnableEurekaServer annotation to the startup class ServerApplication generated by Spring Boot for us
package com.springcloud.eureka;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@EnableEurekaServer@SpringBootApplicationpublic class ServerApplication { public static void main(String[] args) { SpringApplication.run(ServerApplication.class, args); }}Enter http://localhost:8881/ in your browser
You can see the following interface:
You can see that the service has not been registered yet. At this point, a simple microservice registration center has been built.
Writing a microservice userservice
Next, use rest to build a microservice interface and register it in the registration center. Still using Spring Initialzr to build a new project. The usage is the same as above.
Note that this time you need to check the Eureka Discovery component. Instead of Eureka Server .
Modify the application.properties file and add the following content:
spring.application.name=user server.port=8882 eureka.client.service-url.defaultZone=http://localhost:8881/eureka/
Use the @EnableDiscoveryClient annotation in the UserApplication class that Spring Boot generates for us.
package com.springcloud;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@EnableDiscoveryClient@SpringBootApplicationpublic class UserApplication { public static void main(String[] args) { SpringApplication.run(UserApplication.class, args); }}Create a rest full microservice interface.
package com.springcloud;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class UserController { @GetMapping("/getUser") public String getUser() { return "I am user list."; }}After running UserApplication, visit http://localhost:8881/ again
You will find that the user service has been registered.
Writing a microservice order
Next, we build an order microservice and access the interface in the user microservice.
Still use Spring Initialzr to build a new project. The user microservice can be deployed on multiple machines. When the client accesses this service, the request can be routed to any machine with the user service deployed. Therefore, the client needs to use a routing algorithm to schedule the user service. In Spring Cloud, you can use Ribbon components to do client routing. Ribbon will go to the service registration center to obtain the service list in order to call the corresponding service.
This time, in addition to checking the Eureka Discovery component. You also need to check Ribbon .
Modify the application.properties file and add the following content:
spring.application.name=order server.port=8883 eureka.client.service-url.defaultZone=http://localhost:8881/eureka/
Add the following configuration to the OrderApplication class generated by Spring Boot .
package com.springboot;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.client.loadbalancer.LoadBalanced;import org.springframework.context.annotation.Bean;import org.springframework.web.client.RestTemplate;@EnableDiscoveryClient@SpringBootApplicationpublic class OrderApplication { @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(OrderApplication.class, args); }} Since Ribbon is used, the @LoadBalanced annotation needs to be used here.
Write OrderController .
package com.springboot;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate;@RestControllerpublic class OrderController { @Autowired private RestTemplate restTemplate; @GetMapping("/getOrderUser") public String getOrderUser() { return restTemplate.getForEntity("http://user/getUser",String.class).getBody(); }} After running OrderApplication , visit http://localhost:8881/
You will find that the order service has also been registered in the registration center.
Next, we access the getOrderUser method in OrderController and trigger the call to getUser method of UserController .
Enter: http://localhost:8883/getOrderUser
You can see that it returns: I am user list.
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.