• SpringBoot集成Eureka


    0、公共pom文件,各项目都需要

        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.3.5.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
            <spring-cloud.version>Hoxton.SR8</spring-cloud.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</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>
    

    1、EurekaServer_7001

    额外添加spring-cloud-starter-netflix-eureka-server

            <!-- 引入的Eureka-server -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            </dependency>
    

    application.yml

    server:
      port: 7001 #服务注册中心端口号
    eureka:
      instance:
        hostname: 127.0.0.1 #服务注册中心IP地址
      client:
        registerWithEureka: false #是否向服务注册中心注册自己
        fetchRegistry: false #是否检索服务
        serviceUrl: #服务注册中心的配置内容,指定服务注册中心的位置
          defaultZone: http://127.0.0.1:7001/eureka/
    

    EurekaServer_7001

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
    
    @SpringBootApplication
    @EnableEurekaServer //服务端的启动类,可以接受别人注册进来
    public class EurekaServer_7001 {
        public static void main(String[] args) {
            SpringApplication.run(EurekaServer_7001.class, args);
        }
    }
    

    2、EurekaProvider_8001

    pom

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
    
    
    eureka:
      client:
        serviceUrl: #注册中心的注册地址
          defaultZone: http://127.0.0.1:7001/eureka/
    server:
      port: 8001  #服务端口号
    spring:
      application:
        name: service-provider #服务名称--调用的时候根据名称来调用该服务的方法
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    
    @SpringBootApplication
    @EnableEurekaClient
    public class EurekaProvider_8001 {
        public static void main(String[] args) {
            SpringApplication.run(EurekaProvider_8001.class, args);
        }
    }
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    @RestController
    public class TestController {
        @RequestMapping("/get")
        public String get() {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
            return "provider-A " + sdf.format(new Date());
        }
    }
    

    3、EurekaConsumer_9001

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
    
    eureka:
      client:
        serviceUrl: #注册中心的注册地址
          defaultZone: http://127.0.0.1:7001/eureka/
    server:
      port: 9001  #服务端口号
    spring:
      application:
        name: service-consumer #服务名称--调用的时候根据名称来调用该服务的方法
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.loadbalancer.LoadBalanced;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    import org.springframework.context.annotation.Bean;
    import org.springframework.web.client.RestTemplate;
    
    @SpringBootApplication
    @EnableEurekaClient
    public class EurekaConsumer_9001 {
        @Bean
        @LoadBalanced   //启用负载均衡机制
        public RestTemplate restTemplate() {
            return new RestTemplate();
        }
    
        public static void main(String[] args) {
            SpringApplication.run(EurekaConsumer_9001.class, args);
        }
    }
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;
    import org.springframework.web.client.RestTemplate;
    
    @RestController
    public class TestController {
        @Autowired
        RestTemplate restTemplate;
    
        @RequestMapping("/get")
        public String getUser() {
            return restTemplate.getForObject("http://service-provider/get", String.class);
        }
    }
    
  • 相关阅读:
    @property @synthesize的含义以及误区。
    OC中类的扩展介绍
    Thinking in Java第三、四章学习笔记----操作符与控制执行流程
    LeetCode之Easy篇 ——(12)Integer to Roman
    LeetCode之Easy篇 ——(9)Palindrome Number
    LeetCode之Easy篇 ——(7)Reverse Integer
    LeetCode之Easy篇 ——(1)Two Sum
    Java关键字汇总
    Thinking in Java 第二章学习笔记----一切都是对象
    php文件上传原理详解
  • 原文地址:https://www.cnblogs.com/kikyoqiang/p/14672732.html
Copyright © 2020-2023  润新知