Getting Started with spring cloud Series 2: Using Eureka for Service Governance "Suggested Collections"

Hello, everyone. See you again. I'm Quan stack Jun.

Service governance is arguably the most core and fundamental module in the micro-service architecture. It mainly implements the automatic registration and discovery of individual micro-service instances.

Spring Cloud Eureka is part of the Spring Cloud Netflix microservice suite and is responsible for completing the service governance functions in the microservice architecture.

This article uses a simple example to share how to use Eureka for service governance:

  • Set up Service Registration Center
  • Register Service Provider
  • Service Discovery and Consumption

===========I am a gorgeous dividing line======================

1. Set up a service registration center

First list the full directory structure:

The construction process is as follows:

  1. Create maven project: Eureka (implementation omitted)
  2. Modify the pom file to introduce dependencies
<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>
    <groupId>com.sam</groupId>
    <artifactId>eureka</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.1.RELEASE</version>
    </parent>

    <properties>
        <javaVersion>1.8</javaVersion>
    </properties>
    <!-- Use dependencyManagement Version management -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Camden.SR6</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>

    </dependencyManagement>

    <dependencies>
        <!-- Introduce eureka server rely on -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>

    </dependencies>

</project>
 
copy

  1. Create Startup Class
/**
 * 
 * @EnableEurekaServer
 * Service registry used to specify this project as Eureka
 */
@EnableEurekaServer
@SpringBootApplication
public class EurekaApp {

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

 
copy

  1. Configure application.properties file
#Setting tomcat service port number
server.port=1111
#Set Service Name
spring.application.name=eureka-service

eureka.instance.hostname=localhost
#The registry does not need to register itself
eureka.client.register-with-eureka=false
#Registry does not need to discover services
eureka.client.fetch-registry=false
#Set the URL of the Service Registry
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka
copy

  1. Start the service and visit it. We'll see this:

2. Registering Service Providers

List the complete directory structure first:

The construction process is as follows:

  1. Create maven project: hello-service (implementation omitted)
  2. Modify the pom file to introduce dependencies

 <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>
    <groupId>com.sam</groupId>
    <artifactId>hello-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.1.RELEASE</version>
    </parent>

    <properties>
        <javaVersion>1.8</javaVersion>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Camden.SR6</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>

    </dependencyManagement>

    <dependencies>
        <!-- Introduce eureka Client Dependency -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>

    </dependencies>
</project>
 
copy

  1. Create Startup Class

/***
 * 
 * @EnableDiscoveryClient
 * Let services use eureka servers
 * Implement service registration and discovery
 *
 */
@EnableDiscoveryClient
@SpringBootApplication
public class HelloApp {

    public static void main(String[] args) {

        SpringApplication.run(HelloApp.class, args);
    }

}
 
copy

  1. Create controller

@RestController
public class HelloController {

    Logger logger = LoggerFactory.getLogger(HelloController.class);

    @Autowired
    DiscoveryClient discoveryClient;
    
    @RequestMapping("/hello")
    public String hello() {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        //Service id of print service
        logger.info("*********" + instance.getServiceId());
        return "hello,this is hello-service";
    }
}
 
copy

  1. Configure application.properties file
server.port=9090
#Set Service Name
spring.application.name=hello-service
#Set the URL of the service registry to which this service will register itself
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka
 
 
copy

Start and test

  1. ) After booting, the console of hello-service will read like this (xxx stands for your PC name)
Registered instance HELLO-SERVICE/xxx:hello-service:9090 with status UP (replication=false)
copy

The console of eureka prints the following (xxx stands for your PC name)

Registered instance HELLO-SERVICE/xxx:hello-service:9090 with status UP (replication=false)
copy
  1. ) Visit localhost:1111 again, and you will find that the service is registered with the registry

3. Service Discovery and Consumption

The complete directory structure is as follows:

Setup process:

  1. Create maven project (implementation omitted)
  2. Modify the pom file to introduce dependencies
<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>
    <groupId>com.sam</groupId>
    <artifactId>hello-consumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.1.RELEASE</version>
    </parent>

    <properties>
        <javaVersion>1.8</javaVersion>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Camden.SR6</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>

    </dependencyManagement>

    <dependencies>
        <!-- Introduce eureka Client Dependency -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <!-- Introduce ribbon Dependency, to achieve load balancing, we're just using it here, let's not introduce it yet-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
        </dependency>

    </dependencies>
</project>

This is more than hello-service Service providers, much more ribbon Dependency on
 
 
copy

Create Startup Class

@EnableDiscoveryClient
@SpringBootApplication
public class ConsumerApp {


    //@Bean is used on methods to set method return value to bean
    @Bean
    @LoadBalanced  //@LoadBalanced for load balancing
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
    
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApp.class, args);
    }
}

  
 
 
copy
This is also used here@EnableDiscoveryClient, Let the service use eureka Server for service registration and discovery
copy

  1. Create controller
@RestController
public class ConsumerController {

    //The restTemplate injected here is at com. Instances configured with @Bean in sam.ConsumerApp
    @Autowired
    RestTemplate restTemplate;

    @RequestMapping("/hello-consumer")
    public String helloConsumer() {
        //Call the hello-service service, note that the service name is used here, not the specific ip+port
        restTemplate.getForObject("http://hello-service/hello", String.class);
        return "hello consumer finish !!!";
    }
}
 
 
copy

  1. Configure application.properties file
server.port=9999

spring.application.name=hello-consumer
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka


#The configuration project here is the same as the service provider hello-service
 
 
copy

  1. Start, test
    1. ) Start eureka. To show a balanced effect, our hello-service starts two services, starting two services as follows

      The above is the start-up step of hello-service 1 with port number 9090; Similarly, set hello-service 2 with port number 9091 (implementation omitted).

    2. ) Start hello-consumer
    3. ) Visit again http://localhost:1111/ , you will find two hello-service services (port number one is 9090, one is 9091), and one hello-consume service
    4. ) Multiple visits http://localhost:9999/hello-consumer, you will find that hello-service 1 and hello-service 2 are invoked in turn (responsible for balance has been achieved) and can be confirmed by the console print content for both (remember that we have loggerlogger.info in the hello-service controller ("*******"+ instance.getServiceId()); Are you? Yes, that's the print)

4. Summary

The above example implements basic service governance:

  • Implement Service Registry via spring-cloud-starter-eureka-server and @EnableEurekaServer
  • Use and register with the service registry via spring-cloud-starter-eureka and @EnableDiscoveryClient
  • Use the registry and discover services through spring-cloud-starter-eureka and @EnableDiscoveryClient, and load-balanced consumer services through spring-cloud-starter-ribbon

PS: The IDE I use here is Spring Tool Suite, which is a customized version of eclipse for spring. It is convenient for us to use spring for development. Interested friends can learn about it by themselves in Baidu.

Publisher: Full stack programmer stack length, please specify the source: https://javaforall.cn/120909.html Original Link: https://javaforall.cn

Posted by hhheng on Mon, 18 Jul 2022 16:34:14 +0930