1、整体思路
1.1、服务注册中心Eureka(可以是一个集群,对外暴露自己的地址)
1.2、服务提供者:启动后向Eureka注册自己的信息(地址,提供什么服务)
1.3、客户端消费者:向Eureka订阅服务,Eureka会将服务的所有提供者地址列表发送给消费者,并且定期更新
2、代码实现
2.1 创建父工程cloud-demo,导入依赖
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.myx.demo</groupId> 8 <artifactId>cloud-demo</artifactId> 9 <version>1.0.0-SNAPSHOT</version> 10 <modules> 11 <module>user-service</module> 12 <module>user-consumer</module> 13 <module>eureka-server</module> 14 </modules> 15 16 <packaging>pom</packaging> 17 18 <parent> 19 <groupId>org.springframework.boot</groupId> 20 <artifactId>spring-boot-starter-parent</artifactId> 21 <version>2.0.4.RELEASE</version> 22 <relativePath/> 23 </parent> 24 25 <properties> 26 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 27 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 28 <java.version>1.8</java.version> 29 <spring-cloud.version>Finchley.SR1</spring-cloud.version> 30 <mybatis.starter.version>1.3.2</mybatis.starter.version> 31 <mapper.starter.version>2.0.2</mapper.starter.version> 32 <druid.starter.version>1.1.9</druid.starter.version> 33 <mysql.version>5.1.32</mysql.version> 34 <pageHelper.starter.version>1.2.3</pageHelper.starter.version> 35 <leyou.latest.version>1.0.0-SNAPSHOT</leyou.latest.version> 36 </properties> 37 38 <dependencyManagement> 39 <dependencies> 40 <!-- springCloud --> 41 <dependency> 42 <groupId>org.springframework.cloud</groupId> 43 <artifactId>spring-cloud-dependencies</artifactId> 44 <version>${spring-cloud.version}</version> 45 <type>pom</type> 46 <scope>import</scope> 47 </dependency> 48 <!-- mybatis启动器 --> 49 <dependency> 50 <groupId>org.mybatis.spring.boot</groupId> 51 <artifactId>mybatis-spring-boot-starter</artifactId> 52 <version>${mybatis.starter.version}</version> 53 </dependency> 54 <!-- 通用Mapper启动器 --> 55 <dependency> 56 <groupId>tk.mybatis</groupId> 57 <artifactId>mapper-spring-boot-starter</artifactId> 58 <version>${mapper.starter.version}</version> 59 </dependency> 60 <!-- 分页助手启动器 --> 61 <dependency> 62 <groupId>com.github.pagehelper</groupId> 63 <artifactId>pagehelper-spring-boot-starter</artifactId> 64 <version>${pageHelper.starter.version}</version> 65 </dependency> 66 <!-- mysql驱动 --> 67 <dependency> 68 <groupId>mysql</groupId> 69 <artifactId>mysql-connector-java</artifactId> 70 <version>${mysql.version}</version> 71 </dependency> 72 </dependencies> 73 </dependencyManagement> 74 75 <build> 76 <plugins> 77 <plugin> 78 <groupId>org.springframework.boot</groupId> 79 <artifactId>spring-boot-maven-plugin</artifactId> 80 </plugin> 81 </plugins> 82 </build> 83 </project>
2.2 编写Eureka-server
1)导入依赖
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <parent> 6 <artifactId>cloud-demo</artifactId> 7 <groupId>com.myx.demo</groupId> 8 <version>1.0.0-SNAPSHOT</version> 9 </parent> 10 <modelVersion>4.0.0</modelVersion> 11 12 <groupId>com.myx.demo</groupId> 13 <artifactId>eureka-server</artifactId> 14 15 <dependencies> 16 <dependency> 17 <groupId>org.springframework.cloud</groupId> 18 <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> 19 </dependency> 20 </dependencies> 21 </project>
2)编写启动类
1 @SpringBootApplication 2 @EnableEurekaServer // 声明这个应用是一个EurekaServer 3 public class EurekaDemo { 4 public static void main(String[] args) { 5 SpringApplication.run(EurekaDemo.class); 6 } 7 }
3)编写配置 application.yml
1 server: 2 port: 10086 3 4 spring: 5 application: 6 name: eureka-server # 应用名称,会在Eureka中显示 7 8 eureka: 9 client: 10 register-with-eureka: false # 是否注册自己的信息到EurekaServer,默认是true 11 fetch-registry: false # 是否拉取其它服务的信息,默认是true 12 service-url: # EurekaServer的地址,现在是自己的地址,如果是集群,需要加上其它Server的地址。 13 defaultZone: http://127.0.0.1:${server.port}/eureka
2.3 创建user-service,并将其注册到Eureka
1)导入依赖
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <parent> 6 <artifactId>cloud-demo</artifactId> 7 <groupId>com.myx.demo</groupId> 8 <version>1.0.0-SNAPSHOT</version> 9 </parent> 10 <modelVersion>4.0.0</modelVersion> 11 12 <groupId>com.myx.demo</groupId> 13 <artifactId>user-service</artifactId> 14 15 <dependencies> 16 <dependency> 17 <groupId>org.springframework.boot</groupId> 18 <artifactId>spring-boot-starter-web</artifactId> 19 </dependency> 20 <dependency> 21 <groupId>org.mybatis.spring.boot</groupId> 22 <artifactId>mybatis-spring-boot-starter</artifactId> 23 </dependency> 24 <dependency> 25 <groupId>mysql</groupId> 26 <artifactId>mysql-connector-java</artifactId> 27 </dependency> 28 <dependency> 29 <groupId>tk.mybatis</groupId> 30 <artifactId>mapper-spring-boot-starter</artifactId> 31 </dependency> 32 <!-- Eureka客户端 --> 33 <dependency> 34 <groupId>org.springframework.cloud</groupId> 35 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> 36 </dependency> 37 </dependencies> 38 39 </project>
2)、编写配置 application.yml
1 server: 2 port: 8080 3 spring: 4 datasource: 5 url: jdbc:mysql://localhost:3306/day11mybatis 6 password: 123 7 username: root 8 application: 9 name: user-service #应用名称 10 eureka: 11 client: 12 service-url: # EurekaServer的地址,现在是自己的地址,如果是集群,需要加上其它Server的地址。 13 defaultZone: http://127.0.0.1:10086/eureka 14 instance: 15 prefer-ip-address: true # 当调用getHostname获取实例的hostname时,返回ip而不是host名称 16 ip-address: 127.0.0.1 # 指定自己的ip信息,不指定的话会自己寻找 17 18 19 logging: 20 level: 21 com.myx.demo: 22 debug
3)编写启动类并开启Eureka客户端功能
1 @SpringBootApplication 2 @MapperScan("com.myx.demo.mapper") 3 @EnableDiscoveryClient //开启EurekaClient 4 public class UserApplication { 5 public static void main(String[] args) { 6 SpringApplication.run(UserApplication.class); 7 } 8 }
4)实体类
1 @Table(name = "tb_user") 2 public class User { 3 @Id 4 @GeneratedValue(generator = "JDBC") 5 private Long id; 6 // 用户名 7 private String userName; 8 // 密码 9 private String password; 10 // 姓名 11 private String name; 12 // 年龄 13 private Integer age; 14 // 性别,1男性,2女性 15 private Integer sex; 16 // 出生日期 17 private Date birthday; 18 // 创建时间 19 private Date created; 20 // 更新时间 21 private Date updated; 22 //get、set方法... 23 }
5)controller
1 @RestController 2 @RequestMapping("user") 3 public class UserController { 4 @Autowired 5 private UserService userService; 6 7 @GetMapping("/{id}") 8 public User queryById(@PathVariable("id") Long id) { 9 return this.userService.queryById(id); 10 } 11 }
6) service
1 @Service 2 public class UserService { 3 @Autowired 4 private UserMapper userMapper; 5 public User queryById(Long id){ 6 return this.userMapper.selectByPrimaryKey(id); 7 } 8 }
7)mapper接口
1 public interface UserMapper extends Mapper<User>{ }
2.4创建user-consumer,并让其获取Eureka服务
1)导入依赖
1 <dependencies> 2 <dependency> 3 <groupId>org.springframework.boot</groupId> 4 <artifactId>spring-boot-starter-web</artifactId> 5 </dependency> 6 <!-- 添加OkHttp支持 --> 7 <dependency> 8 <groupId>com.squareup.okhttp3</groupId> 9 <artifactId>okhttp</artifactId> 10 <version>3.9.0</version> 11 </dependency> 12 13 <dependency> 14 <groupId>org.springframework.cloud</groupId> 15 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> 16 </dependency> 17 </dependencies>
2)编写application.yml
1 server: 2 port: 8081 3 spring: 4 application: 5 name: user-consumer # 应用名称 6 7 8 eureka: 9 client: 10 service-url: # EurekaServer地址 11 defaultZone: http://127.0.0.1:10086/eureka 12 instance: 13 prefer-ip-address: true # 当其它服务获取地址时提供ip而不是hostname 14 ip-address: 127.0.0.1 # 指定自己的ip信息,不指定的话会自己寻找 15 16 logging: 17 level: 18 com.myx.demo: 19 debug
3)编写启动类并开启Eureka客户端
1 @SpringBootApplication 2 @EnableDiscoveryClient //开启Eureka客户端 3 public class UserConsumer { 4 public static void main(String[] args) { 5 SpringApplication.run(UserConsumer.class); 6 } 7 8 //此时restTemplate的实现就是okHttp3 9 @Bean 10 public RestTemplate restTemplate(){ 11 // 这次我们使用了OkHttp客户端,只需要注入工厂即可 12 return new RestTemplate(new OkHttp3ClientHttpRequestFactory()); 13 } 14 }
4)service
1 @Service 2 public class UserService { 3 @Autowired 4 private RestTemplate restTemplate; 5 @Autowired 6 private DiscoveryClient discoveryClient; 7 public List<User> queryUsersByIds(List<Long> ids) { 8 List<User> userList = new ArrayList<>(); 9 10 //根据服务器的名称获取所有的服务实例 11 List<ServiceInstance> instances = discoveryClient.getInstances("user-service"); 12 //因为只有一个UserService,因此我们直接get(0)获取 13 ServiceInstance instance = instances.get(0); 14 //获取主机名 15 String host = instance.getHost(); 16 //获取端口号 17 int port = instance.getPort(); 18 for (Long id : ids) { 19 User user = restTemplate.getForObject("http://"+host+":"+port+"/user/" + id, User.class); 20 userList.add(user); 21 } 22 return userList; 23 }
5)mapper
1 @RestController 2 @RequestMapping("yun9") 3 public class UserMapper { 4 5 @Autowired 6 private UserService userService; 7 8 @GetMapping("users") 9 public List<User> queryUsersByIds(@RequestParam("ids") List<Long> ids){ 10 List<User> userList = userService.queryUsersByIds(ids); 11 if (userList != null){ 12 return userList; 13 } 14 15 return null; 16 17 }
/*Spring提供了一个RestTemplate模板工具类,对基于Http的客户端进行了封装,并且实现了对象与json的序列化和反序列化,非常方便。RestTemplate并没有限定Http的客户端类型,
而是进行了抽象,目前常用的3种都有支持: - HttpClient - OkHttp - JDK原生的URLConnection(默认的)*/