src\main\resources目录下新增application.yml文件,内容如下,这是普通的注册中心设置:
spring: application: name: eureka server: port: 8080 eureka: client: service-url: defaultZone: :${server.port}/eureka/ fetch-registry: false register-with-eureka: falsejava文件只有一个,就是启动类,还通过注解EnableEurekaServer开启了注册中心服务:
package com.bolingcavalry.eureka; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); } }
以上就是注册中心eureka的内容,运行EurekaApplication即可启动服务,访问8080端口的结果如下:
现在注册中心已经就绪,开始编写服务提供者provider应用的代码吧。 provider工程
在gatewaydemo下创建一个子工程,名为provider,pom.xml内容如下,可见用到了spring-boot-starter-web和spring-cloud-starter-netflix-eureka-client这两个依赖,分别用来支持web服务和注册发现:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 "> <parent> <artifactId>gatewaydemo</artifactId> <groupId>com.bolingcavalry</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>provider</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> <build> <finalName>${project.artifactId}</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>配置文件application.yml如下,指定了注册中心地址,并且自身端口为8081:
eureka: client: serviceUrl: defaultZone: :8080/eureka/ server: port: 8081 spring: application: name: provider启动类ProviderApplication.java:
package com.bolingcavalry.provider; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); } }增加一个controller,用于响应web请求,注意hello方法会从请求的header中取出名为extendtag的属性值,返回给浏览器:
package com.bolingcavalry.provider.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; import java.text.SimpleDateFormat; import java.util.Date; @RestController @RequestMapping("/hello") public class HelloController { @RequestMapping(value = "time", method = RequestMethod.GET) public String hello(HttpServletRequest request){ return "hello, " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + ", extendtag [" + request.getHeader("extendtag") + "]"; } }