Springcloud-eureka service registration and discovery

  • Monolithic architecture: simple and convenient, highly coupled, poor scalability, suitable for small projects. Example: Student Management System

  • Distributed architecture: loosely coupled and scalable, but complex and difficult. Suitable for large-scale Internet projects, such as JD.com and Taobao

  • Microservices: A Good Distributed Architecture Solution

    ①Advantages: smaller split granularity, more independent services, and lower coupling

    ②Disadvantages: the structure is very complex, and the difficulty of operation and maintenance, monitoring and deployment is increased

  • SpringCloud is a one-stop solution for microservice architecture, integrating various excellent microservice functional components

2.2. Example of service splitting

Taking the microservice cloud-demo in the pre-class materials as an example, its structure is as follows:

 

cloud-demo: parent project, manage dependencies

  • order-service: order microservice, responsible for order-related business

  • user-service: user microservice, responsible for user-related business

Require:

  • Both the order microservice and the user microservice must have their own databases, independent of each other

  • Both order service and user service expose Restful interfaces to the outside world

  • If the order service needs to query user information, it can only call the Restful interface of the user service, and cannot query the user database

 Register Eureka server

First of all, everyone registers the center server: eureka-server, which must be an independent microservice

1) Create eureka-server service

Under the cloud-demo parent project, create a submodule:

Fill in the module information:

 Then fill in the service information:

2) Introduce eureka dependency

Introduce the starter dependency provided by SpringCloud for eureka:

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

3) Write the startup class

 To write a startup class for the eureka-server service, be sure to add a @EnableEurekaServer annotation to enable the registration center function of eureka:

package cn.itcast.eureka;

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);
    }
}

4) Write configuration files

Write an application.yml file with the following content:

server:
  port: 10086
spring:
  application:
    name: eureka-server
eureka:
  client:
    service-url: 
      defaultZone: http://127.0.0.1:10086/eureka

5) Start the service

Start the microservice, and then visit it in the browser: http://127.0.0.1:10086

See the following result should be successful:

 View the list of services on eureka 

 

 

service registration

Next, we register user-service to eureka-server

1) Introduce dependencies

In the pom file of user-service, introduce the following eureka-client dependencies: 

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

2) Configuration file

In user-service, modify the application.yml file and add the service name and eureka address:

spring:
  application:
    name: userservice
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka

3) Start multiple user-service instances

In order to demonstrate a scenario where a service has multiple instances, we add a SpringBoot startup configuration and start a user-service.

First, copy the original user-service startup configuration:

Then, in the pop-up window, fill in the information

 1. The method of copying two order services, right click, select copy Configuration, and then add in the selected place of the picture

-Dserver.port=8082 (8082 refers to the port number)

 Now, two user-service startup configurations will appear in the SpringBoot window:

 

However, the first is port 8081 and the second is port 8082.

Start two user-service instances:

 View the eureka-server management page:

1. A main service of eureka

2. Two order service

3. One User Service

 service discovery

1. Go to eureka-server to pull the instance list of user-service services and implement load balancing.

But we don't need to do these actions, we just need to add some annotations.

In the OrderApplication of order-service, add a @LoadBalanced annotation to the RestTemplate Bean:

Go to eureka-server to pull the instance list of user-service service and implement load balancing.

But we don't need to do these actions, we just need to add some annotations.

In the OrderApplication of order-service, add a @LoadBalanced annotation to the RestTemplate Bean:  

    /**
     * 创建RestTemplate并注入Spring容器
     */
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

 Modify the queryOrderById method in the OrderService class under the cn.itcast.order.service package in the order-service service. Modify the url path to access, and use the service name instead of ip and port:

   @Autowired
    private RestTemplate restTemplate;

 public Order queryOrderById(Long orderId) {
        // 1.查询订单
        Order order = orderMapper.findById(orderId);
        // 2.利用RestTemplate发起http请求,查询用户
        // 2.1.url路径
        String url = "http://userservice/user/" + order.getUserId();
        // 2.2.发送http请求,实现远程调用
        User user = restTemplate.getForObject(url, User.class);
        // 3.封装user到Order
        order.setUser(user);
        // 4.返回
        return order;
    }

Summarize

 

 

Guess you like

Origin blog.csdn.net/zxc472504515/article/details/125715740