接着上一节写着走,微服务的原理就是将一个系统拆分为更细致的各个模块,比如认证是个单独的服务,文件上传也可以是个单独的服务...然后注册到Eureka服务中心,这样,各个服务都可以互相调用,如下图
新建springcloud_auth服务
和之前一样,照猫画虎
此时的父工程又多了一个auth子模块,然后再新建启动类,测试接口,pom, application配置文件:
代码:分别是:
package com.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.client.SpringCloudApplication;
@SpringCloudApplication
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
public class AuthServer {
public static void main(String[] args) {
SpringApplication.run(AuthServer.class, args);
}
}
package com.springcloud.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/auth/")
public class AuthController {
@GetMapping("getAuthInfo")
public String getAuthInfo() {
return "这里是Auth认证服务!";
}
}
pom.xml
<?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">
<parent>
<artifactId>springcloud_parent</artifactId>
<groupId>com.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud_auth</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!--注意,这里引入的是Euraka客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
application.yml
server:
port: 8003
spring:
application:
name: api-auth
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8001/eureka/
同时启动euraka服务和auth服务
再看Eureka服务中心,已经将auth服务注册到了Eureka
然后我们访问一下接口:/auth/getAuthInfo,成功!
后面,我会集成JWT作为认证的票据!