1. 程式人生 > >spring-cloud配置eureka客戶端

spring-cloud配置eureka客戶端

ons tin ati list isempty work glassfish report too

spring-cloud配置eureka客戶端

eureka用來發現其他程序

需要提前配置eureka服務端,具體看 https://www.cnblogs.com/ye-hcj/p/10292944.html

依賴

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>test</groupId>
    <artifactId>test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <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>
            <version>2.0.5.RELEASE</version>
        </dependency>

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

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
        </dependency>

    </dependencies>

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

Application

package test.test;

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

@EnableEurekaClient
@SpringBootApplication
public class DemoApplication {

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

}

配置

spring:
  application:
    name: eureka-client-clustered 
     
---

eureka:
  instance:
    appname: eureka-client-clustered  
  client:   
    fetchRegistry: true
    serviceUrl:
      defaultZone: http://eureka:9001/eureka
server:
  port: 8000  
  
---
# java -jar -Dspring.profiles.active=c1 test-0.0.1-SNAPSHOT.jar
# java -jar -Dspring.profiles.active=c2 test-0.0.1-SNAPSHOT.jar
# java -jar -Dspring.profiles.active=c3 test-0.0.1-SNAPSHOT.jar
spring:
  profiles: c1
eureka:
  instance:
    appname: eureka-client-clustered
  client:   
    fetchRegistry: true
    serviceUrl:
      defaultZone: http://eureka:9001/eureka  
server:
  port: 8001   
     
---
spring:
  profiles: c2
eureka:
  instance:
    appname: eureka-client-clustered
  client:   
    fetchRegistry: true
    serviceUrl:
      defaultZone: http://eureka:9001/eureka   
server:
  port: 8002
  
---
spring:
  profiles: c3
eureka:
  instance:
    appname: eureka-client-clustered
  client:   
    fetchRegistry: true
    serviceUrl:
      defaultZone: http://eureka:9001/eureka
server:
  port: 8003

Controller

package test.test.controller;

import java.util.List;

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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
    @Autowired
    private DiscoveryClient discoveryClient;
    
    @ResponseBody
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home() {
 
        return "<a href=‘showAllServiceIds‘>Show All Service Ids</a>";
    }
 
    @ResponseBody
    @RequestMapping(value = "/showAllServiceIds", method = RequestMethod.GET)
    public String showAllServiceIds() {
 
        List<String> serviceIds = this.discoveryClient.getServices();
 
        if (serviceIds == null || serviceIds.isEmpty()) {
            return "No services found!";
        }
        String html = "<h3>Service Ids:</h3>";
        for (String serviceId : serviceIds) {
            html += "<br><a href=‘showService?serviceId=" + serviceId + "‘>" + serviceId + "</a>";
        }
        return html;
    }
 
    @ResponseBody
    @RequestMapping(value = "/showService", method = RequestMethod.GET)
    public String showFirstService(@RequestParam(defaultValue = "") String serviceId) {
 
        List<ServiceInstance> instances = this.discoveryClient.getInstances(serviceId);
 
        if (instances == null || instances.isEmpty()) {
            return "No instances for service: " + serviceId;
        }
        String html = "<h2>Instances for Service Id: " + serviceId + "</h2>";
 
        for (ServiceInstance serviceInstance : instances) {
            html += "<h3>Instance: " + serviceInstance.getUri() + "</h3>";
            html += "Host: " + serviceInstance.getHost() + "<br>";
            html += "Port: " + serviceInstance.getPort() + "<br>";
        }
 
        return html;
    }
}

運行

運行 mvn install 將項目打包成jar
分別執行
java -jar -Dspring.profiles.active=c1 test-0.0.1-SNAPSHOT.jar
java -jar -Dspring.profiles.active=c2 test-0.0.1-SNAPSHOT.jar
java -jar -Dspring.profiles.active=c3 test-0.0.1-SNAPSHOT.jar

然後訪問,即可看到其他同時註冊的程序
    http://localhost:8001
    http://localhost:8002
    http://localhost:8003

spring-cloud配置eureka客戶端