本文学习资料来源于蚂蚁课堂。
一、项目结构
项目依赖:只需在根目录的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>springboot</groupId>
<artifactId>springboot</artifactId>
<packaging>pom</packaging>
<version>1.0</version>
<modules>
<module>kk-app</module>
<module>kk-heros-service</module>
<module>kk-story-service</module>
<module>kk-region-service</module>
<module>kk-heros-service-9100</module>
<module>kk-heros-service-7100</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<!-- 管理依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.M7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 管理依赖 -->
<dependencies>
<!--SpringCloud eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- SpringBoot整合Web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- SpringBoot整合eureka客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<!-- 注意: 这里必须要添加, 否者各种依赖有问题 -->
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
注册中心:
kk-heros-serive kk-heros-serive-7100 kk-heros-serive-9100
3个注册中心yml配置文件只是,端口不一样,其他都一样,defaultZone为其他两个注册中心的访问地址
# 服务端口号
server:
port: 8100
#eureka 基本信息配置
eureka:
instance:
###注册到eurekaip地址
hostname: 127.0.0.1
client:
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:7100/eureka/,http://${eureka.instance.hostname}:9100/eureka/
#因为自己是为注册中心,不需要自己注册自己(集群需要为true)
register-with-eureka: true
fetch-registry: true
spring:
application:
name: kk-heros-server
服务提供者:
kk-story-server
# 服务端口号
server:
port: 8000
#eureka 基本信息配置
eureka:
instance:
###注册到eurekaip地址
hostname: 127.0.0.1
client:
serviceUrl:
#注册到eureka的服务地址
defaultZone: http://${eureka.instance.hostname}:8100/eureka/,http://${eureka.instance.hostname}:9100/eureka/,http://${eureka.instance.hostname}:7100/eureka/
#需要将我的服务注册到eureka上
register-with-eureka: true
#需要检索服务
fetch-registry: true
spring:
application:
name: kk-story-server
服务消费者:
kk-region-server
# 服务端口号
server:
port: 8001
#eureka 基本信息配置
eureka:
instance:
###注册到eurekaip地址
hostname: 127.0.0.1
client:
serviceUrl:
#注册到eureka的服务地址
defaultZone: http://${eureka.instance.hostname}:8100/eureka/,http://${eureka.instance.hostname}:9100/eureka/,http://${eureka.instance.hostname}:7100/eureka/
#需要将我的服务注册到eureka上
register-with-eureka: true
#需要检索服务
fetch-registry: true
spring:
application:
name: kk-region-server
二、代码
注册中心:另两个注册中心一样
package com.google;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* @author wk
* @Description:注册中心
* @date 2019/11/26 12:00
**/
@EnableEurekaServer
@SpringBootApplication
public class HerosEureka {
public static void main(String[] args) {
SpringApplication.run(HerosEureka.class, args);
}
}
服务提供者:
package com.google.api.controller;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @author wk
* @Description:服务提供者
* @date 2019/11/26 12:00
**/
@EnableEurekaClient
@SpringBootApplication
public class StoryEureka {
public static void main(String[] args) {
SpringApplication.run(StoryEureka.class, args);
}
}
package com.google.api.controller.service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author wk
* @Description:
* @date 2019/11/26 13:55
**/
@RestController
public class MessageController {
@RequestMapping(value = "/getMessage")
public String getMessage() {
return "盖伦来自德玛西亚";
}
}
服务消费者:
package com.google.api.controller;
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;
/**
* @author wk
* @Description:消费者
* @date 2019/11/26 16:46
**/
@EnableEurekaClient
@SpringBootApplication
public class RegionEureka {
public static void main(String[] args) {
SpringApplication.run(RegionEureka.class, args);
}
/**
* @LoadBalanced就能让这个RestTemplate在请求时拥有客户端负载均衡的能力
*/
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
package com.google.api.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* @author wk
* @Description:
* @date 2019/11/26 16:50
**/
@RestController
public class RegionController {
/**
* restTemplate底层是httpClient
*/
@Autowired
private RestTemplate restTemplate;
@RequestMapping(value = "/getRegionMessage")
public String getRegionMessage() {
return restTemplate.getForObject("http://kk-story-server/getMessage", String.class);
}
}
三、效果演示