Spring cloud set up registration center and service registration

Time:2020-1-12

Last article “principle of microservice registration center”, read this one! 》This paper introduces the concept and principle of the registration center, and introduces the process of using Eureka to build the center and register services to the registration center.

Contents of this article

1、 Eureka introduction2、 Set up a registration center3: Registration services

1、 Eureka introduction

Eureka is a service discovery framework developed by Netflix. Spring cloud integrates it into its sub project spring cloud Netflix to realize the service discovery function of spring cloud.

Eureka consists of two components: Eureka server and Eureka client

  • Eureka server provides service registration service. After each node is started, it will be registered in Eureka server. In this way, the information of all available service nodes will be stored in the service registry of Eureka server. The information of service nodes can be seen intuitively in the interface.
  • Eureka client is a java client, which is used to simplify the interaction with Eureka server. The client is also a built-in load balancer using round robin load algorithm.

2、 Set up a registration center

First, create a new spring boot project, name it spring cloud Eureka, and then write the code as follows.

  1. Pom.xml code
    Add the dependency of Eureka server as follows:
<dependencies>        <dependency>            <groupId>org.springframework.bootgroupId>            <artifactId>spring-boot-starterartifactId>        dependency>        <dependency>            <groupId>org.springframework.cloudgroupId>            <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>        dependency>        <dependency>            <groupId>org.springframework.bootgroupId>            <artifactId>spring-boot-starter-testartifactId>            <scope>testscope>        dependency>    dependencies>    <dependencyManagement>        <dependencies>            <dependency>                <groupId>org.springframework.cloudgroupId>                <artifactId>spring-cloud-dependenciesartifactId>                <version>Finchley.RELEASEversion>                <type>pomtype>                <scope>importscope>            dependency>        dependencies>    dependencyManagement>
  1. Start class code
    Start the class and add the annotation @ enableeurekaserver. The code is as follows:
@[email protected] class SpringCloudEurekaApplication {    public static void main(String[] args) {        SpringApplication.run(SpringCloudEurekaApplication.class, args);    }}
  1. configuration file
    Using the configuration file of YML, application.yml is configured as follows:
Server: Port: 9001 ා service port Eureka: instance: hostname: localhost client: register with Eureka: false ා whether to register Eureka itself as an application to the Eureka registry; fetch registry: false ා when it is true, it can be started, but an exception is reported: cannot execute request on any known server serviceurl:  defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

Configuration Item Description:

1. Server. Port = 9001 means to set the port number of the service registration center. 2. Eureka. Instance. Hostname = localhost means to set the hostname of the service registration center. 3. Eureka. Client. Register with Eureka = false. Because the application we are currently creating is a service registration center, not an ordinary application. By default, this application will register itself with the registry (itself). Setting it to false means that this default behavior is prohibited. 4. Eureka. Client. Fetch registry = false means that other services will not be retrieved, because the service registry itself is responsible for maintaining service instances, and it does not need to retrieve other services
  1. Run screenshots
    Open the browser and visit http: / / localhost: 9001 /, you can see the registry and startup. The running screenshot is as follows:


Screenshot of registry operation

3: Registration services

After the service registry is available, we can register a service provider with the service registry, create a new spring boot project, name spring cloud user service, provide user services, and then write the code according to the following steps.

  1. Pom.xml code
    Add the dependency of Eureka server as follows:
<dependencies>    <dependency>        <groupId>org.springframework.bootgroupId>        <artifactId>spring-boot-starter-webartifactId>    dependency>    <dependency>        <groupId>org.springframework.bootgroupId>        <artifactId>spring-boot-starter-testartifactId>        <scope>testscope>    dependency>    <dependency>        <groupId>org.springframework.cloudgroupId>        <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>    dependency>dependencies><dependencyManagement>    <dependencies>        <dependency>            <groupId>org.springframework.cloudgroupId>            <artifactId>spring-cloud-dependenciesartifactId>            <version>Finchley.RELEASEversion>            <type>pomtype>            <scope>importscope>        dependency>    dependencies>dependencyManagement>
  1. Start class code
    Start the class and add the annotation @ enableeurekaclient. The code is as follows:
@[email protected] class UserServiceDemoApplication {    public static void main(String[] args) {        SpringApplication.run(UserServiceDemoApplication.class, args);    }}
  1. configuration file
    Using the configuration file of YML, application.yml is configured as follows:
Server: Port: 8081 service port Eureka: client: serviceurl: defaultzone: http: // localhost: 9001 / Eureka / spring: application: Name: User Service
  1. Run screenshots

Open the browser to visit http: / / localhost: 9001 /. Compared with the above figure, you can see that the user service has been registered in the registry. The screenshot is as follows:


Screenshot of registration center after registering service

This spring cloud service registration center and service registration have been fully implemented. If you have any questions, please leave a message!

Full source address:https://github.com/suisui2019/springboot-study

Recommended reading
1. Spring boot integrates ActiveMQ. This is enough!
2. Don’t hit the log in Java code, this is the right posture!
3. Lombok, the coding artifact, can improve the development efficiency by at least one time after learning it!
4. Using spring boot + zxing to generate QR code can be so simple
5. Profile of spring boot: quick handling of multi environment use and switching


Collect free Java related materials in limited time, covering Java, redis, mongodb, mysql, zookeeper, spring cloud, Dubbo / Kafka, Hadoop, HBase, Flink and other high concurrent distributed, big data, machine learning and other technologies.
Pay attention to the public numbers below for free.

Java碎碎念公众号Java read the public address.