SpringCloud学习-Spring Cloud Eureka
一、搭建服务注册中心
1.使用idea创建springboot项目
添加依赖
2.修改pom.xml文件
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR5</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</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>
3.主类添加@EnableEurekaServer注解
通过@EnableEurekaServer注解启动一个服务注册中心提供给其他的应用进行对话
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
4.配置application.properties文件,添加一下内容
server.port=8761
eureka.client.hostname=localhost
# eureka.client.register-with-eureka=false不向注册中心注册自己
eureka.client.register-with-eureka=false
# eureka.client.fetch-registry=false注册中心的职责是维护服务实例,不需要检索服务,所以设置为false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://${eureka.client.hostname}:${server.port}/eureka/
5.验证
http://localhost:8761/
二、服务提供搭建
1.创建springboot项目,添加依赖
pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>eureka-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-client</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR5</spring-cloud.version>
</properties>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 引入log4j2依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</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>
2.修改application.properties文文件
server.port=8081
spring.application.name=hello-server
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
3.主类添加@EnableDiscoveryClient注解
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
}
4.通过注入DiscoveryClient对象打印相关内容
@RestController
public class HelloControl {
private final Logger logger = Logger.getLogger(String.valueOf(getClass()));
@Autowired
private DiscoveryClient client; // 服务发现客户端
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String test() {
System.out.println(client.getServices().get(0));
return "hello";
}
}
5.查看注册情况
6.访问接口查看服务信息
http://localhost:8081/hello
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cGlAQq7O-1591351635451)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\1591350776205.png)]
三、服务发现与消费
1.创建springboot项目
2. 添加@EnableDiscoveryClient注解让该应用注册为Eureka的客户端应用,以获得服务发现的能力。同时,在主类中创建RestTemplate的SpringBean实例,通过@LoadBalanced注解开启客户端负载均衡。
@EnableDiscoveryClient
@SpringBootApplication
public class RibbonConsumerApplication {
@Bean
// @LoadBalanced 开启客户端负载均衡
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(RibbonConsumerApplication.class, args);
}
}
3.创建ConsumerController类实现/ribbon-consumer接口、通过创建RestTemplate来实现对HELLO-SERVER服务提供的test接口进行调用。访问地址是服务名,符合服务治理的要求。
@RestController
public class CustomerController {
@Autowired
RestTemplate restTemplate;
@RequestMapping(value="/ribbon-consumer",method = RequestMethod.GET)
public String helloConsumer(){
return restTemplate.getForEntity("http://HELLO-SERVER/hello",String.class).getBody();
}
}
4.在application.properties配置Eureka服务注册中心的位置,同时设置消费者的端口为9000,
spring.application.name=ribbon-consumer
server.port=9000
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
5.启动服务后查看eureka,可以看到服务提供方的服务和消费方的服务
6.访问消费者接口测试
http://localhost:9000/ribbon-consumer