springcloud之eureka高可用注册中心搭建HelloWorld

SpringCloud的简介在这里先不多说,直接上代码。
1.首先新建一个springboot项目pom文件如下:

<dependencies>
        <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>Dalston.SR1</version>
               <type>pom</type>
               <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>  
  
    
    <build>
        <finalName>spring-cloud-001-eureka-a</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!-- 主函数的入口 -->
                    <mainClass>com.wz.springcloud.eureka.EurekaApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

2.配置yml文件,多环境配置,maven多环境打包这里不多说
首先新建application.yml文件,内容如下:

spring:
  application: 
    name: eureka-server
  profiles:   ## application-{}.yml
    #active: $spring.profiles.active$
    active: dev ##默认加载application-dev.yml文件,可以采用$spring.profiles.active$这种方式用maven多环境打包来定义是dev 还是 pro

application-dev.yml 内容如下:

server:
  port: 8001
    
eureka: 
  instance:
    hostname: eureka1
    lease-renewal-interval-in-seconds: 10   ##客户端向服务器(注册中心发送心跳的时间间隔)
    lease-expiration-duration-in-seconds: 120 ##服务器(注册中心)租期到期的时间, 也就是说服务器在收到最后一次心跳的时间上线
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
       #defaultZone: http://localhost:8001/eureka/  
      defaultZone: http://eureka2:8002/eureka/  ##互相注册到对方--高可用服务发现  

3.新建EurekaApplication内容如下:启动注册中心

package com.wz.springcloud.eureka;

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

@EnableEurekaServer     //启用服务器的配置中心
@SpringBootApplication
public class EurekaApplication {
    
    
    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class, args);
    }   
}

这里第一个注册中心就OK了 ,接下来在复制一份一模一样的项目就可以了
只需要将 application-dev.yml中的 server.port修改一下可以改为8002 还有将eureka.instance.hostname 改为eureka2。既然是高可用那么在defaultZone中一定要互相引用注册到对方,做的高可用的服务发现

最后右击-->Run AS -->spring boot App启动就可以了
在浏览器输入http://localhost:8001/ 或者http://localhost:8002/ 就可以了。
8001界面如下:

Paste_Image.png

8002界面如下:

Paste_Image.png

以上是高可用的Eureka 注册中心为两台。

接下来写一个服务的提供者和消费者来通过注册中心调用。
首先新建一个生产者provider。springboot项目
pom.xml如下:

  <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>Dalston.SR1</version>
               <type>pom</type>
               <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>  
  
    
    <build>
        <finalName>spring-cloud-01-provider</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!-- 主函数的入口 -->
                    <mainClass>bhz.springcloud.ProviderApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>  

application.yml

spring:
  application: 
    name: provider-service1

server:
  context-path: /provider
  port: 7001
  
eureka:
  client:
    service-url:
      ##单点配置:
      #defaultZone: http://eureka1:8001/eureka/
      ##高可用配置
      defaultZone: http://eureka1:8001/eureka/,http://eureka2:8002/eureka/   
  

一个简单的controller

package com.wz.springcloud.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class IndexController {

    @RequestMapping(value = "/hello", method = {RequestMethod.GET})
    public String hello(){
        return "Hello World";
    }
        
}

启动入口类ProviderApplication

package com.wz.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient  //表示我是一个服务,注册到服务中心上
@SpringBootApplication
public class ProviderApplication {

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

启动provider界面如下,将服务注册到了注册中心

Paste_Image.png

接下来消费者pom.xml如下,这里新加了一个负载均衡ribbon

 <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <!-- 负载均衡组件的jar -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
        </dependency>       
    </dependencies>
    
    <dependencyManagement>
        <dependencies>
            <dependency>
               <groupId>org.springframework.cloud</groupId>
               <artifactId>spring-cloud-dependencies</artifactId>
               <version>Dalston.SR1</version>
               <type>pom</type>
               <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>  
  
    
    <build>
        <finalName>spring-cloud-001-consumer</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!-- 主函数的入口 -->
                    <mainClass>bhz.springcloud.ConsumerApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>  

application.yml

spring:
  application: 
    name: consumer-service1

server:
  context-path: /consumer
  port: 7002
  
eureka:
  client:
    service-url:
      ##单点配置:
      #defaultZone: http://eureka1:8001/eureka/
      ##高可用配置
      defaultZone: http://eureka1:8001/eureka/,http://eureka2:8002/eureka/  
  

简单的controller

package cmo.wz.springcloud.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class ConsumerController {

    @Autowired
    private RestTemplate restTemplate;
    
    @RequestMapping(value="getHello", method={RequestMethod.GET})
    public String getHello() {
        ResponseEntity<String> str=restTemplate.getForEntity("http://provider-service1/provider/hello", String.class);
        return str.getBody();
    }
}

restTemplate有很多方法可以多看下api

启动入口类:ConsumerApplication

package cmo.wz.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@EnableDiscoveryClient  //表示我是一个服务,注册到服务中心上
@SpringBootApplication
public class ConsumerApplication {

    
    @Bean
    @LoadBalanced       //自动负载均衡: 他的机制是(通过)application name 去寻找服务发现 然后去做负载均衡策略的
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
    
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }
    
}

启动consumer,两个服务都注册上来了,如图

Paste_Image.png

最后访问一下

Paste_Image.png

通过consumer调用了provider,并且provider将信息返回到了consumer

高可用的eureka注册中心就先到这里。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容