创建更服务测试项目
本文需要对IDEA有一定的了解,清楚IDEA中项目与文件的创建,后续迭代补充基础知识
前期准备
- 通过IDEA创建module dsz-demo
Spring Cloud版本选型
- Greenwich SR2
- Spring Boot 2.1.6.RELEASE
- Spring 5.1.8.RELEASE
- jdk 1.8.0_172
最终展示pom.xml和项目结构
项目结构
pom.xml
dsz-root
com.dsz.platform
1.0-SNAPSHOT
4.0.0
dsz-demo
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-openfeign
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-starter-web
application.yml
server:
port: 9080
spring:
application:
name: dsz-demo
eureka:
instance:
hostname: 127.0.0.1
## 心跳间隔-5秒
lease-renewal-interval-in-seconds: 5
## 没有心跳的淘汰时间-10秒
lease-expiration-duration-in-seconds: 10
client:
## 刷新本地缓存-5秒
registry-fetch-interval-seconds: 5
service-url:
defaultZone: http://admin:admin@${eureka.instance.hostname}:8761/eureka/
com.dsz.base.demo.DemoApplication
package com.dsz.base.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
测试dsz-gateway请求类
package com.dsz.base.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RouterController {
@RequestMapping("/info")
public String getServiceUrl() {
System.out.println("getRouter");
return "getRouter";
}
}
测试dsz-security使用
package com.dsz.base.demo.controller;
import com.dsz.base.demo.api.OAuthApi;
import com.dsz.base.demo.vo.TokenInfo;
import com.dsz.base.demo.vo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
@RestController
public class SecurityController {
@Autowired
OAuthApi oAuthApi;
@GetMapping("/load/user")
UserInfo loadUserByUsername(@RequestParam(value = "username") String username) {
UserInfo userInfo = new UserInfo();
userInfo.setUsername(username);
userInfo.setPassword("$2a$10$fbOCOr20fUQhZnnIGbz9vOeWbJTN8fD4B07hqL/mO8tgkgi1i/IYC");
List list = new ArrayList<>();
list.add("admin");
list.add("query");
list.add("ROLE_TRUSTED_CLIENT");
userInfo.setGrantedAuthorities(list);
return userInfo;
}
@GetMapping("/load/auth")
public TokenInfo loadToken(@RequestParam("code") String code) {
MultiValueMap map = new LinkedMultiValueMap<>();
map.add("client_id", "demo-client");
map.add("client_secret", "123456");
map.add("grant_type", "authorization_code");
map.add("scope","all");
map.add("code", code);
TokenInfo tokenInfo = oAuthApi.token(map);
return tokenInfo;
}
@GetMapping("/load/refresh")
public TokenInfo refreshToken(@RequestParam("token") String refreshToken) {
MultiValueMap map = new LinkedMultiValueMap<>();
map.add("client_id", "demo-client");
map.add("client_secret", "123456");
map.add("grant_type", "refresh_token");
map.add("refresh_token", refreshToken);
TokenInfo tokenInfo = oAuthApi.token(map);
return tokenInfo;
}
}
请求dsz-security使用的Fegin API
package com.dsz.base.demo.api;
import com.dsz.base.demo.vo.TokenInfo;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
@FeignClient(name = "dsz-security")
public interface OAuthApi {
@PostMapping(value = "/oauth/token", headers = {"Content-Type: multipart/form-data"})
TokenInfo token(@RequestBody MultiValueMap map);
@PostMapping(value = "/oauth/check_token")
Object checkToke(@RequestBody MultiValueMap map);
}
用户信息Vo
package com.dsz.base.demo.vo;
import java.util.List;
public class UserInfo {
private String username;
private String password;
private List grantedAuthorities;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public List getGrantedAuthorities() {
return grantedAuthorities;
}
public void setGrantedAuthorities(List grantedAuthorities) {
this.grantedAuthorities = grantedAuthorities;
}
}
token返回信息vo
package com.dsz.base.demo.vo;
public class TokenInfo {
private String access_token;
private String refresh_token;
private String token_type;
private Integer expires_in;
private String scope;
public Integer getExpires_in() {
return expires_in;
}
public void setExpires_in(Integer expires_in) {
this.expires_in = expires_in;
}
public String getAccess_token() {
return access_token;
}
public void setAccess_token(String access_token) {
this.access_token = access_token;
}
public String getRefresh_token() {
return refresh_token;
}
public void setRefresh_token(String refresh_token) {
this.refresh_token = refresh_token;
}
public String getScope() {
return scope;
}
public void setScope(String scope) {
this.scope = scope;
}
public String getToken_type() {
return token_type;
}
public void setToken_type(String token_type) {
this.token_type = token_type;
}
}