-
搭建环境
eclipse
jdk 1.8
maven 3.5
spring-boot 2.1.4
springcloud 2.1.1 -
创建4个springboot项目,两个作为注册中心,搭建集群,一个作为服务提供者,一个作为服务调用者
-
.注册中心核心依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<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-netflix-eureka-server</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
</dependencies>
- yml文件配置
###服务端口号
server:
port: 8011
#集群服务名称一定要相同
#定义注册中心服务名称
spring:
application:
name: service-hi
eureka:
instance:
####注册中心ip地址,因为我是在本地做的测试,所以是本地ip地址
hostname: 127.0.0.1
client:
#因为是集成所以需要改为true,单机情况下是false
registerWithEureka: true
fetchRegistry: true
serviceUrl:
####注册地址 ${eureka.instance.hostname} 代表注册中心地址, ${server.port}服务端口号
defaultZone: http://${eureka.instance.hostname}:8010/eureka/
5.在启动类上添加@EnableEurekaServer注解
- 创建服务提供者
相关依赖
<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>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestones</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
yml配置文件
server:
port: 8000
spring:
application:
name: service-member
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8010/eureka/,http://localhost:8011/eureka/
package com.example.demo.api;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MemberApiController {
@RequestMapping("/getMember")
public String getMember(){
return "这是springcloud客户端测试";
}
}
服务的调用者配置和此一样,只不过启动类需要使用bean注解,把restTemplate加入spring容器中,否则使用别名调用时会出现问题
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableEurekaClient
public class Order {
public static void main(String[] args) {
SpringApplication.run(Order.class, args);
}
@Bean
@LoadBalanced //解决restTemplate 找不到原因,应该吧restTemplate注册springboot容器中
RestTemplate getrestTemplate() {
return new RestTemplate();
}
}
8.调用接口
package com.example.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class OrderController {
/*
在spring中有两种调用方式rest,fegin(springcloud)
*/
//restTemplate是由springboot web组件提供的
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/getOrder")
public String getOrder(){
//两种方式,一种是采用服务器别名,另一种是直接调用
//restTemplate.getForObject("http://127.0.0.1:8000/getMember",String.class) 直接调用
String url = "http://service/getMember"; //使用别名去注册中心获取对应的服务器调用地址
return restTemplate.getForObject(url,String.class);
}
}
此时配置就完成了,在启动注册中心时会报错,因为使用集群搭建,所以启动时另一个服务可能没有启动,但是不影响正常的调用。