eureka-client 是一个springboot服务,
会完成相应的业务模块功能的时候,
也会与eureka的注册中心进行心跳通信
1、springboot的主类
package com.springcloudtest.eurekaclient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
}
2、pom.xml文件中添加eureka-client的maven依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- feign依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
3、application.yml文件中添加eureka的注册中心的地址
将当前的服务注册到eureka的注册中心上
还要写上当前服务的名称
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8181/eureka
spring:
application:
name: eureka-client-goods
server:
port: 8183
4、IndexController提供查询所有商品信息的功能
package com.springcloudtest.eurekaclient.controller;
import com.springcloudtest.eurekaclient.domain.GoodsFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/queryGoods")
public class IndexController {
@Autowired
public GoodsFactory goodsFactory;
private static Logger logger = LoggerFactory.getLogger(IndexController.class);
@ResponseBody
@RequestMapping(value = "/allGoods", method = RequestMethod.GET)
public Object allGoods() {
return goodsFactory.getGoodsMap();
}
}
4、启动当前服务,会到当前服务在中心进行注册
可以看到当前,有两个Springboot服务注册到了eureka的注册中心上面
一个服务的名称是eureka-client-goods
另外一个服务的名称是eureka-client-orders