Spring Cloud - 服务发现、注册、调用
- Spring Cloud - 服务发现、注册、调用(一)(eureka server)
- Spring Cloud - 服务发现、注册、调用(二)(eureka client(provider))
- Spring Cloud - 服务发现、注册、调用(三)(eureka client(consumer))
代码参见https://github.com/happut/springbootdemo
eureka client (provider)
既然有了服务注册的服务器,那么咱们就可以进行服务注册了,实际上服务注册服务器相当于一个中介,提供者将服务提供到注册服务器中,消费者只需要跟注册服务器打交道,不用硬编码具体的某个提供者。
建立springboot项目,增加spring-cloud-starter-eureka-server
起步依赖:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.happut</groupId>
<artifactId>springboot-demo-eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot-demo-eureka-server</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.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>Edgware.RELEASE</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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</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>
启动代码中增加@EnableEurekaClient
@SpringBootApplication
@EnableEurekaClient
public class SpringbootDemoEurekaProviderApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootDemoEurekaProviderApplication.class, args);
}
}
application.properties
(yml请注意缩进)
server.port=1112
spring.application.name=hello-service-provider
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka
这里需要注意下端口不要和主服务器冲突,否则无法启动。
spring.application.name
代表服务名称,前面说到,消费者无需硬编码接口服务器的ip、端口等,而需要依赖服务名称,这个等到搭建服务消费者的时候会提到。
eureka.client.serviceUrl.defaultZone
务必要和主服务器一致,否则你也注册不上去啊。
在这里为了演示,我们临时写一个接口:
@RestController
public class HelloController {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@RequestMapping("/hello")
public String index() {
return "ok";
}
}
然后乐呵启动,我们可以简单测试下接口:
顺便看下主服务器中的变化:
可以看到我们的服务,已经被主服务器发现了。
服务名称就是刚才我们在application.properties中的设置