基於Eureka的服務治理

代碼地址以下:
http://www.demodashi.com/demo/11927.htmlhtml

1、服務的註冊與發現

1. 服務註冊於發現.png

關係調用說明:java

  • 服務生產者啓動時,向服務註冊中心註冊本身提供的服務
  • 服務消費者啓動時,在服務註冊中心訂閱本身所須要的服務
  • 註冊中心返回服務提供者的地址信息個消費者
  • 消費者從提供者中調用服務

2、Eureka簡介

Eureka是Spring Cloud Netflix微服務套件中的一部分,能夠與Springboot構建的微服務很容易的整合起來。
Eureka包含了服務器端和客戶端組件。服務器端,也被稱做是服務註冊中心,用於提供服務的註冊與發現。Eureka支持高可用的配置,當集羣中有分片出現故障時,Eureka就會轉入自動保護模式,它容許分片故障期間繼續提供服務的發現和註冊,當故障分片恢復正常時,集羣中其餘分片會把他們的狀態再次同步回來。
客戶端組件包含服務消費者與服務生產者。在應用程序運行時,Eureka客戶端向註冊中心註冊自身提供的服務並週期性的發送心跳來更新它的服務租約。同時也能夠從服務端查詢當前註冊的服務信息並把他們緩存到本地並週期性的刷新服務狀態。spring

3、使用Eureka進行服務治理

1. 搭建服務註冊中心

單首創建一個Springboot項目,並添加以下的依賴緩存

<dependencies>
  <!--添加Eureka服務器端依賴-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
</dependencies>

 <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Brixton.SR5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

在Springboot項目中的main入口,添加@EnableEurekaServer註解,來開啓服務註冊中心springboot

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

在默認狀況下,服務註冊中心也會把本身當作是一個服務,將本身註冊進服務註冊中心,因此咱們能夠經過配置來禁用他的客戶端註冊行爲,在application.properties中添加以下配置:服務器

server.port=3333

eureka.instance.hostname=localhost
#不要向註冊中心註冊本身
eureka.client.register-with-eureka=false
#禁止檢索服務
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka

啓動應用,並訪問http://localhost:3333/便可看到Eureka信息面板,以下:app

2. Eureka信息面板.png

從上圖看到,在"Instances currently registered with Eureka"信息中,沒有一個實例,說明目前尚未服務註冊。框架

2. 註冊服務

基於文章[微服務系列] 微服務構建框架--Spring Boot中構建的第一個Springboot項目(在附件中的springbootdemo.zip實例),來進行改造。首先在pom文件中添加Eureka客戶端相關的依賴:微服務

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

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Brixton.RS5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

在以前的TestRestful方法中添加以下代碼,將org.springframework.cloud.client.discovery.DiscoveryClient;對象注入,而且在日誌中打印出與服務相關的一些信息。post

@RestController
public class TestRestful {

    private final Logger logger=Logger.getLogger(getClass());

    @Autowired
    private DiscoveryClient client;

    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String home(){
        ServiceInstance instance=client.getLocalServiceInstance();
        logger.info("serviceId"+instance.getServiceId()+"host:post="+instance.getHost()+":"+instance.getPort());
        return "hello spring";
    }
}

在主類上添加@EnableEurekaClient註解以實現Eureka中的DiscoveryClient實現。

@EnableEurekaClient
@SpringBootApplication
public class SpringbootdemoApplication {

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

最後在配置文件application.yml中配置與服務相關的一些基本信息,如服務名、註冊中心地址(即上一節中設置的註冊中心地址http://localhost:3333/eureka)

#  設置服務名
spring:
  application:
    name: hello-service
#  設置註冊中心地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:3333/eureka
3. 測試
  1. 啓動註冊中心服務
  2. 啓動springbootdemo項目,能夠看到以下的信息,說明此服務已經註冊在了註冊中心

3 服務啓動日誌.png

同時訪問http://localhost:3333/能夠在Instances currently registered with Eureka中看到已經有一個服務註冊了進來,而且名稱爲HELLO-SERVICE的服務

4 服務啓動.png

  1. 訪問http://localhost:8088/hello能夠在控制檯中看到以下日誌信息:

5 日誌.png

4、項目代碼截圖

基於Eureka的服務治理

代碼地址以下:
http://www.demodashi.com/demo/11927.html

注:本文著做權歸做者,由demo大師代發,拒絕轉載,轉載須要做者受權

相關文章
相關標籤/搜索