(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 }