Take you hand in hand to build the first SpringCloud project (2)

Some of my other articles related to Springcloud ~ welcome to watch



foreword

Continuing from the above, we have successfully built a simple SpringCloud project in the above, realizing simple access from the service consumer to the service of the service provider. This article will integrate Eureka on the basis of the above to complete the construction of the service registration center.

Before opening the text, let us briefly understand the background of eureka~

During the development process, we always need to manually use the ip:port method to directly call the service, and as the complexity of the project deepens, the number of services is also increasing rapidly. maintenance and load balancing. At this time, the concept of service registration center came into being.

Using the service registration center can realize service governance, service dynamic expansion, and load , and Eureka, which we are going to talk about today, is the veteran player who implements the service registration center.


1. What is Eurea?

Eureka is a service discovery framework developed by Netflix . It is itself a REST -based service. SpringCloud integrates it into its subproject spring-cloud-netflix to realize SpringCloud's service discovery function.

To put it simply: the service provider registers the service into Eureka, and the service caller obtains the corresponding service through Eureka. During this process, Eureka realizes the automatic registration , service discovery and status monitoring of the service .

2. Advantages of Eureka

  • Provides complete service registration and service discovery implementations

  • Seamless integration with spirngcloud

  • Use AP instead of CP

Eureka's functions focus on service registration and discovery, and there will be no data race. That is, at the same time, there will not be multiple threads competing for the same data. The AP principle guarantees availability and achieves eventual consistency.

 eureka AP architecture

3. Eureka actual combat case 

 

--------------------------------- Build Eureka Service Registration Center- ----------- ------------------------------------

 

1. Create a new Module under the cloud project: cloud-eureka-server7001 

2. Add eureka server and other corresponding dependencies in the pom.xml file

    <dependencies>
        <!--eureka-server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
        <dependency>
            <groupId>com.canrioyuan</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.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </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>

3. Create an application.yaml in resource and configure it

server:
  port: 7001


eureka:
  instance:
    hostname: eureka7001.com #eureka服务端的实例名称
  client:
    register-with-eureka: false     #false表示不向注册中心注册自己。
    fetch-registry: false     #表示是否从Eureka Server获取注册的服务信息
    service-url:
      #单机即指向7001自己
       defaultZone: http://eureka7001.com:7001/eureka

 4. Create a startup class in the corresponding directory and add the @EnableEurekaServer annotation to indicate that this is a service registry component

package com.canrioyuan;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer//表示这是一个服务注册中心的服务组件
public class MainEureka7001 {
    public static void main(String[] args) {
        SpringApplication.run(MainEureka7001.class,args);
    }
}

5. Start MainEureka7001, enter http://eureka7001.com:7001/ in the URL  , as shown in the figure below, the Eureka service registration center is successfully configured!

 

--------------------------------- Register the service into the Eureka service registration center --------- ---------------------------

 

We first register the service provider cloud-provider-payment8001 into the service registration center

1. Add eureka-client dependency in the pom.xml file

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

 2. Add the corresponding Eureka configuration in the application.yaml file

eureka:
  instance:
    instance-id: payment8001
    #以IP地址注册到服务中心,相互注册使用IP地址
    prefer-ip-address: true


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

3. Add the annotation @EnableEurekaClient (or @EnableDiscoveryClient) to enable service discovery configuration in the startup class

package com.canrioyuan;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@MapperScan(value = "com.canrioyuan.mapper")
@EnableEurekaClient
//@EnableDiscoveryClient  //可以通过服务发现来获得该服务的信息
/**
 * @EnableEurekaClient 和 @EnableDiscoveryClient
 * 两者之间,如果注册中心使用Eureka我们推荐使用@EnableEurekaClient
 * 如果是其他的服务注册中心推荐使用@EnableDiscoveryClient来实现服务发现
 */
public class PaymentMain8001 {
    public static void main(String[] args){
        SpringApplication.run(PaymentMain8001.class,args);
    }
}

4. Start PayMain8001 , then start  MainEureka7001, enter http://eureka7001.com:7001/  in the URL  , as shown in the figure below, it means that the service of the 8001 service provider has been successfully registered in the Eureka service registration center! ! !

 

According to the same steps, we can also register the consumer module cloud-consumer-order80 into the eureka service registration center. After successful registration, the result is as follows:

 

So far, we can perform service management and status monitoring on registered services through eureka! !

​​​​​​​

Four, pay attention

Although Eureka's functions are very powerful, it is worth mentioning that  Eureka2.0 has stopped updating in July 2018. Alternative Eureka technologies such as Consul, Nacos, and Zookeeper have also begun to emerge on the market , but Eureka As the pioneer of the service registry, many of its structures and ideas are still worth learning.

 


conclusion

So far, our tutorial today is over~

Thank you for reading, I hope my article can bring you help! ! !

 

Guess you like

Origin blog.csdn.net/weixin_47025166/article/details/125876756