springCloud的服务注册与发现(一)
一、 构建一个空的Maven工程,其他模块放在下面
整体模块是这样的
二、 搭建服务注册中心服务注册中心 (eureka-server)
1、 在Maven新建一个springBoot项目
2、 加入Eureka Server组件
3、 修改启动类EurekaServerApplication.java,添加@EnableEurekaServer
在默认情况下,服务注册中心也会把自己当做是一个服务,将自己注册进服务注册中心,所以我们可以通过配置来禁用他的客户端注册行为,修改application.properties文件
spring.application.name=eureka-server
#服务注册中心端口号
server.port=8080
#服务注册中心实例的主机名
eureka.instance.hostname=localhost
#关闭自我保护
eureka.server.enableSelfPreservation=false
# 续期时间,即扫描失效服务的间隔时间(缺省为60*1000ms)
eureka.server.eviction-interval-timer-in-ms: 1000
#是否向服务注册中心注册自己
eureka.client.register-with-eureka=false
#是否检索服务
eureka.client.fetch-registry=false
#服务注册中心的配置内容,指定服务注册中心的位置
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
4、 启动应用,并访问http://localhost:8080/即可看到Eureka信息面板,如下:
三、 创建并注册服务提供者 Eureka Client
1 、 创建方式如eureka-server模块类似;在选择组件的时候需要选择对应的组件
注意要选择Web组件或者其它能够持久运行的。不然会注册失败
2、 启动类EurekaClientApplication.java添加@EnableDiscoveryClient注解以实现Eureka中的DiscoveryClient实现。
package com.chenghu.eurekaclient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
}
@EnableEurekaClient和@EnableDiscoveryClient的区别spring cloud中discovery service有许多种实现(eureka、consul、zookeeper等等),@EnableDiscoveryClient基于spring-cloud-commons,@EnableEurekaClient基于spring-cloud-netflix。就是如果选用的注册中心是eureka,那么就推荐@EnableEurekaClient,如果是其他的注册中心,那么推荐使用@EnableDiscoveryClient。
3、 修改配置文件application.properties
spring.application.name=eureka-client
server.port=8090
eureka.client.serviceUrl.defaultZone=http://localhost:8080/eureka/
测试是否能够注册
1、 先启动:eureka-server
2、然后启动eureka-client
发现多了一个EUREKA-CLIENT,说明注册成功了
四、 服务的简单调用
1、 简单的创建一个项目
导入依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>
2、 配置文件
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.application.name=eureka-student
server.port=8091
eureka.client.serviceUrl.defaultZone=http://localhost:8080/eureka/
3、 配置启动类
package com.chenghu.eurekastudent;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@MapperScan("com.chenghu.eurekastudent.mapper")
@EnableEurekaClient
//@EnableDiscoveryClient
/*第二步*/
@EnableFeignClients
public class EurekaStudentApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaStudentApplication.class, args);
}
}
到这里基本配置完了,其余就是简单的一个后台操作了
4、 如果要调用其他服务的方法
1、 创建一个client的包
2、 创建一个接口,通过@FeignClient注解关联
package com.chenghu.eurekastudent.client;
import com.chenghu.eurekastudent.pojo.Grade;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.List;
@FeignClient("eureka-grade")
public interface GradeClient {
@GetMapping("/grades")
public List<Grade> findStudents();
}