Service governance SpringCloud Eureka

Service Governance: In traditional rpc remote call, services and service dependencies, manage more complex, so the need to use the service governance, management services and the dependencies between services, service calls can be achieved, load balancing, fault tolerance, and achieve service discovery registered.

 

Service registration and discovery: in the service registration and discovery, there is a registry, when the server starts, will present information on their own servers such as communications and other services to address alias Register to the registry. The other (Consumer Services | Service Provider) to register the alias way to get to the center of the actual service address, and then implement local rpc call remotely.

 

Service Provider: refers to providing service interface

Consumer Services: refers to the use of call interface

 

Eureka by the Eureka server and Eureka client of two components, Eureka server as a service registry. It supports cluster deployment. Eureka client is a java client for handling service registration and discovery. When the application starts, Eureka clients register their service information to the server, while the server information is cached locally. The client and server interact periodic heartbeat to update the service lease and service information.

 

Registration center set up

Maven pom file dependency information

 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.1.RELEASE</version>
  </parent>
  
  <!-- 管理依赖 -->
  <dependencyManagement>
      <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Finchley.M7</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
      </dependencies>
  </dependencyManagement>
  
  <dependencies>
    <!-- SpringCloud eureka-server -->
    <dependency>
        <groupId> Org.springframework.cloud </ groupId > 
        < artifactId > the Spring-Cloud-Starter-Netflix-Eureka-Server </ artifactId > 
    </ dependency > 
  </ the Dependencies > 
  
  <-! Note: this must be added, otherwise do all kinds dependence in question -> 
  < Repositories > 
      < Repository > 
          < ID > Spring-Milestones </ ID > 
          < name > the Spring Milestones </ name > 
          < URL >https://repo.spring.io/libs-milestone</url>
          <snapshots>
              <enabled>false</enabled>
          </snapshots>
      </repository>
  </repositories>

Profiles (application.yml)

### service port number 
Server: 
  Port: 8100 
Eureka: 
  instance: 
    ### registry ip address 
    hostname: 127.0.0.1 
  Client: 
    Service-url: 
      ### Registration Center Address 
      defaultZone: http: // $ {eureka.instance } .hostname: $ {server.port} / Eureka / 
    ### because he is a registry, whether you need to register themselves to their registry (when the cluster is needed is to true)       
    the Register-with-Eureka: false 
    # ## because he is a registry, the service does not need to retrieve information 
    fetch-registry: false

Test category

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

@SpringBootApplication
@EnableEurekaServer //表示开启EurekaServer服务,开启注册中心
public class AppEureka {
    public static void main(String[] args) {
        SpringApplication.run(AppEureka.class, args);
    }
}

Build complete!

 

Registration Service Providers

Maven pom file dependency information

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.1.RELEASE</version>
  </parent>
  
  <!-- 管理依赖 -->
  <dependencyManagement>
      <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Finchley.M7</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
      </dependencies>
  </dependencyManagement>
  
  <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
  </dependencies>
  
  <!-- 注意:这里必须要添加,否则各种依赖有问题 -->
  <repositories>
      <repository>
          <id>spring-milestones</id>
          <name>Spring Milestones</name>
          <url>https://repo.spring.io/libs-milestone</url>
          <snapshots>
              <enabled>false</enabled>
          </snapshots>
      </repository>
  </repositories>

配置文件(application.yml)

###会员项目的端口号
server:
  port: 8000
###服务别名(服务注册到注册中心名称)
spring:
  application:
    name: app-hclz-member 
eureka:
  client:
    service-url:
      ###当前会员服务注册到eureka服务地址
      defaultZone: http://localhost:8100/eureka
    ###需要将我的服务注册到eureka上
    register-with-eureka: true
    ###需要检索服务
    fetch-registry: true

测试类

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

@SpringBootApplication
@EnableEurekaClient //将当前服务注册到eureka上
public class AppMember {
    public static void main(String[] args) {
        SpringApplication.run(AppMember.class, args);
    }
}

controller

@RestController
public class MemberApiController {

    @RequestMapping("/getMember")
    public String getMember() {
        return "this is member,我是会员服务,springcloud2.0版本";
    }
}

启动注册中心后启动服务提供者

搭建服务提供者注册到Eureka成功!

 

Guess you like

Origin www.cnblogs.com/tripleDemo/p/11245357.html