SpringCloud Study Notes (1): Eureka registry

Brief introduction

Eureka is based on open source Netflix service governance programs rest is divided into registered and Client-Server side, Server-side for the registry, other micro-service service connection through Client Server-side end and discovery.

Project Introduction

  1. sc-parent, the parent module
  2. sc-provider, the provider module
  3. sc-eureka, registry
  4. sc-consumer-discovery, the consumer module

Building the parent module

Create a parent module sc-parent, pom.xml:

<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.cf</groupId>
  <artifactId>sc-parent</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
   
   <!-- 继承Spring Boot的默认值 -->
   <parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <version>2.1.6.RELEASE</version>
       <relativePath /> <!-- lookup parent from repository -->
   </parent>
   
   <properties>
    <!-- 降低maven-jar-plugin插件版本,防止版本不匹配导致的pom.xml第一行报错 -->
    <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
   </properties>
   
   <dependencies>
        <!-- 添加web应用依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
   </dependencies>
   
   <dependencyManagement>
        <dependencies>
            <!-- SpringCloud依赖 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
   </dependencyManagement>
   
   <build>
        <plugins>
               <!-- 打可执行jar包插件 -->
               <plugin>
                   <groupId>org.springframework.boot</groupId>
                   <artifactId>spring-boot-maven-plugin</artifactId>
               </plugin>
               
               <!-- 指定编译插件使用的jdk版本 -->
               <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
        </plugins>
   </build>
</project>

Registration center set up

1. Create a sub-module project sc-eureka under the parent module, pom.xml:

<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>com.cf</groupId>
    <artifactId>sc-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>sc-eureka</artifactId>
  
  <dependencies>
    <!-- Eureka服务端依赖 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
  </dependencies>
</project>

2. Create a startup class eureka.EurekaApplication:

package eureka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

//声明该类为SpringBoot服务的入口
@SpringBootApplication
//声明该微服务为注册中心,提供服务发现和注册的功能
@EnableEurekaServer
public class EurekaApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class, args);
    }
}

3. Create a configuration file /src/main/resources/application.yml:

server:
  port: 8080 #当前服务端口

eureka:
  instance:
    hostname: localhost #当前Eureka实例主机名
  client:
    registerWithEureka: false #表示不向注册中心注册自己
    fetchRegistry: false #表示此客户端不需要从Eureka注册中心获取Eureka注册表信息
    serviceUrl:
      defaultZone: http://localhost:8080/eureka/ ##eureka对外提供的地址(客户端连接的地址)

Other configuration information may refer to two configuration classes EurekaInstanceConfigBean and EurekaClientConfigBean

4. Run the startup class EurekaApplication, in your browser to http: // localhost: 8080 /, appears in the following figure represents a successful registration center set up:

Service providers and registration

1. Create a sub-module project sc-provider in the parent module, pom.xml:

<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>com.cf</groupId>
    <artifactId>sc-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>sc-provider</artifactId>
  
  <dependencies>
    <!-- Eureka客户端依赖,用于连接服务端进行服务注册和发现 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
  </dependencies>
</project>

2. Create a startup class provider.ProviderApplication:

package provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ProviderApplication {

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

3.创建Controller:provider.controller.BookController

package provider.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/book")
@RestController
public class BookController {
    
    @GetMapping("/list")
    public String getBookList(){
        //模拟从service返回数据
        return "[\"Java入门到放弃\",\"C++入门到放弃\",\"Python入门到放弃\",\"C入门到放弃\"]";
    }
}

4. Create a configuration file /src/main/resources/application.yml:

server:
  port: 8081

spring:
  application:
    name: sc-provider #注册到Eureka注册中心上的服务名称,对应Eureka界面上的Application列
    
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8080/eureka/ #注册中心的访问地址
  instance:
    preferIpAddress: true #表示将自己的IP注册到Eureka注册中心。默认为false,表示将hostname注册到注册中心

5. Click Start Registry sc-eureka and providers sc-provider, the provider when activated, will own information registered with the Eureka registration center, in a browser to http: // localhost: 8080 /, provider sc -provider already registered in the registry.

Consumers and service discovery

1. Create a sub-module project sc-consumer-discovery in the parent module, pom.xml:

<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>com.cf</groupId>
    <artifactId>sc-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>sc-consumer-discovery</artifactId>
  
  <dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
  </dependencies>
</project>

2. Create a startup class consumer.ConsumerDiscoveryApplication:

package consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

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

3. Create a call to service providers Controller: consumer.controller.ConsumerDiscoveryController

package consumer.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class ConsumerDiscoveryController {
    @Autowired
    private DiscoveryClient discoveryClient;
    
    @Autowired
    private RestTemplate restTemplate;
    
    @GetMapping("/getBookList")
    public String getBookList(){
        //通过服务名获取实例信息
        List<ServiceInstance> list = discoveryClient.getInstances("sc-provider");
        if (list != null && list.size() > 0 ) {
            //调用服务,并返回服务结果
            return restTemplate.getForObject(list.get(0).getUri() + "/book/list", String.class);
        }
        return null;
    }
}

4. Create application.yml:

server:
  port: 8082

spring:
  application:
    name: sc-consumer-discovery
    
eureka:
  client:
    registerWithEureka: false #在本实例中消费者不提供服务,所以无需到注册中心注册。在实际应用中,消费者也可能是提供者。
    serviceUrl:
      defaultZone: http://localhost:8080/eureka/

5. Click Start Registry sc-eureka, provider sc-provider, consumers sc-consumer-discovery, when the consumer starts, query the list of services and network addresses available from the registry. Direct access providers and consumers to call the provider as follows:

to sum up

In traditional applications, are the providers of network address hard-coded in the code, leading provider of consumer and high coupling, when the provider network address has changed, you need to modify the configuration of consumers and republish. Eureka played a role decoupled provider registration centers registered to Eureka, consumer access provider network address from Eureka registry and make calls when the provider network address changes to the registry will be re-registered.

Guess you like

Origin www.cnblogs.com/seve/p/11502579.html