Second, the registration center of Eureka Spring Cloud

Foreword

Considered the official start of spring cloud knowledge learning project, probably know Springcloud is composed of a large number of micro-services, so we are now one by one to learn it.

Registration center, considered the core of the micro service. All services will be registered with the registry, requesting the service, and not directly to request a service address, destination address but first and then go through the registration center. While Eureka has stopped maintenance, but we do use is no problem.

Eureka main service registry, service providers and consumer services. Many times the service consumer and a service provider. So in terms of Eureka, Eureka is divided into server and client Eureka, it is the registry, the client is the service provider and consumer services end.

Stand-alone mode

Well, we have come up with a Eureka's first server-side bar, there are stand-alone mode the server and cluster mode, let's stand-alone mode.

More on the article said, we use maven modular development, we create a parent maven projects, pom.xml document reads as follows:

<?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">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>cn.quellanan</groupId>
    <artifactId>SpringCloud</artifactId>
    <version>1.0.0</version>
    <packaging>pom</packaging>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    
    <modules>
        <module>eureka-server-8000</module>
        <module>eureka-server-8001</module>
        <module>eureka-server-8002</module>
        <module>zlflovemm</module>
    </modules>

</project>

You can see the file specified in the basic version dependent spring boot and Spring cloud, etc., thus ensuring that each module version consistency.

Sub-module

Next we create a eureka-server-8000 sub-modules.

pom.xml

pom.xml reads as follows:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>cn.quellanan</groupId>
        <artifactId>SpringCloud</artifactId>
        <version>1.0.0</version>
    </parent>
    <groupId>com.quellanan.springcloud</groupId>
    <artifactId>eureka-server-8000</artifactId>
    <version>1.0.0</version>
    <name>eureka-server-8000</name>
    <description>eureka project for Spring Boot</description>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

</project>

We can see inherits the parent pom, additional increased reliance spring-cloud-starter-netflix-eureka-server's.

@EnableEurekaServer

Increase @EnableEurekaServer annotation startup class, represents Eureka enabled server.

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

application.properties

Configuration file to add the following configuration

spring.application.name=spring-cloud-eureka-server-8000
server.port=8000

#表示是否将自己注册到Eureka Server,默认为true。
eureka.client.register-with-eureka=true

# 表示是否从Eureka Server获取注册信息,默认为true。
eureka.client.fetch-registry=true

#设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。多个地址可使用 , 分隔。
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/

Now we can start the project and see
Here Insert Picture Description
can see that we will register itself with the service.

Eureka client

Said earlier, service providers and service consumers are clients, in fact, some of our specific business projects. So we'll create a sub-module. I'm here to separate it, we are creating a service provider and service consumer.

service providers

We create a eureka-client-provider sub-module, pom file the introduction of spring-cloud-starter-netflix-eureka-client.

    <parent>
        <groupId>cn.quellanan</groupId>
        <artifactId>SpringCloud</artifactId>
        <version>1.0.0</version>
    </parent>
    groupId>com.quellanan.springcloud</groupId>
    <artifactId>eureka-client-provider</artifactId>
    <version>1.0.0</version>
    <name>eureka-client-provider</name>
    <description>eureka-client-provider 服务提供者</description>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

Start adding @EnableEurekaClient class notes or annotations can be @EnableDiscoveryClient.

@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaClientProviderApplication.class, args);
    }

}

increase follows application.properties

server.port=9000
#服务名,在注册时所用
spring.application.name=eureka-client-provider
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

Here the address specified eureka service center is 8000. As the configuration can be registered to the service registry friends.
We write a test interface.
IndexController create a class, as follows:

@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String hello(){
        return "hello world ";
    }
}

Consumer Services

We like to create a eureka-client-consumer module. pom file as follows:

<parent>
        <groupId>cn.quellanan</groupId>
        <artifactId>SpringCloud</artifactId>
        <version>1.0.0</version>
    </parent>

    <groupId>com.quellanan.springcloud</groupId>
    <artifactId>eureka-client-consumer</artifactId>
    <version>1.0.0</version>
    <name>eureka-client-consumer</name>
    <description>eureka-client-consumer 服务消费者</description>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    </dependencies>

In relation to a service provider, we added Feign rely mainly used to discover services and client load balancing to achieve, we are here to discover services on it.

In the startup class @EnableDiscoveryClient for discovering services, and inject the bean instance RestTemplate used to interface to the service provided by the call. @LoadBalanced is on the client load balancing, the beginning I did not add this comment, but found that without it, the service consumer will not be able to get through the service name Examples of available service providers. So here you can try it out.

@EnableDiscoveryClient
@SpringBootApplication
public class EurekaClientConsumerApplication {
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
    public static void main(String[] args) {
        SpringApplication.run(EurekaClientConsumerApplication.class, args);
    }
}

We took write an interface, call the service consumers, we create a IndexController, reads as follows:

@RestController
public class IndexController {
    private static final String applicationName = "eureka-client-provider";
    @Autowired
    private RestTemplate restTemplate;
    @RequestMapping("/index")
    public String getHello(){
        String url = "http://"+ applicationName +"/hello";
        return  restTemplate.getForObject(url,String.class);
    }
}

Here we can see the service name applicationName is the service provider. In practice, one type of service may have several servers, physical address and ip may be different, but they guarantee the same service name on it, so consumers can get a list of services available through the service name, and then through a complex balancing policy to select one instance access.

Finally, we add the following configuration in the application:

server.port=9001
#服务名,在注册时所用
spring.application.name=eureka-client-consumer
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

test

Now we start the registration center and client look at these two projects. After the start, we input

http://localhost:8000

Here Insert Picture Description
You may find that our clients have been registered with the registry friends. Service providers and service consumers are already registered to the registry friends. Let's try to tune the interface. We enter the following:

http://localhost:9001/index

Here Insert Picture Description
可以看到其实获取了9000服务提供者的接口。这样就实现了服务的注册和发现啦,并实现远程调用。

集群模式(高可用)

上面我们我们搭建的注册中心只是单机模式,只有一个Eureka 服务端,单实际应用中注册中心其实是尤为重要的,所以就需要搭建集群环境,其实Eureka 对集群模式是天然的支持的,我们搭建也非常简单。
为什么这么说呢,我们前面可以看到只要配置了eureka.client.serviceUrl.defaultZone 就就会被对应的注册中线检测到,所以我们代码完全一样,只需要将eureka.client.serviceUrl.defaultZone相互指引就可以了,就就可以简单的搭建一个高可用的环境。
下面我们来搭建一个,因为我们就一台服务器,所以就用不同的端口,其实代码完全一样的,只是配置文件中配置不一样,我们分别把三个分配置文件贴出来。
8000端口的

spring.application.name=spring-cloud-eureka-server-8000
server.port=8000

#表示是否将自己注册到Eureka Server,默认为true。
eureka.client.register-with-eureka=true

# 表示是否从Eureka Server获取注册信息,默认为true。
eureka.client.fetch-registry=true

#设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。多个地址可使用 , 分隔。
eureka.client.serviceUrl.defaultZone=http://localhost:8001/eureka/,http://localhost:8002/eureka/

8001端口:

spring.application.name=spring-cloud-eureka-server-8001
server.port=8001
#表示是否将自己注册到Eureka Server,默认为true。
eureka.client.register-with-eureka=true
# 表示是否从Eureka Server获取注册信息,默认为true。
eureka.client.fetch-registry=true
#设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。多个地址可使用 , 分隔。
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/,http://localhost:8002/eureka/

8002 端口

spring.application.name=spring-cloud-eureka-server-8002
server.port=8002
#表示是否将自己注册到Eureka Server,默认为true。
eureka.client.register-with-eureka=true
# 表示是否从Eureka Server获取注册信息,默认为true。
eureka.client.fetch-registry=true
#设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。多个地址可使用 , 分隔。
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/,http://localhost:8001/eureka/

我们现在分别启动这个这三个配置文件,你们可以用profile 来指向,我这为了分明,直接创建了三个模块。启动后,我们分别访问

http://localhost:8000/
http://localhost:8001/
http://localhost:8002/

Here Insert Picture Description
这里可以看到其实已经相互监控了。我们了解一下这两个配置参数。

#定义服务续约任务的调用时间间隔,默认30s
eureka.instance.lease-renewal-interval-in-seconds=30
#定义服务失效的时间默认90s
eureka.instance.lease-expiration-duration-in-seconds=90

我们现在再将我们的服务提供者和服务消费者注册进来,但是这里,需要修改的地方也是eureka.client.serviceUrl.defaultZone。将服务注册到集群中。

eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/,http://localhost:8001/eureka/,http://localhost:8002/eureka/

然后启动项目可,可以看到注册到了注册中心,并且可以调用服务提供者提供的接口。
Here Insert Picture Description

总结

最后画了一张图来说明整个注册中心的架构图。
Here Insert Picture Description

可以看到注册服务端可以是一个集群。相互注册监控。服务消费者和服务提供者都是服务客户端,都会将服务注册到服务中心,同时这些服务也都可以使是集群或者分布式的。服务提供者会从服务端获取服务提供者可用的服务实例列表,通过负载均衡策略选择其中某一实例进行调用。这个算是Eureka 的总结吧哈哈

番外

Well, always is finished, this article really is my card for several days, some place to write is not very good, we welcome the guidance.
Code is uploaded to GitHub:
https://github.com/QuellanAn/SpringCloud

Come follow ♡

Welcome to the personal public concern number "Programmers love yogurt"

Share a variety of learning materials including java, linux, big data. Information includes video documentation and source code, while sharing themselves and delivery of high-quality technology blog.

If you like attention and remember to share yo ❤

file

Guess you like

Origin www.cnblogs.com/quellanan/p/12173441.html