一、Eureka Server
pom
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.liaoxiang.eureka</groupId>
<artifactId>user-eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>user-eureka</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<!--spring-cloud版本-->
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependencies>
<!--服务端eureka依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
UserEurekaApplication
package com.liaoxiang.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class UserEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(UserEurekaApplication.class, args);
}
}
配置文件
server:
port: 10086
spring:
application:
name: user-eureka #微服务名称
eureka:
client:
service-url:
defaultZone: http://localhost:10086/eureka #Eureka服务地址
server:
enable-self-preservation: false # 关闭自我保护模式
eviction-interval-timer-in-ms: 1000 # 失效剔除扫描间隔时间
二、服务提供者
pom
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.liaoxiang.service</groupId>
<artifactId>user-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>user-service</name>
<description>Demo project for Spring Boot</description>
<!--版本属性-->
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<!--依赖的版本管理-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.0.2</version>
</dependency>
<!--引入eureka客户端依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
controller
@Controller
@RequestMapping("user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("findUser/{id}")
@ResponseBody
public User findUserById(@PathVariable("id") Integer id){
return this.userService.findUserById(id);
}
}
mapper
package com.liaoxiang.userservice.mapper;
import com.liaoxiang.userservice.pojo.User;
import tk.mybatis.mapper.common.Mapper;
public interface UserMapper extends Mapper<User> {
}
实体类
package com.liaoxiang.userservice.pojo;
import javax.persistence.*;
import java.util.Date;
/**
* @auther Mr.Liao
* @date 2019/4/26 19:32
*/
@Table(name = "tb_user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String userName;
private String password;
private String name;
private Integer age;
private Integer sex;
private Date birthday;
private Date created;
private Date updated;
//getter setter toString...
}
service
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User findUserById(Integer id){
return this.userMapper.selectByPrimaryKey(id);
}
}
启动类
@SpringBootApplication
@EnableDiscoveryClient
@MapperScan("com.liaoxiang.userservice.mapper")
public class UserServiceApplication {
public static void main(String[] args) {
SpringApplication.run(UserServiceApplication.class, args);
}
}
yml配置文件:
server:
port: 8081
mybatis:
type-aliases-package: com.liaoxiang.userservice.pojo
spring:
datasource:
url: jdbc:mysql://localhost:3306/spring
username: root
password: 123456
application:
name: user-service #作为微服务的名称注册到Eureka Server
eureka:
client:
service-url:
defaultZone: http://localhost:10086/eureka
register-with-eureka: true #服务启动时是否注册到Eureka Server,默认为true
fetch-registry: true #是否从Eureka Server获取服务列表信息,默认为true
instance:
lease-renewal-interval-in-seconds: 5 #续约时间,每5s向注册续约
lease-expiration-duration-in-seconds: 15 #过期时间,超过15s没有续约则会从服务列表中剔除
三、服务消费者
pom
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.liaoxiang.service</groupId>
<artifactId>user-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>user-consumer</name>
<description>Demo project for Spring Boot</description>
<!--版本属性-->
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<!--依赖的版本管理-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--引入eureka-client依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
controller
@RestController
@RequestMapping("user")
public class UserController {
@Autowired
private UserService userService;
//http://localhost:8081/user/findUsers?ids=1,2,3,4
@GetMapping("findUsers")
public List<User> findUser(@RequestParam("ids") List<Integer> ids) {
return this.userService.findUserByIds(ids);
}
}
dao,我们在dao里通过远程调用获取数据,代替直接从数据库获取数据,实体类同上
@Component
public class UserDao {
@Autowired
private RestTemplate restTemplate;
@Autowired
private DiscoveryClient discoveryClient;
public User findUserById(Integer id){
// 获取服务名称为user-service的集群
List<ServiceInstance> instances = discoveryClient.getInstances("user-service");
// 获取第一个服务实例,当获取的实例是多个的时候,需要编写一个负载均衡的算法来使用这些服务实例
ServiceInstance instance = instances.get(0);
String url = "http://" + instance.getHost()+ ":" +instance.getPort() + "/user/findUser/"+id;
return this.restTemplate.getForObject(url, User.class);
}
}
service
@Service
public class UserService {
@Autowired
private UserDao userDao;
public List<User> findUserByIds(List<Integer> ids){
ArrayList<User> users = new ArrayList<>();
for (Integer id : ids) {
User user = this.userDao.findUserById(id);
users.add(user);
}
return users;
}
}
启动类
@SpringBootApplication
@EnableDiscoveryClient
public class UserConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(UserConsumerApplication.class, args);
}
@Bean
public RestTemplate restTemplate() {
return new RestTemplate(new OkHttp3ClientHttpRequestFactory());
}
}
yml配置文件
server:
port: 8082
spring:
application:
name: user-consumer
eureka:
client:
service-url:
defaultZone: http://localhost:10086/eureka
fetch-registry: true #拉取Eureka Server服务的列表只读备份,然后缓存在本地
registry-fetch-interval-seconds: 5 #每隔5秒重新拉取并更新数据,生产环境30
四、启动以上三个服务打开Eureka Server:http://localhost:10086/,可以看到两个服务已经注册成功。
先测试服务提供者,成功获取数据
测试服务消费者,成功获取数据