一、创建Eureka注册中心
1. 创建项目Eureka_0720
2.导入依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR9</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
3.在项目上新建模块EurkaServer
4.导入依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
5.编写启动器
@EnableEurekaServer
@SpringBootApplication
public class EurekaServer7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaServer7001.class, args);
System.out.println("注册中心启动成功!");
}
}
6.书写yml配置
server:
port: 7001
eureka:
instance:
hostname: localhost #主机名
client:
register-with-eureka: false #1、不注册自己(不做服务提供者)
fetch-registry: false #2、不获取配置(不做消费者)
service-url: #指定注册中心地址,配置集群逗号分隔即可
defaultZone: http://localhost:7001/eureka
7.访问 http://localhost:7001/
二、创建服务提供者
1.在项目上新建模块Server
2.导入依赖
<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>
</dependencies>
3.编写启动器
@EnableEurekaClient
@SpringBootApplication
public class Server8001 {
public static void main(String[] args) {
SpringApplication.run(Server8001.class, args);
}
}
4.书写yml配置
server:
port: 8001
spring:
application:
name: cloud-payment-service #给服务起个名字
eureka:
client:
register-with-eureka: true #向注册中心注册自己
fetch-registry: true #可以获取注册信息
service-url:
defaultZone: http://localhost:7001/eureka
5.提供一个外部接口然后测试
@RestController
@RequestMapping("/test")
public class TestController {
@Value("${server.port}")
private String port;
@RequestMapping("/test1")
public String test1(){
return String.format("/port==>%s,service ok",port);
}
}
6.先启动注册中心,然后启动服务提供者,然后查看注册中心能看到服务提供者即为成功
7.测试外部接口是否正常
8.复制多个sever模拟分布式部署
9.找到 VM options,不同版本界面可能不相同,添加 -Dserver.port配置端口
10.启动多个server
三、创建消费者
1.新建模块Client
2.导入依赖
<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>
</dependencies>
3.编辑启动器
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class Client80 {
public static void main(String[] args) {
SpringApplication.run(Client80.class, args);
}
}
4.熟悉yml配置
server:
port: 80
spring:
application:
name: cloud-order-service
eureka:
client:
register-with-eureka: true #向注册中心注册自己
fetch-registry: true #可以获取注册信息
service-url:
defaultZone: http://localhost:7001/eureka
5.RestTemplate配置
在配置类中添加Bean声明即可:
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class Client80 {
public static void main(String[] args) {
SpringApplication.run(Client80.class, args);
}
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
6.通过注册中心拉取服务提供者列表 进行随机访问
@RestController
public class TestController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private DiscoveryClient discoveryClient;
@GetMapping("/test")
public String test() {
List<ServiceInstance> serverList = discoveryClient.getInstances("cloud-payment-service");
System.out.println(serverList);
int index = new Random().nextInt(serverList.size());
ServiceInstance serviceInstance = serverList.get(index);
String url = String.format("http://%s:%s/test/get", serviceInstance.getHost(), serviceInstance.getPort());
return restTemplate.getForObject(url, String.class);
}
}
server中
@RestController
@RequestMapping("/test")
public class TestController {
@Value("${server.port}")
private String port;
@RequestMapping("/test1")
public String test1(){
return String.format("/port==>%s,service ok",port);
}
@RequestMapping("/get")
public String test() {
return port;
}
}
7.测试
访问消费者的test接口
多次刷新,访问的提供者是随机的,可以通过端口判断