Teach you how to build a SpringCloud project (3) Integrate the Eureka service registration center

What are microservices? A series will be seen at a glance!

1. Teach you how to build a SpringCloud project (1) Detailed explanation with pictures and texts, fool-like operation

2. Teach you how to build a SpringCloud project (2) Producers and consumers

3. Teach you how to build a SpringCloud project (3) Integrate the Eureka service registration center

4. Teach you how to build the SpringCloud project (4) Eureka cluster version construction

5. Teach you how to build the SpringCloud project (5) Build the producer cluster version

6. Teach you how to build a SpringCloud project (6) Eureka realizes service discovery

7. Teach you how to build a SpringCloud project (7) Integrate the Consul service registration center

8. Teach you how to build a SpringCloud project (8) Integrated Ribbon load balancer

9. Teach you how to build a SpringCloud project (9) Integrate OpenFeign service interface calls

10. Teach you how to build a SpringCloud project (10) Integrate Hystrix service downgrade

11. Teach you to build a SpringCloud project (11) Integrating Hystrix's service fuse

12. Teach you how to build a SpringCloud project (12) Integrate Hystrix's graphical Dashboard real-time monitoring

13. Teach you how to build a SpringCloud project (13) Integrate a new generation of Gateway

14. Teach you how to build a SpringCloud project (14) Integrated Config Distributed Configuration Center

15. Teach you how to build a SpringCloud project (15) Integrated Bus message bus

16. Teach you how to build a SpringCloud project (16) Integrated Stream message driver

17. Teach you how to build a SpringCloud project (17) Integrating Sleuth distributed link tracking

Continue to update, welcome to like and follow!
Let's get to know Eureka first: official introduction

1. Understanding Eureka

1. What is service governance?

Springcloud encapsulates the Eureka module developed by Netflix to implement service governance. In the traditional RPC remote call, managing the dependencies between each service and service is complicated, and the management is complicated, so service governance is needed to manage the dependencies between services and services, which can realize service calling, load balancing, fault tolerance, etc. Service discovery and registration.

2. What is service registration and discovery?

Eureka adopts the design architecture of CS, and the Eureka Server server serves as the server of the service registration function, which is the service registration center. For other microservices in the system, use the Eureka Client to connect to the Eureka Server server and maintain a heartbeat connection, so that the system maintainers can use the ureka Server to monitor whether each microservice in the system is running normally.

In service registration and discovery, there is a registry. When the server starts, it will register the information of the current server, such as: service address, communication, etc., to the registration center in the form of an alias, and the other party (consumer|producer) will obtain it from the registration center in the form of the alias The actual service communication address, and then realize the core design idea of ​​the local RPC call and RPC remote call framework. The focus here is the registry, because the registry manages the dependencies between each service and service. In any RPC framework, there will be a registration center that stores service address-related information, that is, the interface address.

The following figure shows the architecture diagram of Eureka:
insert image description here

2. Build Eureka server service

Create a new module, which is the service of Eureka. The steps are the same as before creating a new module, the module name: cloud-eureka-server. Refer to the previous two articles. Here we start to paste the code by modifying the pom file. Note: The main thing is to add eureka-server dependencies.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>com.study.springcloud</groupId>
        <artifactId>mcroservice</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
 
    <artifactId>cloud-eureka-server</artifactId>
    <dependencies>
        <!--eureka-server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
        <dependency>
            <groupId>com.study.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <!--boot web actuator-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--一般通用配置-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
    </dependencies>
 
</project>

Configure the yml file

server:
  port: 7001
eureka:
  instance:
    hostname: eureka7001.com #eureka服务端的实例名称
  client:
    register-with-eureka: false     #false表示不向注册中心注册自己。
    fetch-registry: false     #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    service-url:
      #设置与eureka  server交互的地址和注册服务都需要依赖这个地址
      defaultZone: http://eureka7001.com:7001/eureka/  #单机就是指向自己

The main startup class: because it is the server of Eureka, the annotation @EnableEurekaServer should be added

package com.buba.springcloud.eureka;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
 
 
@SpringBootApplication
@EnableEurekaServer
public class EurekaMain {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(EurekaMain.class,args);
    }
}

To start the project, we directly access the address http://localhost:7001/ , port 7001 is the port of the changed service, and the following interface appears, indicating that the startup is successful.
insert image description here

Now that Erueka's server has been successfully started, we will now register our order producers and consumers with our Erueka registration center.

3. Register the producer to the Erueka service

First we modify the pom file in the cloud-provide-payment service. Only need to add eureka-client dependency.

<!--   添加eureka客户端的依赖     -->
 <dependency>
      <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
 </dependency>

The next step is to modify the yml file and add the following configuration in the yml file.

#eureka配置
eureka:
  client:
    #表示是否将自己注册进eureka  默认为true
    register-with-eureka: true
    #是否从EurekaServer中抓取已有的注册信息,默认为true,单点无所谓,集群必须设置true才能和ribbon使用负载均衡
    fetch-registry: true
    service-url:
      #单机配置
      defaultZone: http://localhost:7001/eureka

Then modify the main startup class to add this annotation@EnableEurekaClient

@SpringBootApplication
@EnableEurekaClient
public class PayMentMain {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(PayMentMain.class,args);
    }
}

Finally, we need to start the service on the Eureka server first, and then start the producer. Then visit: http://localhost:7001/ , the following interface will appear, we can see the service mcroservice-payment in the service list
insert image description here

The service name mcroservice-payment in the service list is configured in the yml file of the mcroservice-payment service, as shown in the following figure:
insert image description here

Let's visit the previous interface to see if the access is successful. Enter http://localhost:8001/payment/get/1 and you can access it successfully, as shown in the figure below.

insert image description here

4. Register the consumer service to the Erueka service

The specific modification steps are similar to our above service. In the cloud-consumer-order microservice project, only the eureka-client dependency needs to be added.

<!--   添加eureka客户端的依赖     -->
 <dependency>
      <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
 </dependency>

The next step is to modify the yml file and add the following configuration in the yml file.

#eureka配置
eureka:
  client:
    #表示是否将自己注册进eureka  默认为true
    register-with-eureka: true
    #是否从EurekaServer中抓取已有的注册信息,默认为true,单点无所谓,集群必须设置true才能和ribbon使用负载均衡
    fetch-registry: true
    service-url:
      #单机配置
      defaultZone: http://localhost:7001/eureka

Then modify the main startup class to add this annotation@EnableEurekaClient

@SpringBootApplication
@EnableEurekaClient
public class OrderMain {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(OrderMain.class,args);
    }

Finally, we need to start the Eureka server service first, and then start the consumer service. Then visit: http://localhost:7001/ , the following interface will appear, we can see that in addition to the service mcroservice-payment, the service mcroservice-order is added in the service list.

insert image description here

Now both the producer service and the consumer service have been successfully registered to our Eureka registration center, let's test whether the consumer service can be accessed successfully. Enter http://localhost/consumer/payment/get/1 , the access is successful, as shown below:

insert image description here

We have successfully registered producers and consumers to the registration center, and can also obtain information through the interface. so easy.

insert image description here

In the next article, continue to build a cluster version of Eureka, continue to pay attention and like it. We continue to update.

Guess you like

Origin blog.csdn.net/weixin_39570655/article/details/131764668