Spring Cloud is one of the most popular microservices frameworks. Many companies use it to some extent. Almost every Java development job Posting requires knowledge of Spring Cloud or Dubbo microservices framework. Spring Cloud should be a must for a Java developer.

If you’re not familiar with the Spring Cloud architecture, read it first if you’re going to learn about Spring Cloud

This article introduces Eureak to implement a simple registry. The next article will cover Eureka’s implementation of a highly available registry and security controls.

What had been

Eureka is an open source service registration and discovery component of Netflix. Service discovery can be said to be the core function of micro-service architecture. After micro-service deployment, there must be service registration and discovery capabilities, and Eureka is playing this role. If you’ve used Dubbo, you know that the service registration and discovery functions in Dubbo are implemented using ZooKeeper.

Eureka is currently in version 2.x and has officially announced that it will not be maintained and updated. But Eureka is stable enough to be a registry. Spring Cloud integrates with Eureka and is fully packaged. Convenient when we use Spring Boot development simple configuration can be used.

summary

There are three types of roles in microservices framework, namely registry, service provider and service consumer. Registry is the protagonist of today’s talk, Eureka. This article briefly explains the use of Spring Cloud Eureka, simulating the implementation of single point and high availability registry. It also briefly introduces how service providers and service consumers can use the service registration and discovery capabilities provided by Eureka.

Version Description Java: 1.8

Spring the Boot: 2.1.3. RELEASE

Spring Cloud: Finchley.SR2

The finchley. SR2 version has a different package name from the previous version, so be careful when referencing maven packages.

Implement a single point registry

Create the Eureka registry

1, reference maven package, where

<dependencyManagement> 

  <dependencies> 

    <dependency> 

      <groupId>org.springframework.cloud</groupId>  

      <artifactId>spring-cloud-dependencies</artifactId>  

      <version>Finchley.SR2</version>  

      <type>pom</type>  

      <scope>import</scope> 

    </dependency> 

  </dependencies> 

</dependencyManagement>



<! -- Latest eureka server package -->

<dependency> 

  <groupId>org.springframework.cloud</groupId>  

  <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> 

</dependency>



<! -- Monitor management -->

<dependency> 

  <groupId>org.springframework.boot</groupId>  

  <artifactId>spring-boot-starter-actuator</artifactId> 

</dependency>

Copy the code

2. Create bootstrap.yml and configure Spring Cloud parameters

spring:

  application:

    name: kite-eureka-center

  cloud:

    inetutils: ## Nic Settings

      ignoredInterfaces:  ## Ignored network card

        - docker0

        - veth.*

        - VM.*

      preferredNetworks:  ## Preferred network segment

192.168

Copy the code

3. Create application.yml and set parameters

server:

  port: 3000

eureka:

  instance:

    hostname: eureka-center

Appname: registry

  client:

    registerWithEureka: false# Single point set tofalseProhibit registering yourself

    fetchRegistry: false

    serviceUrl:

      defaultZone: http://localhost:3000/eureka

  server:

    enableSelfPreservation: false

    evictionIntervalTimerInMs: 4000

Copy the code

The difference between bootstrap.yml and application.yml:

  • Yml starts before application. Yml;
  • The bootstrap. Yml configuration application name, spring. Cloud. Config. Server. Git. Uri, some of the encryption/decryption (encryption/decryption) information;
  • The information in application. Yml overwrites the content in bootstrap.yml when the same configuration is encountered;

4. Create the application. Java startup file

@EnableEurekaServer

@SpringBootApplication

public class Application {

    public static void main(String[] args) {

        SpringApplication.run(Application.class, args);

    }

}

Copy the code

@enableEurekaserver Indicates to use the EurekaServer function, that is, to start as a registry node.

5. Run application. Java and visit http://localhost:3000 to see the UI console provided by Eureka.

Create a service provider

Next create a service provider and register with the Eureka registry created above.

1. Add maven dependencies

<dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-web</artifactId>

</dependency>

<! -- Eureka client -->

<dependency>

    <groupId>org.springframework.cloud</groupId>

    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

</dependency>

Copy the code

2. Configure application.yml

server:

  port: 3001



eureka:

  instance:

    preferIpAddress: true

  client:

    serviceUrl:

      defaultZone: http://localhost:3000/eureka  ## Register with Eureka

spring:

  application:

    name: single-provider  ## Application name, which will be used later in the consumer

Copy the code

3. Create a simple RESTful controller interface

@Slf4j

@RestController

public class ProviderController {



    @Autowired

    private DiscoveryClient discoveryClient;



    @RequestMapping(value = "/hello")

    public String hello(){

        List<String> services = discoveryClient.getServices();

        for(String s : services){

            log.info(s);

        }

        return "hello spring cloud!";

    }



    @RequestMapping(value = "/nice")

    public String nice(){

        List<String> services = discoveryClient.getServices();

        for(String s : services){

            log.info("gogogo" + s);

        }

        return "nice to meet you!";

    }



}

Copy the code

4. Create the Spring Boot class

@EnableEurekaClient

@SpringBootApplication

public class SingleProviderApplication {



    public static void main(String[] args) {

        SpringApplication.run(SingleProviderApplication.class, args);

    }

}

Copy the code

The @enableeurekaclient modifier means to register with the registry.

5. Start the project and register with the Eureka registry normally. Open the Eureka console and you will see that the service has already appeared

Create a service consumer

Now that you have a service provider, create a consumer to consume

1. Reference the Maven package

<dependency>

    <groupId>org.springframework.cloud</groupId>

    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

</dependency>

<dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-web</artifactId>

</dependency>

<dependency>

    <groupId>org.springframework.cloud</groupId>

    <artifactId>spring-cloud-starter-openfeign</artifactId>

</dependency>

<dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-actuator</artifactId>

</dependency>

Copy the code

2. Configure application.yml

server:

  port: 3002

eureka:

  client:

    serviceUrl:

DefaultZone: http://127.0.0.1:3000/eureka## Register with Eureka

  instance:

    preferIpAddress: true

spring:

  application:

    name: single-customer  

Copy the code

3. Start consuming the service interface provided by the provider. Two consumption methods are shown here, one is using RestTemplate and the other is using FeignClient, which is also open source for Netflix. And is packaged by Spring Cloud into the Spring-Cloud-starter-OpenFeign package.

Create a startup class and add annotations

@SpringBootApplication

@EnableEurekaClient

@EnableFeignClients

public class SingleCustomerApplication {



    / * *

* into the RestTemplate

* and@LoadBalancedAnnotations to request service providers with load balancing policies

* This is what Spring Ribbon provides

     * @return

* /


    @LoadBalanced

    @Bean

    public RestTemplate restTemplate(a) {

        return new RestTemplate();

    }



    public static void main(String[] args) {

        SpringApplication.run(SingleCustomerApplication.class, args);

    }

}

Copy the code

@enableeurekaclient states that this project is a Eureka client, and @enableFeignClients states that this project can use Feign.

4, Create a service interface class. This is how Feign is used. See the Spring Cloud Feign documentation for details

/ * *

 * IHelloService

* Configure the service provider: single-provider is the application.name of the service provider

* /


@FeignClient("single-provider")

public interface IHelloService {



    @RequestMapping(value = "/hello")

    String hello();



    @RequestMapping(value = "nice")

    String nice();

}

Copy the code

Value annotated by @feignClient is the appplication. Name of the service provider.

Create a Controller to call the service

@RestController

public class ConsumerController {



    @Autowired

    private RestTemplate restTemplate;



    @Autowired

    private IHelloService helloService;



    private static final String applicationName = "single-provider";



    @RequestMapping(value = "feignRequest")

    public Object feignRequest(){

        String s = helloService.nice();

        return s;

    }



    @RequestMapping(value = "commonRequest")

    public Object commonRequest(){

        String url = "http://"+ applicationName +"/hello";

        String s = restTemplate.getForObject(url,String.class);

        return s;

    }



}

Copy the code

The feignRequest method calls the service interface in the way of Feign.

The commonRequest method invokes the service interface with the method provided by the RestTemplate;

6, in the end, start the service, access to the address: http://localhost:3002/commonRequest and http://localhost:3002/feignRequest

Everybody classmates hurry up attention, praise, this year, get a few praise is too difficult! The next series of articles is coming at you crazily on the road!