4 Eureka服務註冊

4.1 微服務註冊中心

4.1.1註冊中心的主要作用

 

 

4.1.2 常見的註冊中心

 

4.2 Eureka概述

4.2.1Eureka組成

 4.2.2 Eureka的交互流程與原理

Eureka使用流程

4.3使用Eureka步驟

4.3.1搭建eureka server

1 創建工程

2 導入pom依賴

<?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>org.example</groupId>
    <artifactId>spring_cloud_demo</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>product_service</module>
        <module>order_service</module>
        <module>eureka_server</module>
    </modules>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

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

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


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

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.RELEASE</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>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>


</project>

1.3 配置application.properties

server.port=9000

eureka.instance.hostname=localhost
#是否將自己註冊到註冊中心
eureka.client.register-with-eureka=false
#是否從eureka中獲取註冊信息
eureka.client.fetch-registry=false
#配置暴露給Eureka Client請求的地址
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

1.4  配置啓動類

 

package xx.study.sc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {



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

4.3.2創建Eureka生產者

1 引入EurekaClient依賴

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

2修改application.properties 添加EurekaServer信息


server.port=9001
#服務名稱
spring.application.name=service-product
#註冊中心訪問地址
eureka.client.service-url.defaultZone=http://localhost:9000/eureka/
#使用ip地址註冊
eureka.instance.prefer-ip-address=true

3修改啓動類 添加服務發現的支持(可選)

package xx.study.sc;

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

@SpringBootApplication
//激活EurekaClient 最新版本可以不寫
@EnableEurekaClient
public class ProductApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProductApplication.class);
    }
}

 4產品

package xx.study.sc.controller;

import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/product")
public class ProductController {
    @RequestMapping(value = "/{name}",method = RequestMethod.GET)
    public String  say(@PathVariable String name){
        String returnVal="歡迎"+name+"學習Spring Cloud!!!";
        return returnVal;
    }
    @RequestMapping(value = "/buy",method = RequestMethod.GET)
    public String  buy(@RequestParam String name){
        String returnVal=name+Math.random();
        return returnVal;
    }
}

4.3.3創建Eureka消費者

Eureka中的元數據:服務的主機名,ip等信息,可以通過EurekaServer進行獲取,用於服務之間的調用。

1 引入EurekaClient依賴(同生產者)

2修改application.properties 添加EurekaServer信息(同生產者)

3實現消費

package xx.study.sc.controller;

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.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {
    //注入
    @Autowired
    private RestTemplate restTemplate;

    /**
     *注入DiscoverClient
     * Springcloud提供獲取元數據的工具類
     */
    @Autowired
    private DiscoveryClient discoveryClient;


    @RequestMapping(value = "/buyApple",method = RequestMethod.GET)
    public String  buyApple(@RequestParam String name){
        //調用服務名稱的所有元數據
        List<ServiceInstance> instances=discoveryClient.getInstances("service-product");
        /*for (ServiceInstance instance : instances) {
            System.out.println(instance);
        }*/
        //獲取唯一的元數據
        ServiceInstance instance=instances.get(0);


        name=restTemplate.getForObject("http://"+instance.getHost()+":"+instance.getPort()+"/product/buy?name= "+name,String.class);

        String returnVal="從註冊中心收到"+name+"!!!";
        return returnVal;
    }
    
}

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章