美文网首页
Spring Cloud Eureka 入门

Spring Cloud Eureka 入门

作者: 歌哥居士 | 来源:发表于2019-03-30 12:33 被阅读0次

    注册中心

    <?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.1.3.RELEASE</version>
          <relativePath/> <!-- lookup parent from repository -->
       </parent>
       <groupId>org.baozi</groupId>
       <artifactId>demo-eureka-server</artifactId>
       <version>1.0</version>
       <name>demo-eureka-server</name>
       <description>Demo project for Spring Boot</description>
    
       <properties>
          <java.version>1.8</java.version>
          <spring-cloud.version>Greenwich.RELEASE</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>
          </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>
    
    </project>
    

    application.properties

    spring.application.name=eureka-server
    server.port=8081
    
    eureka.instance.hostname=localhost
    # 不注册自己,因为本身这个就是注册中心
    eureka.client.register-with-eureka=false
    # 是否从eureka server获取注册信息。这里是单点所以false
    eureka.client.fetch-registry=false
    

    启动类

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
    
    @EnableEurekaServer
    @SpringBootApplication
    public class DemoEurekaServerApplication {
    
       public static void main(String[] args) {
          SpringApplication.run(DemoEurekaServerApplication.class, args);
       }
    
    }
    

    服务提供者

    <dependencies>
       <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
       </dependency>
       <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>
       </dependency>
    </dependencies>
    

    application.properties

    spring.application.name=eureka-provider
    server.port=8082
    
    # 设置与Eureka交互的地址,用来查询和注册服务。使用逗号分隔多个。
    eureka.client.service-url.defaultZone=http://localhost:8081/eureka/
    

    启动类

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    
    @EnableDiscoveryClient
    // @EnableEurekaClient // 这个也可以
    @SpringBootApplication
    public class DemoEurekaProviderApplication {
    
       public static void main(String[] args) {
          SpringApplication.run(DemoEurekaProviderApplication.class, args);
       }
    
    }
    

    控制器

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.cloud.client.discovery.DiscoveryClient;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class ProviderController {
    
        @Autowired
        private DiscoveryClient discoveryClient;
    
        @GetMapping("/provider")
        private String provider() {
            return "Services: " + discoveryClient.getServices();
        }
    
    }
    

    服务消费者

    <dependencies>
       <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
       </dependency>
       <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>
       </dependency>
    </dependencies>
    

    application.properties

    spring.application.name=eureka-consumer
    server.port=8083
    
    eureka.client.service-url.defaultZone=http://localhost:8081/eureka/
    

    启动类

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    import org.springframework.context.annotation.Bean;
    import org.springframework.web.client.RestTemplate;
    
    @EnableDiscoveryClient
    @SpringBootApplication
    public class DemoEurekaConsumerApplication {
    
       // 用于发起Rest请求
       @Bean
       public RestTemplate restTemplate() {
          return new RestTemplate();
       }
    
       public static void main(String[] args) {
          SpringApplication.run(DemoEurekaConsumerApplication.class, args);
       }
    
    }
    

    控制器

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.cloud.client.ServiceInstance;
    import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate;
    
    import java.text.MessageFormat;
    
    @RestController
    public class ConsumerController {
    
        // 用于发起Rest请求
        @Autowired
        private RestTemplate restTemplate;
        // 负载均衡
        @Autowired
        private LoadBalancerClient loadBalancerClient;
    
        @GetMapping("/consumer")
        private String consumer() {
            ServiceInstance serviceInstance = loadBalancerClient.choose("eureka-provider");
            String url = MessageFormat.format("http://{0}:{1}/provider",
                    // MessageFormat.format会把int类型插入成8,082, 而不是自动转String"8082"
                    // 获取Int类型之后手动转: String.valueOf(serviceInstance.getPort())
                    serviceInstance.getHost(), String.valueOf(serviceInstance.getPort()));
            return restTemplate.getForObject(url, String.class);
        }
    
    }
    

    分别启动注册中心,提供者,消费者
    访问http://localhost:8081/,看到界面
    访问http://localhost:8082/provider : Services: [eureka-provider, eureka-consumer]
    访问http://localhost:8083/consumer : Services: [eureka-provider, eureka-consumer]

    相关文章

      网友评论

          本文标题:Spring Cloud Eureka 入门

          本文链接:https://www.haomeiwen.com/subject/rsacvqtx.html