独角兽企业重金招聘Python工程师标准>>>
用 Spring Cloud 子项目Spring Cloud Netflix
新建Spring Boot项目,引入 eureka-server,pom.xml
<?xml version="1.0" encoding="UTF-8"?><project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.forwy</groupId><artifactId>eureka-server</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>eureka-server</name><description>eureka-server project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.6.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><spring-cloud.version>Dalston.SR3</spring-cloud.version></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka-server</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
给启动类加上@EnableEurekaServer 注解
@EnableEurekaServer@SpringBootApplicationpublic class EurekaApplication {public static void main(String[] args) {SpringApplication.run(EurekaApplication.class, args);}}
禁用此工程注册自己
server.port=9760#eureka.instance.hostname=localhosteureka.client.register-with-eureka=falseeureka.client.fetch-registry=falseeureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
启动项目,访问http://127.0.0.1:9760
此时并没有发现任何服务。
新建一个服务用于测试
新建 Spring Boot 项目,引入 spring-cloud-starter-eureka,pom.xml
<?xml version="1.0" encoding="UTF-8"?><project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.forwy</groupId><artifactId>eureka-client</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>eureka-client</name><description>eureka-client project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.6.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><spring-cloud.version>Dalston.SR3</spring-cloud.version></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper</artifactId><version>4.1.0</version></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
配置文件
spring.application.name=eureka-clientserver.port=9761eureka.client.serviceUrl.defaultZone=http://localhost:9760/eureka/
给启动类加上@EnableDiscoveryClient 注解,并写一个 rest controller 服务
package com.forwy.eurekaclient.controller;import com.forwy.eurekaclient.dto.ResponseData;import org.apache.log4j.Logger;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.cloud.client.ServiceInstance;import org.springframework.cloud.client.discovery.DiscoveryClient;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.util.Arrays;@RestControllerpublic class EurekaController {private final Logger log = Logger.getLogger(getClass());@Autowiredprivate DiscoveryClient client;@RequestMapping(value = "/eurekaTest" ,method = RequestMethod.GET)public ResponseData eurekaTest(@RequestParam String w, @RequestParam String y) {ServiceInstance si = client.getLocalServiceInstance();log.info("the host is : "+ si.getHost() + " ,the port is : " + si.getPort() + ", the servier id is : " + si.getServiceId());String v = w + "&" + y;return new ResponseData(Arrays.asList(v));}}
启动client,并刷新http://localhost:9760/
服务已经注册上了。
如果觉得《Spring Cloud 服务注册与发现 [ eureka ]》对你有帮助,请点赞、收藏,并留下你的观点哦!