SpringBoot集成SpringCloud
(1)、新建一个普通Maven项目,用于存放一些公共服务接口及公共的Bean等。
项目结构:
公共Bean:
1 package cn.coreqi.entities; 2 3 import java.io.Serializable; 4 5 public class User implements Serializable { 6 private Integer id; 7 private String userName; 8 private String passWord; 9 private Integer enabled; 10 11 public User() { 12 } 13 14 public User(Integer id, String userName, String passWord, Integer enabled) { 15 this.id = id; 16 this.userName = userName; 17 this.passWord = passWord; 18 this.enabled = enabled; 19 } 20 21 public Integer getId() { 22 return id; 23 } 24 25 public void setId(Integer id) { 26 this.id = id; 27 } 28 29 public String getUserName() { 30 return userName; 31 } 32 33 public void setUserName(String userName) { 34 this.userName = userName; 35 } 36 37 public String getPassWord() { 38 return passWord; 39 } 40 41 public void setPassWord(String passWord) { 42 this.passWord = passWord; 43 } 44 45 public Integer getEnabled() { 46 return enabled; 47 } 48 49 public void setEnabled(Integer enabled) { 50 this.enabled = enabled; 51 } 52 53 @Override 54 public String toString() { 55 return "User{" + 56 "id=" + id + 57 ", userName='" + userName + '\'' + 58 ", passWord='" + passWord + '\'' + 59 ", enabled=" + enabled + 60 '}'; 61 } 62 }
公共服务接口:
1 package cn.coreqi.service; 2 3 import cn.coreqi.entities.User; 4 5 import java.util.List; 6 7 public interface UserService { 8 public void addUser(User user); 9 public void delById(Integer id); 10 public void modifyUser(User user); 11 public User getById(Integer id); 12 public List<User> getList(); 13 }
(2)、新建SpringBoot项目用作与Eureka注册中心
1)、导入依赖(或者在SpringBoot初始化向导中勾选cloud Discovery =》 Eureka Server)
1 <dependency> 2 <groupId>org.springframework.cloud</groupId> 3 <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> 4 </dependency>
2)、配置Eureka Server相关信息(二选一)
①properties
1 server.port=8761 2 #eureka.instance.hostname Eureka服务端实例名称 3 eureka.instance.hostname=eureka-server 4 #eureka.client.register-with-eureka 是否向注册中心注册自己 5 eureka.client.register-with-eureka=false 6 #eureka.client.fetch-registry 是否从Eureka上获取服务的注册信息,自己就是注册中心,本身职责就是维护服务实例,并不需要去检索服务 7 eureka.client.fetch-registry=false 8 #eureka.client.service-url.defaultZone 设置与Eureka Server交互的地址(查询服务、注册服务等) 9 eureka.client.service-url.defaultZone=http://localhost:${server.port}/eureka/
②yml
1 server: 2 port: 8761 3 eureka: 4 instance: 5 hostname: eureka-server #Eureka服务端实例名称 6 client: 7 register-with-eureka: false #是否向注册中心注册自己 8 fetch-registry: false #是否从Eureka上获取服务的注册信息,自己就是注册中心,本身职责就是维护服务实例,并不需要去检索服务 9 service-url: 10 defaultZone: http://localhost:${server.port}/eureka/ #设置与Eureka Server交互的地址(查询服务、注册服务等)
3)、在主程序类上添加@EnableEurekaServer注解启用注册中心功能
1 package cn.coreqi; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; 6 7 @SpringBootApplication 8 @EnableEurekaServer //启用Eureka注册中心功能 9 public class SpringbootcloudregistryApplication { 10 11 public static void main(String[] args) { 12 SpringApplication.run(SpringbootcloudregistryApplication.class, args); 13 } 14 15 }
4)、启动项目后访问http://ip:port查看服务启动情况
(3)、新建SpringBoot项目用作与服务提供者
1)、导入依赖(或者在SpringBoot初始化向导中勾选cloud Discovery =》 Eureka Discovery,注意还要导入公共项目)
1 <dependency> 2 <groupId>org.springframework.cloud</groupId> 3 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> 4 </dependency> 5 <dependency> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-web</artifactId> 8 </dependency> 9 <dependency> 10 <groupId>cn.coreqi</groupId> 11 <artifactId>springbootcloudapi</artifactId> 12 <version>1.0-SNAPSHOT</version> 13 </dependency>
2)、配置Eureka相关信息(二选一)
①properties
1 server.port=8001 2 3 spring.application.name=user-provider 4 5 #eureka.instance.prefer-ip-address 注册服务的时候使用服务的ip地址 6 eureka.instance.prefer-ip-address=true 7 8 eureka.client.service-url.defaultZone:http://localhost:8761/eureka/
②yml
1 server: 2 port: 8001 3 spring: 4 application: 5 name: user-provider 6 eureka: 7 instance: 8 prefer-ip-address: true #注册服务的时候使用服务的ip地址 9 client: 10 service-url: 11 defaultZone: http://localhost:8761/eureka/
3)、编写服务及控制器等
1 package cn.coreqi.service.impl; 2 3 import cn.coreqi.entities.User; 4 import cn.coreqi.service.UserService; 5 import org.springframework.stereotype.Service; 6 7 import java.util.ArrayList; 8 import java.util.List; 9 10 @Service 11 public class UserServiceImpl implements UserService { 12 private static List<User> users = new ArrayList<>(); 13 static { 14 users.add(new User(1,"fanqi","123456",1)); 15 users.add(new User(2,"zhangsan","123456",1)); 16 users.add(new User(3,"lisi","123456",1)); 17 users.add(new User(4,"wangwu","123456",1)); 18 } 19 @Override 20 public void addUser(User user) { 21 users.add(user); 22 } 23 24 @Override 25 public void delById(Integer id) { 26 for (User s:users){ 27 if(s.getId() == id){ 28 users.remove(s); 29 break; 30 } 31 } 32 } 33 34 @Override 35 public void modifyUser(User user) { 36 delById(user.getId()); 37 addUser(user); 38 } 39 40 @Override 41 public User getById(Integer id) { 42 for (User s:users){ 43 if(s.getId() == id){ 44 return s; 45 } 46 } 47 return null; 48 } 49 50 @Override 51 public List<User> getList() { 52 return users; 53 } 54 }
1 package cn.coreqi.controller; 2 3 import cn.coreqi.entities.User; 4 import cn.coreqi.service.UserService; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.web.bind.annotation.GetMapping; 7 import org.springframework.web.bind.annotation.RestController; 8 9 import java.util.List; 10 11 @RestController 12 public class UserController { 13 @Autowired 14 private UserService userService; 15 16 @GetMapping("/users") 17 public List<User> getUsers(){ 18 return userService.getList(); 19 } 20 }
4)、在主程序类上添加@EnableEurekaClient注解启动Eureka客户端功能,并启动项目
1 package cn.coreqi; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 6 7 @SpringBootApplication 8 @EnableEurekaClient ////启用Eureka客户端功能 9 public class SpringbootcloudserviceproviderApplication { 10 11 public static void main(String[] args) { 12 SpringApplication.run(SpringbootcloudserviceproviderApplication.class, args); 13 } 14 15 }
(4)、新建SpringBoot项目用作与服务消费者
1)导入依赖(或者在SpringBoot初始化向导中勾选cloud Discovery =》 Eureka Discovery,注意还要导入公共项目)=》同服务提供者一致,此处略
2)配置Eureka相关信息(二选一)
①properties
1 server.port=8200 2 3 spring.application.name=user-consumer 4 5 #eureka.instance.prefer-ip-address 注册服务的时候使用服务的ip地址 6 eureka.instance.prefer-ip-address=true 7 8 eureka.client.service-url.defaultZone:http://localhost:8761/eureka/
②yml
1 server: 2 port: 8200 3 spring: 4 application: 5 name: user-consumer 6 eureka: 7 instance: 8 prefer-ip-address: true #注册服务的时候使用服务的ip地址 9 client: 10 service-url: 11 defaultZone: http://localhost:8761/eureka/
3)在主程序上添加@EnableDiscoveryClient注解,开启发现服务功能,并在容器中添加RestTemplate。
1 package cn.coreqi; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 import org.springframework.cloud.client.loadbalancer.LoadBalanced; 7 import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 8 import org.springframework.context.annotation.Bean; 9 import org.springframework.web.client.RestOperations; 10 import org.springframework.web.client.RestTemplate; 11 12 @SpringBootApplication 13 @EnableDiscoveryClient //开启服务发现功能 14 public class SpringbootcloudserviceconsumerApplication { 15 16 public static void main(String[] args) { 17 SpringApplication.run(SpringbootcloudserviceconsumerApplication.class, args); 18 } 19 20 @Bean 21 @LoadBalanced //使用负载均衡机制 22 public RestOperations restTemplate(){ 23 return new RestTemplate(); 24 } 25 26 }
4)、在控制器中注入RestTemplate并调用远程服务
1 package cn.coreqi.controller; 2 3 import cn.coreqi.entities.User; 4 import com.fasterxml.jackson.core.type.TypeReference; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Controller; 7 import org.springframework.web.bind.annotation.GetMapping; 8 import org.springframework.web.bind.annotation.RestController; 9 import org.springframework.web.client.RestOperations; 10 11 import java.util.ArrayList; 12 import java.util.List; 13 14 @RestController 15 public class UserController { 16 @Autowired 17 private RestOperations restTemplate; 18 @GetMapping("/userno1") 19 public User getUsersFirst(){ 20 User[] users = restTemplate.getForObject("http://user-provider/users",User[].class); 21 return users[0]; 22 } 23 }