Spring Cloud Eureka 生产者,消费者,注册中心

Spring cloud Eureka 是SpringCloud Netflix微服务套件中的一部分,它基于Netflix Eureka做了二次封装。主要负责完成微服务服务架构的服务治理功能。

一:搭建服务注册中心

  首先,创建一个基础SpringBoot工程,(这里就不介绍怎么搭建SpringBoot工程,可自行百度),命名为:Spring-server,并 在pom.xml中引入必要的依赖内容

<dependencies>

<!--增加eureka-server的依赖-->

        <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>Camden.SR5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
 </dependencyManagement>

         通过@EnableEurekaServer注解启动一个服务注册中心给其他应用进行对话

/**
 * @EnableEurekaServer:
 *  该注解表明应用为eureka服务,有可以联合多个服务作为集群,对外提供服务注册以及发现功能
 */
@EnableEurekaServer
@SpringBootApplication
public class SpringServceApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(SpringServceApplication.class).web(true).run(args);
}
}

默认配置下,该服务中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行不,在application.peopertise中心添加如下配置

#服务注册中心端口号

server.port=1111

#注册中心的IP地址

eureka.instance.hostname=localhost

#是否向服务注册中心注册自己,默认为true

eureka.client.register-with-eureka=false

#是否需要去检索寻找服务,因为是注册中心,他的任务是维护服务实例,所以不需要去寻找服务,默认是true

eureka.client.fetch-registry=false

#注册中心的url

eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

#服务注册完后,会有一个心跳来持续高收益Spring-server 我还活着,已防止spring-server 剔除任务,

#将服务实例从服务中排除出去,我们就该操作为服务续约

#用于定义服务续约任务的调用间隔时间默认30秒

euekka.instance.lease-renewal-interval-in-seconds=30

#用于定义服务失效时间 默认90秒

euekka.instance.lease-expiration-duration-in-seconds=90

#关闭保护机制

eureka.server.enable-self-preservation=false

启动程序后,

Instances currently registered with Eureka栏是空的,说明服务注册中心还没有注册任何服务


至此:服务中心就配置完成了



二:搭建服务提供者

 下面是搭建一个服务提供者来加入到eureka的服务治理体系中去

创建一个SpringBoot工程,命名为Spring-offer 并在pox.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>Camden.SR5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

        </dependencies>

         </dependencyManagement>

接着创建一个Controller处理接口,注入DiscoveryClient对象

@RestController
public class HelloController {
private final Logger logger = Logger.getLogger(getClass());

@Autowired
    private DiscoveryClient client;

@RequestMapping(value = "/text", method = RequestMethod.GET)
public String text(){
return "HELLO WORD";
}

然后在启动项中加入@EnableDiscoveryClient注解,激活eureka中的EnableDiscoveryClient实现(

自动装配,创建EnableDiscoveryClient接口针对Eureka客户端的EnableDiscoveryClient实例)

@EnableDiscoveryClient
@SpringBootApplication
public class SpringBoot1Application {


public static void main(String[] args) {
new SpringApplicationBuilder(SpringBoot1Application.class).web(true).run(args);
}
}

最后修改application.propertion配置文件

#命名服务  这里命名为hello-server

spring.application.name=hello-server

#命名端口号

server.port=2222

#指定服务注册中心的地址,这里地址为上面创建的服务中心地址

eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

启动后可以看到注册中心上有一个命为hello-server 端口号为2222的 服务了


三:搭建服务消费者

接下来创建一个SpringBoot工程来命名来实现消费者,命名为 spring-ribbon ,并在pox.xml中添加如下依赖

</dependencies>

        <dependency>

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

</dependencies>
<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Camden.SR5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
 </dependencyManagement>
在application.propertise中配置如下

#给消费者的命名

spring.application.name=ribbon-cousumer 

#消费者的端口号

server.port=3333

#将消费者注册要注册中心

eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

#修改缓存清单名称,已确保不会调用已出现异常的提供者

eureka.client.registry-fetch-interval-seconds=30

启动项加入如下2个注解

@EnableDiscoveryClient:让该应用注册到Euteka客户端,以获取服务发现的能力

 @EnableFeignClients:Feign 是一个声明web服务客户端,使用Feign 创建一个接口并对它进行注解,它具有可插拔的注解支持包括Feign注解与JAX-RS注解SpringCloud对Feign进行了封装,使其支持SpringMVC标准注解和HttpMessageConverters。Feign可以    与    Eureka和Ribbon组合使用以支持负载均衡。

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class RibbonConsumerApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(RibbonConsumerApplication.class).web(true).run(args);
}
}
创建一个server接口来实现HELLO-SERVER的 /text 接口   加入@FeignClient("hello-server") 注解 用来你指定命名的服务提供者的命名

/**
 * @FeignClient用于通知Feign组件对该接口进行代理(不需要编写接口实现),使用者可直接通过@Autowired注入。
 * @RequestMapping表示在调用该方法时需要向/text 发送GET请求。 *
 */
@FeignClient("hello-server")
public interface Servers {

@RequestMapping(value = "/text", method = RequestMethod.GET)
String add();
}

在创建一个Controller 来实现 server接口  路径设为/add

@RestController
public class Controllers {
@Autowired Servers server;

@RequestMapping(value = "/add", method = RequestMethod.GET)
public String add() {
  return server.add();
}
}

启动消费者,在游览器打印消费的接口路径,返回提供者的数据,如图。


至此,Spring Cloud的 服务提供者,消费者 ,注册中心已全部完成


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章