1. 程式人生 > 其它 >服務註冊中心的簡單介紹和使用

服務註冊中心的簡單介紹和使用

服務註冊中心一般原理說明:

  1. 服務提供者啟動
  2. 服務提供者將相關服務資訊主動註冊到註冊中心
  3. 服務消費者採用poll模式主動拉取(定時拉取)可用的服務提供者清單
  4. 服務消費者也可以直接呼叫服務提供者

註冊中心需要完成服務提供者的健康監控,當發現服務提供者失效時需要及時剔除

 註冊中心對比

C:資料一致性

A:高可用性

P:分割槽容錯性(必須滿足的)

服務註冊中心元件eureka

eureka基礎架構

 

eureka的服務端:eureka註冊中心

eureka的客戶端:服務消費者,服務提供者

 

eureka專案啟動出現如下問題

Caused by: java.lang.NoClassDefFoundError: org/springframework/cloud/context/environment/EnvironmentChangeEvent

子專案中缺少如下

使用https://start.spring.io/actuator/info,找到springboot和springcloud對應的版本

 

 eureka配置叢集方案

第一步修改本地電腦host

127.0.0.1 LagouCloudEurekaServerA
127.0.0.1 LagouCloudEurekaServerB

第二步

 

#eureka server服務埠號
server.port=8761

#應用名稱
spring.application.name=eureka-service

#當前eureka的例項名
eureka.instance.hostname=LagouCloudEurekaServerA
#配置eureka客戶端互動的地址
eureka.client.service-url.defaultZone=http://LagouCloudEurekaServerB:8762/eureka
#是否註冊到eureka
eureka.client.register-with-eureka=true
#不需要獲取服務提供者資訊
eureka.client.fetch-registry=true

spring.main.allow-bean-definition-overriding=true


logging.level.com.eureka=info
logging.level.web=info
spring.devtools.add-properties=false



#eureka server服務埠號
server.port=8762

#應用名稱
spring.application.name=first-eureka

#當前eureka的例項名
eureka.instance.hostname=LagouCloudEurekaServerB
#配置eureka客戶端互動的地址
eureka.client.service-url.defaultZone=http://LagouCloudEurekaServerA:8761/eureka
#是否註冊到eureka
eureka.client.register-with-eureka=true
#不需要獲取服務提供者資訊
eureka.client.fetch-registry=true

spring.main.allow-bean-definition-overriding=true


logging.level.com.eureka=info
logging.level.web=info
spring.devtools.add-properties=false

  

 

 服務提供者叢集

server.port=1000

spring.application.name=first-provider
#註冊到eureka註冊中心,如果是註冊到叢集就用逗號連線多個,單例項寫上一個就好
eureka.client.service-url.defaultZone=http://LagouCloudEurekaServerB:8762/eureka,http://LagouCloudEurekaServerA:8761/eureka


logging.level.first.provider=debug
logging.level.web=debug
spring.devtools.add-properties=false


server.port=1001

spring.application.name=first-provider
#註冊到eureka註冊中心,如果是註冊到叢集就用逗號連線多個,單例項寫上一個就好
eureka.client.service-url.defaultZone=http://LagouCloudEurekaServerB:8762/eureka,http://LagouCloudEurekaServerA:8761/eureka


logging.level.second.provider=debug
logging.level.web=debug
spring.devtools.add-properties=false

 建立消費者

server.port=2000

spring.application.name=one-consumer
#註冊到eureka註冊中心,如果是註冊到叢集就用逗號連線多個,單例項寫上一個就好
eureka.client.service-url.defaultZone=http://LagouCloudEurekaServerB:8762/eureka,http://LagouCloudEurekaServerA:8761/eureka


logging.level.one.consumer=debug
logging.level.web=debug
spring.devtools.add-properties=false

  

 <dependencies>
    <!--客戶端-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <!--web依賴-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    </dependencies>

package one.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * @author yourheart
 * @Description
 * @create 2022-04-04 14:40
 */
@SpringBootApplication
@EnableDiscoveryClient
public class OneConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(OneConsumerApplication.class,args);
    }
}


package one.consumer.config;

import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

/**
 * @author yourheart
 * @Description
 * @create 2022-04-04 15:05
 */
@Component
public class RestTemplateConfig {

    @Bean
    public RestTemplate getRestTemplste(){
        return new RestTemplate();
    }
}


package one.consumer.controller.front;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

/**
 * @author yourheart
 * @Description
 * @create 2022-04-05 22:38
 */
@Controller
public class ConsumerController {

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/queryIpCity/{ip}")
    @ResponseBody
    public String queryIpCity(@PathVariable String ip){

        String forObject = restTemplate.getForObject("http://localhost:1000/queryIpCity/" + ip, String.class);


        return "消費呼叫返回結果:"+forObject;
    }
}

消費者從註冊中心獲取例項

 

 

package one.consumer.controller.front;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author yourheart
 * @Description
 * @create 2022-04-05 22:38
 */
@Controller
public class ConsumerController {


    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private RestTemplate restTemplate;



    /**
     * 從註冊中心獲取服務例項
     * @param ip
     * @return
     */
    @RequestMapping("/queryIpCityFromEureka/{ip}")
    @ResponseBody
    public String queryIpCityFromEureka(@PathVariable String ip){

        List<ServiceInstance> instances = discoveryClient.getInstances("FIRST-PROVIDER");
        ServiceInstance instance = instances.get(0);
        String host = instance.getHost();
        int port = instance.getPort();
        String url="http://"+host+":"+port+"/queryIpCity/" + ip;

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


        return "從註冊中心獲取服務例項,消費呼叫返回結果:"+forObject;
    }
}

  父類依賴

<?xml version="1.0" encoding="UTF-8"?>
<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>pingan.parent</groupId>
    <artifactId>pingan-parent</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>eureka-service</module>
        <module>first-eureka</module>
        <module>first-provider</module>
        <module>second-provider</module>
        <module>one-consumer</module>
    </modules>

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

    <!--這裡是為了統一控制版本-->
    <dependencyManagement>
        <dependencies>
            <!--引入springcloud版本-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR12</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>



        </dependencies>
    </dependencyManagement>


</project>

  伺服器提供者

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>pingan-parent</artifactId>
        <groupId>pingan.parent</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>first.provider</groupId>
    <artifactId>first-provider</artifactId>

    <dependencies>
        <!--客戶端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!--web依賴-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>


</project>
server.port=1000

spring.application.name=first-provider
#註冊到eureka註冊中心,如果是註冊到叢集就用逗號連線多個,單例項寫上一個就好
eureka.client.service-url.defaultZone=http://LagouCloudEurekaServerB:8762/eureka,http://LagouCloudEurekaServerA:8761/eureka


logging.level.first.provider=debug
logging.level.web=debug
spring.devtools.add-properties=false


package first.provider;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
 * @author yourheart
 * @Description
 * @create 2022-04-02 23:05
 */
@SpringBootApplication
@EnableDiscoveryClient
public class FirstProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(FirstProviderApplication.class,args);
    }
}


package first.provider.controller.front;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author yourheart
 * @Description
 * @create 2022-04-02 23:08
 */
@Controller
public class FirstController {

    @RequestMapping("/queryIpCity/{ip}")
    @ResponseBody
    public String queryIpCity(@PathVariable String ip){
        return ip;
    }
}

  

 

 

以上就是註冊中心叢集模式,服務提供者叢集模式搭建,消費者從註冊中心獲取服務提供者