一、导入pom包
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/>
</parent>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
<mapper.starter.version>2.1.5</mapper.starter.version>
<mysql.version>8.0.27</mysql.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- springCloud -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- 通用Mapper启动器 -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>${mapper.starter.version}</version>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
二、用案例来测试是否配置成功
2.1导入pom包
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
2.2配置文件
server:
port: 9091
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/springcloud
username: root
password:xxxxxx
application:
name: user-service
mybatis:
type-aliases-package: com.bukaedu.pojo
2.3Pojo
@Data
@Table(name = "tb_user")
public class User {
// id
@Id
@KeySql(useGeneratedKeys = true)
private Long id;
// 用户名
private String userName;
// 密码
private String password;
// 姓名
private String name;
// 年龄
private Integer age;
// 性别,1男性,2女性
private Integer sex;
// 出生日期
private Date birthday;
// 创建时间
private Date created;
// 更新时间
private Date updated;
// 备注
private String note;
// getter和setter省略
}
2.4Mapper
public interface UserMapper extends Mapper<User> {
}
2.5Service
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User queryById(Long id){
return userMapper.selectByPrimaryKey(id);
}
}
2.6Controller
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/{id}")
public User queryById(@PathVariable Long id){
return userService.queryById(id);
}
}
2.7启动类
@SpringBootApplication
@MapperScan("com.bukaedu.user.mapper") //注意包名不要写错
@EnableDiscoveryClient
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class,args);
}
}
该模块结构:
三、创建服务调用上述子模块
3.1导入pom包
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependencies>
3.2Pojo
@Data
public class User {
// id
private Long id;
// 用户名
private String userName;
// 密码
private String password;
// 姓名
private String name;
// 年龄
private Integer age;
// 性别,1男性,2女性
private Integer sex;
// 出生日期
private Date birthday;
// 创建时间
private Date created;
// 更新时间
private Date updated;
// 备注
private String note;
// getter和setter省略
}
3.3Controller
@RestController
@RequestMapping("/consumer")
public class ConsumerController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("{id}")
public User queryById(@PathVariable Long id){
String url="http://localhost:9091/user/"+id;
return restTemplate.getForObject(url,User.class);
}
}
3.4启动类
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class,args);
}
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
该子模块结构:
这样就可以通过访问http:localhost:8080/consumer/userId来调用二的模块。
四、设置Eureka
4.1导入pom包
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
4.2启动类
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class);
}
}
4.3配置文件
server:
port: 10086
spring:
application:
name: eureka-server
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka
register-with-eureka: false
fetch-registry: false
五、开启Eureka对服务提供方,消费方的访问
5.1服务提供方
5.1.1配置文件
在之前的yml文件中增加以下内容:
eureka:
client:
service-url:
defaultZone: http://localhost:10086/eureka
instance:
lease-expiration-duration-in-seconds: 90
lease-renewal-interval-in-seconds: 30
5.1.2启动类
在启动类上边增加如下注解:
@EnableDiscoveryClient
5.2服务消费方
5.2.1配置文件
server:
port: 8080
spring:
application:
name: consumer-demo
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka
5.2.2导入pom包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
5.2.3启动类
增加如下注解:
@EnableDiscoveryClient
5.2.4Controller层
@RestController
@RequestMapping("/consumer")
public class ConsumerController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private DiscoveryClient discoveryClient;
@GetMapping("{id}")
public User queryById(@PathVariable Long id){
String url="http://localhost:9091/user/"+id;
//获取user-service服务
List<ServiceInstance> serviceInstanceList=discoveryClient.getInstances("user-service");
//获取第一个实例
ServiceInstance serviceInstance=serviceInstanceList.get(0);
url="http://"+serviceInstance.getHost()+":"+serviceInstance.getPort()+"/user/"+id;
return restTemplate.getForObject(url,User.class);
}
}
这样通过访问localhost:8080/consumer/userId即可调用Service服务。
六、配置Eureka集群
配置文件:
server:
port: ${port:10086}
spring:
application:
name: eureka-server
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka,http://127.0.0.1:10087/eureka
七、Ribbon负载均衡
7.1设置两个服务提供者
7.2user-service配置文件
7.3服务提供者Usercontroller
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@Value("${server.port}")
private Integer port;
@GetMapping("/{id}")
public User queryById(@PathVariable Long id){
System.out.println(port);
return userService.queryById(id);
}
}
7.3服务消费者启动类Consumerapplication
@SpringBootApplication
@EnableDiscoveryClient
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class,args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
7.4服务消费者Controller
@GetMapping("/{id}")
public User queryById(@PathVariable("id")Long id){
String url="http://user-service/user/"+id;
User user=restTemplate.getForObject(url,User.class);
return user;
}
八、Hystrix
8.1服务消费者导入pom包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
8.2启动类
@SpringCloudApplication
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class,args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
8.3Controller层(单个接口)
@RestController
@RequestMapping("/consumer")
@Slf4j
public class ConsumerController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private DiscoveryClient discoveryClient;
@GetMapping("/{id}")
@HystrixCommand(fallbackMethod = "queryByIdFallback")
public String queryById(@PathVariable Long id){
String url="http://localhost:9091/user/"+id;
String user=restTemplate.getForObject(url,String.class);
return user;
}
public String queryByIdFallback(Long id){
log.error("查询用户信息失败.id:{}",id);
return "网络拥挤!";
}
}
8.4Controller层(整个类)
@RestController
@RequestMapping("/consumer")
@Slf4j
@DefaultProperties(defaultFallback = "defaultFallback")
public class ConsumerController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private DiscoveryClient discoveryClient;
@GetMapping("/{id}")
@HystrixCommand
public String queryById(@PathVariable Long id){
String url="http://localhost:9091/user/"+id;
String user=restTemplate.getForObject(url,String.class);
return user;
}
public String queryByIdFallback(Long id){
log.error("查询用户信息失败.id:{}",id);
return "网络拥挤!";
}
public String defaultFallback(){
return "对不起,网络拥挤!";
}
}