这一章节主要讲如何搭建eureka-client项目.
在我们生成的Demo项目上右键点击New->Module->spring Initializr, 然后next, 填写Group和Artifact等信息,
这里Artifact填写eurekaclient, 再次next,
这里选择的Web下的web和Cloud Discovery下的Eureka Discovery.
最后在Module Name中填写eureka-client.
生成的pom.xml文件如下:
4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.3.RELEASE com.xum eureka-client 0.0.1-SNAPSHOT eureka-client Demo project for Spring Boot UTF-8 UTF-8 1.8 Greenwich.RELEASE org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin spring-milestones Spring Milestones https://repo.spring.io/milestone
然后在EurekaClientApplication上加@EnableEurekaClient注解:
package com.xum.eurekaclient;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@EnableEurekaClient@SpringBootApplicationpublic class EurekaClientApplication { public static void main(String[] args) { SpringApplication.run(EurekaClientApplication.class, args); }}
然后新建application.yml文件, 内容如下:
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ 这里的defaultZone是上一章节的对应eureka-serverserver: port: 8762spring: application: name: eureka-client
然后写一个controller去获取数据, 这里是获取git上的数据, 关于config-server的项目下一章节说
package com.xum.eurekaclient.controller;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.cloud.client.ServiceInstance;import org.springframework.cloud.client.discovery.DiscoveryClient;import org.springframework.cloud.context.config.annotation.RefreshScope;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import java.lang.reflect.Method;import java.util.List;@RestController@RequestMapping(value = "/testone")public class TestOneController { private static final Logger LOG = LoggerFactory.getLogger(TestOneController.class); @Autowired private DiscoveryClient discoveryClient; @Value("${server.port}") String port; /* from git config */ @Value("${democonfigclient.message}") String message; /* from git config */ @Value("${foo}") String foo; @RequestMapping(value = "/test") public String test(@RequestParam(value = "name", required = false, defaultValue = "testOneClient")String name) { /*Listlist = discoveryClient.getInstances("eureka-client"); LOG.info("discoveryClient.getServices().size() = " + discoveryClient.getServices().size()); for( String s : discoveryClient.getServices()){ List serviceInstances = discoveryClient.getInstances(s); for(ServiceInstance si : serviceInstances){ LOG.info("services:" + s + ":getHost()=" + si.getHost()); LOG.info("services:" + s + ":getPort()=" + si.getPort()); LOG.info("services:" + s + ":getServiceId()=" + si.getServiceId()); LOG.info("services:" + s + ":getUri()=" + si.getUri()); } }*/ String info = "Hi " + name + ", this is EurekaClient, port is " + port; return info; } @RequestMapping(value = "/config", method = RequestMethod.GET) public String config() { String info = "port:" + port + ",message:" + message + ",foo:" + foo; return info; }}
最后通过Run DashBoard运行项目,
首先运行上一章节的eureka-server项目, 然后运行eureka-client的项目.
在浏览器上输入http://localhost:8761,显示如下: (我这里同时运行了config-server项目, 这个项目下一章节讲)
先在浏览器中输入http://localhost:8762/testone/test, 显示如下
port是eureka-client的端口
然后在浏览器中输入http://localhost:8762/testone/config, 显示如下
port是eureka-client的端口, message和foo是从git仓库中获取的, 是通过config-server项目获取的.
下一章节讲config-server项目的搭建.