Spring_cloud 父工程 eureka-server注册管理服务 userService 服务端 模拟的消费
Spring_cloud 父工程
1.引入坐标
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
</parent>
<properties> 版本锁定
<spring-cloud>Finchley.SR1</spring-cloud>
<mapper-spring>2.0.3</mapper-spring>
<mysql-connector>8.0.16</mysql-connector>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>${mapper-spring}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
eureka-server注册管理服务
1.依赖坐标 版本使用父工程管理
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
2.启动器 @EnableEurekaServer 开启注册服务 注解
@EnableEurekaServer
@SpringBootApplication
public class EurekaServer {
public static void main(String[] args) {
SpringApplication.run(EurekaServer.class);
}}
3. application.yaml 配置文件
加入有多个 注册服务 端口为 10086, 10087 , 10088 那么每一个都要 注册其他两个 交叉注册
server:
port: 10087
spring:
application:
name: eureka-server
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka
#register-with-eureka: false #不注册自己
# instance:
# ip-address: 127.0.0.1
# prefer-ip-address: true 使服务列表为本身IP地址 127.0.0.1
server:
eviction-interval-timer-in-ms: 30000 # 剔除 过期的心跳 周期 ms
#enable-self-preservation: true 自我保护 机制默认开启
userService 服务端
1. 需要依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2. application.yaml 配置文件 加入有两个 注册管理服务 那么 客服端 就需要都往他们那注册
server:
port: 8083
spring:
application:
name: user-server
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test
username: root
password: 123
mybatis:
type-aliases-package: com.wyc.domain
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka/,http://127.0.0.1:10087/eureka/
instance:
prefer-ip-address: true
ip-address: 127.0.0.1
lease-renewal-interval-in-seconds: 30 #每隔30s发一次请求 ,心跳续约
lease-expiration-duration-in-seconds: 90 #最大过期时长
3.提供接口的服务端启动器 @EnableDiscoveryClient 开启客服端 提供api 接口 注解
@SpringBootApplication
@MapperScan("com.wyc.mapper")
@EnableDiscoveryClient
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class);
}}
4. 真正实现API接口的 web控制器
@RestController
@RequestMapping("user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/{id}")
public User hello(@PathVariable("id") Integer id) {
return userService.findOne(id);
}}
模拟的消费者
1. pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2. 启动器 RestTemplate 一个可以发送http协议的
@SpringBootApplication
@EnableDiscoveryClient
public class ConsumerApplication {
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class);
}
}
3.application.yaml 配置 配置往哪里的 注册管理服务 拉取 列表 同时也是注册自己
server:
port: 8082
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka/,http://127.0.0.1:10087/eureka/
registry-fetch-interval-seconds: 30 #拉取间隔
fetch-registry: true #拉取列表,默认开启
spring:
application:
name: consumer-server
4. 1.discoveryClient.getInstances 拉取列表 2.instances.get(0) 获得 列表里的第一个 获得实例本身
@RestController
@RequestMapping("consumer")
public class UserController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private DiscoveryClient discoveryClient;
@GetMapping("/{id}")
public User hello(@PathVariable("id") Integer id) {
List<ServiceInstance> instances = discoveryClient.getInstances("user-server");
ServiceInstance serviceInstance = instances.get(0);
String url="http://"+ serviceInstance.getHost()+":"+serviceInstance.getPort()+"/user/"+id;
User user = restTemplate.getForObject(url, User.class); 发起请求
return user;
}
}
消费者实现负载均衡 ribbon
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
1. 负载均衡算法 随机, 轮询 , hash(根据ip地址) client.choose 获取所有列表 默认以轮询 方式使用列表中的实例
@RestController
@RequestMapping("consumer")
public class UserController {
@Autowired
private RestTemplate restTemplate;
private RibbonLoadBalancerClient client;
@GetMapping("/{id}")
public User hello(@PathVariable("id") Integer id) {
ServiceInstance serviceInstance = client.choose("user-server"); 实现轮询
String url="http://"+ serviceInstance.getHost()+":"+serviceInstance.getPort()+"/user/"+id;
User user = restTemplate.getForObject(url, User.class);
return user;
}
}
2.第二种方式
1.在 启动类上的RestTemplate 方法上 加上 @LoadBalanced 相当于一个拦截器 底层与上面一样
@SpringBootApplication
@EnableDiscoveryClient
public class ConsumerApplication {
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class);
}
}
2. url 地址 直接写成 String url = "http://user-server/user/"+id;
会自动根据 user-server 获取拉取 所有列表中的一个实例 并拼凑成 完整请求url 轮询方式
@RestController
@RequestMapping("consumer")
public class UserController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/{id}")
public User hello(@PathVariable("id") Integer id) {
serviceInstance.getHost()+":"+serviceInstance.getPort()+"/user/"+id;
String url = "http://user-server/user/"+id;
User user = restTemplate.getForObject(url, User.class);
return user;
}
}
3. 修改 负载均衡算法 修改为 随机算法
server:
port: 8082
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka/ #,http://127.0.0.1:10087/eureka/
registry-fetch-interval-seconds: 30 #拉取间隔
fetch-registry: true #拉取列表,默认开启
spring:
application:
name: consumer-server
user-service:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule