Service governance Eureka

* fetch : 拿来
* perfer : 先拿来
* preservation : 保护
* eviction : 驱逐
* interval : 间隔
* lease : 租用
* expiration : 到期
* duration : 期间
* renewal : 更新

ps :

One: What is the service of governance

  • Shortcoming micro Services Architecture is the most important because of the large number of micro-services lead to huge maintenance costs, service governance is to solve this problem arising
  • The role of governance is to maintain service personnel freed from artificial maintenance out by the self-maintenance services , micro-services as the service providers take the initiative to register with the service management center, service consumer inquiry service needs through service and call center management
  • Service governance in service in the micro-architecture in the heart of the technology, not just micro-services, and also services gateway, monitoring, protection, communication, because the service management center, records all micro-services, others want to manage micro-project service, you need to check these services through micro-governance Center

Two: SpringCloudEureka

  • SpringCloudEureka is further encapsulated NetFlix's Eureka, which implements the functions of governance services, SpringCloudEureka provide services and client, the server is a service registry, the client complete registration and service discovery, service and client are using java language (Eureka supports multi-language)

Three: Architecture

17149157-e2ae05f039d83c8f.png
  • Photo Pirates, easy to understand

Four: Getting Started

  • Users log in to the service registry in Eureka

(A) preparation of eureka project

1. Add eureka

  • New springboot project, check eureka
17149157-c1249383b4538746.png
  • If maven project, then add the following dependence
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.5.RELEASE</version>
</parent>
<properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
  • There is a problem need to explain, I just started using jdk12, the results will start being given
java.lang.TypeNotPresentException: Type javax.xml.bind.JAXBContext not present
  • This is because JAXB-APIa java eepart of the above jdk9 version is not in the default class path, java ee apiin the jdk still exists, it is not loaded by default, jdk9 introduced module concept, you can use the command module --add-modules java.xml.bindintroducedjaxb-api
  • Use jdk8no problem

2. Add annotations on startup class

@EnableEurekaServer
@SpringBootApplication
public class MicroserviceEurekaApplication {

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

}

3. Write the configuration file

  • application.yml
server:
  port: ${port}
spring:
  application:
    name: microservice-eureka # 指定服务名
eureka:
  client:
    register-with-eureka: true # 是否将自己注册到Eureka服务中
    fetch-registry: true # 是否从Eureka中获取注册信息
    service-url: # Eureka客户端与服务端进行交互的地址
      defaultZone: ${eureka.server}
  instance:
    prefer-ip-address: true # 将自己的ip地址注册到Eureka中
    ip-address: 127.0.0.1
    instance-id: ${spring.application.name}:${server.port} # 指定实例id
  server:
    enable-self-preservation: false # 自我保护
    eviction-interval-timer-in-ms: 60000 # 清理时间间隔(单位毫秒,默认是60*1000)

(B) write access to services

1. Add web components and eureka

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

2. Write landing function

@RestController
public class UserController {

    @GetMapping("/user/{username}")
    public Map<String,Object> login(@PathVariable String username){

        if("jim".equals(username)){
            return new HashMap<String,Object>(){{
                put("username", "jim");
                put("age", "22");
            }};
        }else if ("tom".equals(username)){
            return new HashMap<String,Object>(){{
                put("username", "tom");
                put("age", "18");
            }};
        }

        return new HashMap<String,Object>(){{
            put("msg", "用户名错误");
            put("success", false);
        }};
    }
}

3. Write the configuration file

server:
  port: ${port}

spring:
  application:
    name: microservice-user

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://127.0.0.1:6868/eureka/,http://127.0.0.1:6869/eureka/
    eureka-server-connect-timeout-seconds: 60
    eureka-server-read-timeout-seconds: 60
  instance:
    prefer-ip-address: true
    ip-address: 127.0.0.1
    instance-id: ${spring.application.name}:${server.port}
    lease-expiration-duration-in-seconds: 30
    lease-renewal-interval-in-seconds: 10

(C) to start the service

1. Start eureka

  • spring-boot:run -Dport=6868 -Deureka.server=http://localhost:6869/eureka
  • spring-boot:run -Dport=6869 -Deureka.server=http://localhost:6868/eureka

2. Start login

  • spring-boot:run -Dport=8281
  • spring-boot:run -Dport=8282

3. Browser Access

  • http://localhost:6868/
  • http://localhost:6869/
17149157-fa68c9f215964cc5.png

Reproduced in: https: //www.jianshu.com/p/7f81c0c6fe52

Guess you like

Origin blog.csdn.net/weixin_34121282/article/details/91213789