Spring-Cloud-Eureka實例


SpringCloud實現服務注冊中心

注冊中心這么關鍵的服務,如果是單點話,遇到故障就是毀滅性的。在一個分布式系統中,服務注冊中心是最重要的基礎部分,理應隨時處於可以提供服務的狀態。為了維持其可用性,使用集群是很好的解決方案。Eureka通過互相注冊的方式來實現高可用的部署,所以我們只需要將Eureke Server配置其他可用的serviceUrl就能實現高可用部署。

實例結構如下圖所示,服務中心雙機部署(模仿集群)。

 

 

雙節點注冊中心

1、創建application-peer1.properties,作為peer1服務中心的配置,並將serviceUrl指向peer2

spring.application.name=spring-cloud-eureka
server.port=8000
eureka.instance.hostname=peer1

eureka.client.serviceUrl.defaultZone=http://peer2:8001/eureka/

  

2、創建application-peer2.properties,作為peer2服務中心的配置,並將serviceUrl指向peer1

spring.application.name=spring-cloud-eureka
server.port=8001
eureka.instance.hostname=peer2

eureka.client.serviceUrl.defaultZone=http://peer1:8000/eureka/

  

3、host轉換

在hosts文件中加入如下配置

127.0.0.1 peer1  
127.0.0.1 peer2  

  

4、打包啟動

依次執行下面命令

#打包
mvn clean package
# 分別以peer1和peeer2 配置信息啟動eureka
java -jar spring-cloud-eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1
java -jar spring-cloud-eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2

  依次啟動完成后,瀏覽器輸入:http://localhost:8000/ 效果圖如下:

 

 

 

 

根據圖可以看出peer1的注冊中心DS Replicas已經有了peer2的相關配置信息,並且出現在available-replicas中。我們手動停止peer2來觀察,發現peer2就會移動到unavailable-replicas一欄中,表示peer2不可用。

到此雙節點的配置已經完成。

 

服務生產方

spring-cloud-producer

pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ln</groupId>
    <artifactId>spring-cloud-producer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-cloud-producer</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.M3</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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </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>

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

</project>

  

application.properties

spring.application.name=spring-cloud-producer
server.port=9000
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

啟動類

package com.ln.springcloudproducer;

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

@SpringBootApplication
@EnableDiscoveryClient //啟動類增加EnableDiscoveryClient注解,項目就具有了服務注冊的功能
public class SpringCloudProducerApplication {

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

}

 

controller

package com.ln.springcloudproducer.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author linan
 * @date 2019/10/2311:15
 */
@RestController
public class UserController {

    @RequestMapping("/hello")
    public String hello(@RequestParam String name){
        return "hello"+name;
    }
}

  

添加@EnableDiscoveryClient注解后,項目就具有了服務注冊的功能。啟動工程后,就可以在注冊中心的頁面看到SPRING-CLOUD-PRODUCER服務。

 

 

 

服務調用方
spring-cloud-consumer
application.properties
spring.application.name=spring-cloud-consumer
server.port=9001
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

  


pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ln</groupId>
    <artifactId>spring-cloud-consumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-cloud-consumer</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.M3</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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--springboot2.2需要引入此包,使用遠程調用-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-openfeign-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </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>

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

</project>

注意:

  <!--springboot2.2需要引入此包,使用遠程調用-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-openfeign-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

啟動類
package com.ln.springcloudconsumer;

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

@SpringBootApplication
@EnableDiscoveryClient //啟用服務注冊與發現
@EnableFeignClients  //啟用feign進行遠程調用
public class SpringCloudConsumerApplication {

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

}

  

  • @EnableDiscoveryClient :啟用服務注冊與發現
  • @EnableFeignClients:啟用feign進行遠程調用
  • Feign是一個聲明式Web Service客戶端。使用Feign能讓編寫Web Service客戶端更加簡單, 它的使用方法是定義一個接口,然后在上面添加注解,同時也支持JAX-RS標准的注解。Feign也支持可拔插式的編碼器和解碼器。Spring Cloud對Feign進行了封裝,使其支持了Spring MVC標准注解和HttpMessageConverters。Feign可以與Eureka和Ribbon組合使用以支持負載均衡。

feign調用實現

package com.ln.springcloudconsumer.controller;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author linan
 * @date 2019/10/2314:30
 */
@FeignClient(name="spring-cloud-producer") //name為遠程調用服務名字
public interface HelloRemote {
    @RequestMapping(value = "/hello")
    public String hello(@RequestParam String name);
}

 

 

controller

package com.ln.springcloudconsumer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author linan
 * @date 2019/10/2314:33
 */
@RestController
public class HelloController {
    @Autowired
    HelloRemote helloRemote;

    @RequestMapping("/test")
    public String hello(@RequestParam String name){
        return helloRemote.hello(name);
    }
}

  

測試:

簡單調用

依次啟動spring-cloud-eureka、spring-cloud-producer、spring-cloud-consumer三個項目

先輸入:http://localhost:9000/hello?name=zhagnsan 檢查spring-cloud-producer服務是否正常

返回:hellozhagnsan

說明spring-cloud-producer正常啟動,提供的服務也正常。

瀏覽器中輸入:http://localhost:9001/test?name=zhagnsan 

返回:hellozhagnsan

說明客戶端已經成功的通過feign調用了遠程服務hello,並且將結果返回到了瀏覽器。

 

 

參考:https://www.cnblogs.com/ityouknow/p/6859802.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM