Spring Cloud Eureka实现服务治理

一、创建服务注册中心项目 registrycenter

Spring Cloud Eureka实现服务治理

Next,选择依赖的类库Eureka Server,Spring Boot版本选择最新,Next,Finish

Spring Cloud Eureka实现服务治理

项目创建完成,在主类上新增注解@EnableEurekaServer

Spring Cloud Eureka实现服务治理

在application.properties文件中增加配置信息

#注册中心服务ID
spring.application.name=eureka-server
#端口号
server.port=1111

eureka.instance.hostname=127.0.0.1
#是否将自己注册到Eureka服务中心,默认为true
eureka.client.register-with-eureka=false
#是否从Eureka服务中心获取注册信息
eureka.client.fetch-registry=false
#设置与Eureka服务中心交互地址,查询和注册服务都需要依赖这个地址
eureka.client.service-url.defaultZone=http://localhost:${server.port}/eureka/

运行application主类,访问http://l27.0.0.1:1111/

Spring Cloud Eureka实现服务治理

Eureka页面显示正常,注册中心创建成功。

二、创建服务项目MyServiceServer,在注册中心进行注册

Spring Cloud Eureka实现服务治理

Next,选择类库Eureka Discovery和SpringBoot版本,Next,Finish

Spring Cloud Eureka实现服务治理

项目创建完成,在启动类上添加注解@EnableDiscoveryClient,声明这是一个eureka client,否则不会进行服务注册

Spring Cloud Eureka实现服务治理

在application.properties配置文件增加配置信息

#服务名称
spring.application.name=eureka-my-service
#端口号
server.port=2222
#在注册中心进行注册
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:1111/eureka/

运行项目失败,在控制台发现以下信息:

Invocation of destroy method failed on bean with name 'scopedTarget.eurekaClient': org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'eurekaInstanceConfigBean': Singleton bean creation not allowed while singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!)

因为Eureka client不包含tomcat依赖,spring容器无法创建一些实例,导致项目无法启动,需要在pom文件里面加上依赖

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>

启动项目成功,再次访问 http://127.0.0.1:1111/

Spring Cloud Eureka实现服务治理

页面上可以看到eureka-my-service服务,表示服务注册成功

 

在MyServiceServer项目中,新增业务类DispatcherController:

package com.tansky.cloud.controller;

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

@RestController
public class DispatcherController {

    @RequestMapping("/test")
    public String test(){
        return "my service";
    }
}

重启项目后访问http://127.0.0.1:2222/test,页面显示my service表示成功

Spring Cloud Eureka实现服务治理

 

三、创建客户端项目MyServiceClient

Spring Cloud Eureka实现服务治理

Next,选择依赖的类库Eureka Discovery和Feign,Next,Finish

Spring Cloud Eureka实现服务治理

项目创建完成,在启动类上增加注解@EnableDiscoveryClient和@EnableFeignClients,声明可以注册和进行远程服务调用

Spring Cloud Eureka实现服务治理

在application.properties配置文件中增加配置:

spring.application.name=eureka-my-client
server.port=80
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:1111/eureka/

同样不要忘记在pom文件里面加上依赖

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>

在MyServiceClient项目中,添加服务访问类MyClient

package com.tansky.cloud.client;

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

@FeignClient("eureka-my-service")
public interface MyClient {

    @GetMapping("/test")
    String test();

}

@FeignClient(“eureka-my-service”)括号中的字符串要与在注册中心注册的名称一致eureka-my-service

 

在MyServiceClient项目中,添加业务类DispatcherController调用服务访问类MyClient:

package com.tansky.cloud.controller;

import com.tansky.cloud.client.MyClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DispatcherController {

    @Autowired
    private MyClient myClient;

    @RequestMapping("/test")
    public String test(){
        return myClient.test();
    }
}

 

运行项目,访问http://127.0.0.1:3333/test,页面显示my service,表示客户端访问服务成功

Spring Cloud Eureka实现服务治理