Use idea, Eureka as a registry to build a microservice project

Create a SpringBoot project

First, by using idea, quickly create a springboot project. When choosing a dependency, I usually choose a web dependency. Don’t worry about the SpringBoot version here, just choose it.

Change setting

It is preferred to change the version of SpringBoot, which I changed to 2.4.0 here, because it can match the version 3.0 of SpringCloud.

Import dependencies (this imported dependency is a dependency of the registry).

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

 Then add relevant configuration information to the configuration file.


server.port=8001

#eureka服务端的实例名称
eureka.instance.hostname=localhost
#false表示不向注册中心注册自己。
#eureka.client.register-with-eureka=false
#false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
#eureka.client.fetch-registry=false
#设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址。
spring.application.name=eureka
eureka.client.service-url.defaultZone=http://localhost:8001/eureka/

Then add the @EnableEurekaServer annotation to the startup class.

@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {

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

}

 Then run the project and access the Eureka client through http://localhost:8001/ to view the registered services.

create consumer

In layman's terms, it is to create a service and call other services in the registry. The first part of the overall process is similar, and there is a difference when importing dependencies.

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

The difference between it and the above is that here is eureka-client, and the above is server at the end.

Add relevant configuration in the configuration file

server.port=8081
spring.application.name=goods-service

#eureka
#true表示向注册中心注册自己。
eureka.client.register-with-eureka=true
#是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
eureka.client.fetch-registry=true
#设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址。
eureka.client.service-url.defaultZone=http://localhost:8001/eureka

Similarly, add the @EnableEurekaClient annotation to the startup class, and finally start the project.

@SpringBootApplication
@EnableEurekaClient
public class GoodsApplication {

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

}

Check whether the service registration is successful by visiting http://localhost:8001/ .

Guess you like

Origin blog.csdn.net/qq_50801874/article/details/130543488