spring cloud Eureka (服务注册与发现中心)在spring cloud大家庭中就扮演了服务注册与发现的角色。
应用:
1、引入依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
<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>Camden.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2、添加application.yml配置文件:
server: port: 8020
client:
register-with-eureka: false #是否将eureka自身作为应用注册到eureka注册中心
fetch-registry: false #为true时,可以启动,但报异常:Cannot execute request on any known server
service-url:
defaultZone: http://user:password@localhost:8020/eureka
3、在启动类上添加注解@EnableEurekaServer:
@EnableEurekaServer
@SpringBootApplication
@EnableFeignClients
public class MovCenterBootstrap {
public static void main(String[] args) {
SpringApplication.run(MovCenterBootstrap.class, args);
}
}
4、运行main方法,访问http://localhost:8020/
如果访问正常就表示Eureka server已经正常启动。那接下来就开始client客户端,即服务的编写,将每个服务注册到这个server中心。
5、创建Eureka服务提供方:
1》引入pom文件:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2》添加application.yml配置文件:
server:port: 8010
#spring
spring: application: name: emos-user-center
eureka:
client:
serviceUrl:
defaultZone: http://user:password@localhost:8020/eureka #注册 中心已经开启认证
instance:
prefer-ip-address: true
instanceId: ${spring.application.name}:${spring.application.instance_id:${server.port}}
3》启动服务类:
@SpringBootApplication
@EnableEurekaClient //启动EnableEureka客户端
@RestController
public class UserCenterBootstrap {
@GetMapping("/hello/{name}")
public String hello(@PathVariable String name) {
System.out.println(name + " welcome . My is microservice provider user");
return name + " welcome . My is microservice provider user";
}
public static void main(String[] args) {
SpringApplication.run(UserCenterBootstrap.class, args);
}
}
4》启动服务,刷新http://localhost:8020/ 服务注册面板,会看到8010端口的服务注册到了Eureka server端,显示在了server面板页面。
EMOS-USER-CENTER | n/a (1) | (1) | UP (1) - emos-user-center:8010 |
此时如果将服务提供端的端口改为8040,并重启服务提供端的服务,启动完之后刷新服务注册中心,这时会显示有两个服务注册到服务注册中心:
EMOS-USER-CENTER | n/a (2) | (2) | UP (2) - emos-user-center:8040 , emos-user-center:8010 |