0402-服務注冊與發現-Eureka Server使用、將服務注冊到Eureka server上


一、Eureka Server使用

官方文檔地址:http://cloud.spring.io/spring-cloud-static/Edgware.SR3/single/spring-cloud.html#_spring_cloud_netflix

1.1、原項目改造【方便后續開發增加父pom】

  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>com.lhx.cloud</groupId>
    <artifactId>microservice-spring-cloud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>microservice-spring-cloud</name>
    <description>Demo project for Spring Boot</description>

    <modules>
        <module>microservice-comsumer-movie</module>
        <module>microservice-provider-user</module>
    </modules>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Edgware.SR3</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>
</project>
View Code

  3》修改上兩個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>

    <artifactId>microservice-provider-user</artifactId>
    <packaging>jar</packaging>
    <parent>
        <groupId>com.lhx.cloud</groupId>
        <artifactId>microservice-spring-cloud</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
</project>
View Code
<?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>

    <artifactId>microservice-comsumer-movie</artifactId>
    <packaging>jar</packaging>

    <parent>
        <groupId>com.lhx.cloud</groupId>
        <artifactId>microservice-spring-cloud</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

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



</project>
View Code

  4》導入項目,更新maven即可。

1.2、eureka server使用

  新建maven module項目,microservice-discovery-eureka,單機使用

  pom依賴

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

  配置文件  

#server.port: 指明了應用啟動的端口號
#eureka.client.registerWithEureka: 值為false意味着自身僅作為服務器,不作為客戶端
#eureka.client.fetchRegistry: 值為false意味着無需注冊自身
#eureka.client.serviceUrl.defaultZone: 指明了應用的URL

server:
  port: 8761
eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://localhost:8761/eureka

  項目啟動類

@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); } }

  現在可以訪問http://localhost:8761,基本都是字面含義

  git地址:https://github.com/bjlhx15/spring-cloud

  使用tag:初始化eureka-server

二、將服務注冊到Eureka server上

按照官方文檔:http://cloud.spring.io/spring-cloud-static/Edgware.SR3/single/spring-cloud.html#_service_discovery_eureka_clients

修改microservice-provider-user服務

1、pom引用

        <dependency>
            <groupId>org.springframework.cloud</groupId>
                        <!--原引用方式,可用-->
            <!-- <artifactId>spring-cloud-starter-eureka</artifactId> -->
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

2、注冊Eureka

舊版本需要增加@EnableEurekaClient、@EnableDiscoveryClient

新版本按照文檔不需要增加,必須是spring-boot項目 

3、配置文件配置application.yml

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}

盡量補全應用程序名

spring:
  application:
    name: mircoservice-provider-user # 建議小寫

此時啟動項目即可。

三、Eureka Server增加授權認證

官方文檔:http://cloud.spring.io/spring-cloud-static/Edgware.SR3/single/spring-cloud.html#_service_discovery_eureka_clients

修改microservice-discovery-eureka項目

1、配置屬性

eureka.client.serviceUrl.defaultZone=http://user:password@localhost:8761/eureka即可

實踐證明不可行。需要增加security配置

2、security配置pom.xml

  添加 spring-boot-starter-security,該依賴為Eureka Server 提供用戶認證能力。 

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

3、增加配置文件

security:
  basic:
    enabled: true #開啟驗證
  user:
    name: user #用戶名
    password: a123 #密碼

注意如果此處增加了認證,在注冊服務的時候也需要修改注冊服務的鏈接,即第二部分的3設置為

eureka:
  client:
    serviceUrl:
      defaultZone: http://user:a123@localhost:8761/eureka/

四、應用服務增加監控和管理

在user 的pom增加

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

參看地址:http://www.cnblogs.com/bjlhx/p/8719625.html

修改eureka路徑

eureka:
  instance:
    statusPageUrlPath: ${management.context-path}/info
    healthCheckUrlPath: ${management.context-path}/health

五、其他

5.1、注冊安全應用程序

也就是https配置:http://cloud.spring.io/spring-cloud-static/Edgware.SR3/single/spring-cloud.html#_registering_a_secure_application

5.2、eureka的健康檢查

在spring-cloud-netfix-eureka 1.1 之后使用eureka.client.healthcheck.enabled=true配置,此配置也是基礎acturator的

  查看:http://localhost:8761/health

5.3、實例Id

  eureka.instance.instanceId=${spring.application.name}:${spring.application.instance_id:${server.port}}

5.4、eurekaClient與discoveryClient

import

import org.springframework.cloud.client.discovery.DiscoveryClient;
import com.netflix.discovery.EurekaClient;

代碼

    @GetMapping("/eureka-instance")
    public String serviceUrl() {
        InstanceInfo instance = eurekaClient.getNextServerFromEureka("MIRCOSERVICE-PROVIDER-USER", false);
        return instance.getHomePageUrl();
    }
    @GetMapping("/eureka-info")
    public ServiceInstance showInfo() {
        ServiceInstance serviceInstance = discoveryClient.getLocalServiceInstance();
        return serviceInstance;
    }
    @GetMapping("/eureka-url")
    public Object serviceUrl2() {
        List<ServiceInstance> list = discoveryClient.getInstances("MIRCOSERVICE-PROVIDER-USER");
        if (list != null && list.size() > 0 ) {
            return list.get(0).getUri();
        }
        return null;
    }

5.5、http 使用

  默認情況下,EurekaClient使用Jersey進行HTTP通信。如果您希望避免來自Jersey的依賴關系,則可以將它從依賴關系中排除。 Spring Cloud將自動配置基於Spring RestTemplate的傳輸客戶端。

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
    <exclusions>
        <exclusion>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-client</artifactId>
        </exclusion>
        <exclusion>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-core</artifactId>
        </exclusion>
        <exclusion>
            <groupId>com.sun.jersey.contribs</groupId>
            <artifactId>jersey-apache-client4</artifactId>
        </exclusion>
    </exclusions>
</dependency>
View Code

以上類似的配置可以配置到microservice-comsumer-movie上即可

代碼地址:https://github.com/bjlhx15/spring-cloud

 

以上項目啟動 先啟動發現服務,啟動后在分別啟動user和movie

 


免責聲明!

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



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