SpringCLoud Eureka注册发现

224 阅读4分钟

1、创建服务中心 Eureka Server

1.1 新建spring cloud工程,springcloud-eureka-server

1.2 在工程启动类添加@EnableEurekaServer

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


@EnableEurekaServer
@SpringBootApplication
public class SpringcloudEurekaServerApplication {

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

}

1.3 配置文件 application.properties ,配置内容:

server.port= 9525
eureka.instance.hostname=localhost
eureka.client.fetch-registry=false
eureka.client.register-with-eureka=false
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
eureka.server.eviction-interval-timer-in-ms=5000
eureka.server.enable-self-preservation=true
eureka.server.renewal-percent-threshold=0.1

1.4 启动 服务中心 工程,打开浏览器,访问 服务中心 前台页面:http://localhost:9525

No instances available :没有可用的实例。目前还任何服务注册到服务中心。

2、创建服务中心集群

2.1 创建工程配置文件 application-server1.properties ,配置内容:

server.port= 9527
spring.profiles=server1
eureka.instance.hostname=localhost
eureka.client.fetch-registry=true
eureka.client.register-with-eureka=false
eureka.client.service-url.defaultZone=http://localhost:9527/eureka/,http://localhost:9528/eureka/,http://localhost:9529/eureka/
eureka.server.eviction-interval-timer-in-ms=5000
eureka.server.enable-self-preservation=false
eureka.server.renewal-percent-threshold=0.49

并创建工程配置文件 application-server2.properties,并修改 server.port 为 9528 以及 application-server3.properties,并修改 server.port 为 9529

2.2 启动EurekaServer1、EurekaServer2、EurekaServer3,访问 服务中心 前台页面:http://localhost:9527, http://localhost:9528, http://localhost:9529,三个页面都会如下图所示:

以上一个简单的服务中心集群搭建完成。

3、创建服务提供者Eureka Client

3.1 新建spring cloud工程,springcloud-eureka-client

3.2 在工程启动类添加@EnableEurekaClient

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

@EnableEurekaClient
@SpringBootApplication
public class SpringcloudEurekaClientApplication {

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

}

3.3 新建 Eureka Client 控制器类 EurekaClientController,提供一个 Restful 服务

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
public class EurekaClientController {
    @Value("${server.port}")
    private String port;

    /**
     * 提供的一个restful服务,返回内容格式:服务协议://服务器地址:服务端口/服务uri
     *
     * @param request
     * @return
     */
    @RequestMapping("/info")
    public String info(HttpServletRequest request) {
        String message = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getServletPath();
        return message;
    }
}

3.4 配置文件 application.properties ,配置内容:

server.port= 52601
spring.application.name=eureka-client
eureka.instance.hostname=localhost
eureka.instance.lease-renewal-interval-in-seconds=5
eureka.instance.lease-expiration-duration-in-seconds=10
eureka.client.service-url.defaultZone= http://localhost:9527/eureka/,http://localhost:9528/eureka/,http://localhost:9529/eureka/

3.5 启动 Eureka Client 工程,打开浏览器,访问 Eureka Client 提供的 Restful 路径 http://localhost:52601/info

3.6 ,访问 服务中心 前台页面 http://localhost:9527http://localhost:9528http://localhost:9529

服务成功注册到了服务中心,服务名称:EUREKA-CLIENT。

4、创建服务提供者集群

4.1 创建工程配置文件 application-client1.properties ,配置内容:

server.port= 52601
spring.profiles=client1
spring.application.name=eureka-client
eureka.instance.hostname=localhost
eureka.instance.lease-renewal-interval-in-seconds=5
eureka.instance.lease-expiration-duration-in-seconds=10
eureka.client.service-url.defaultZone= http://localhost:9527/eureka/,http://localhost:9528/eureka/,http://localhost:9529/eureka/

并创建工程配置文件 application-client2.properties,并修改 server.port 为 52602 以及 application-client3.properties,并修改 server.port 为 52603

4.2 按照启动配置 EurekaClientC1、EurekaClientC2、EurekaClientC3 依次启动服务提供者,打开浏览器,访问 服务提供者 提供的Restful路径 http://localhost:52601/info,http://localhost:52602/info,http://localhost:52603/info

如上,三个 Restful 服务均返回了信息。信息内容不同之处就在于端口不一样。输出这样的信息目的,是为了后面做集群负载均衡测试做准备。

4.3 访问 服务中心 前台页面 http://localhost:9527http://localhost:9528http://localhost:9529

三个服务成功注册到了服务中心,服务名称均为:EUREKA-CLIENT。 以上一个简单的服务提供者集群部署搭建完成。

5、创建服务消费者

5.1 新建spring cloud工程,springcloud-eureka-feign

5.2 在工程启动类中,添加注解 @EnableDiscoveryClient,@EnableFeignClients

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;

@EnableHystrix
@EnableDiscoveryClient // 启用 Eureka 服务发现
@EnableFeignClients // 启用 Feign
@SpringBootApplication
public class SpringcloudEurekaFeignApplication {

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

}

5.3 创建服务类 EurekaFeignService

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

//@FeignClient(value = "eureka-client") // 调用的服务的名称
@FeignClient(value = "EUREKA-CLIENT", fallback = EurekaFeignServiceFailure.class) // 调用的服务的名称
public interface EurekaFeignService {
    @RequestMapping(value = "/info")
    String getInfo();
}

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class EurekaRibbonService {

    @Autowired
    private  RestTemplate restTemplate;

    public String getInfo() {
        String message;
        try {
            System.out.println("调用 服务 EUREKA-CLIENT/info");
            message = restTemplate.getForObject("http://EUREKA-CLIENT/info", String.class);
            System.out.println("here");
            System.out.println("服务 EUREKA-CLIENT/info 返回信息 : " + message);
            System.out.println("调用 服务 EUREKA-CLIENT/info 成功!");
        } catch (Exception ex) {
            message = ex.getMessage();
        }
        return message;
    }

5.4 创建控制器类 EurekaFeignController

import com.example.springcloudeurekafeign.service.EurekaFeignService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class EurekaFeignController {
    @Resource
    private EurekaFeignService eurekaFeignService;

    @RequestMapping("/feignInfo")
    public String feignInfo() {
        String message = eurekaFeignService.getInfo();
        return "获取到的信息:" + message;
    }
}

5.5 配置文件 application.properties ,配置内容:

server.port= 52620
spring.application.name=eureka-discovery-feign
eureka.instance.hostname=localhost
eureka.instance.lease-renewal-interval-in-seconds=5
eureka.instance.lease-expiration-duration-in-seconds=10
eureka.client.service-url.defaultZone=http://localhost:9527/eureka/,http://localhost:9528/eureka/,http://localhost:9529/eureka/
feign.hystrix.enabled=true

5.6 启动服务消费者工程,访问服务中心前台界面 http://localhost:9527http://localhost:9528http://localhost:9529

可以看到服务消费实例已经注册到了服务注册中心,服务名 EUREKA-DISCOVERY-FEIGN

访问 http://localhost:52620/feignInfo, 多次刷新该地址

可以看到服务消费者成功调用了服务提供者提供的服务,并实现了负载均衡,轮询请求不同的服务提供者。

以上一个简单的单点服务消费者搭建完成。