1,创建springCloud_server项目
2,导入依赖
<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>
<groupId>com.shawemou.spring</groupId>
<artifactId>springboot</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
</parent>
<!-- 导入Spring Cloud的依赖管理 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!--数据校验,自己做的form表单后台校验 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<!-- 提供前端 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- restTemplate封装的okhttp -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
</dependency>
<!-- 注册到eureka服务需要这个依赖,除了eureka本身服务,排除jackson-dataformat-xml的jar包,因为远程调用 这个jar包会吧json转为xml -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- jsp标签库 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- 添加 tomcat的支持,当时为解析jsp用到的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- 可以解析jsp,里面有跳转页面,所以配置 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!-- servlet 依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<!-- 资源文件拷贝插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- java编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
3,编写application.yml文件
这里需要注意的是,在springboot中如果有跳转到jsp页面的话,springboot要改为web项目,并且jsp只能放在WEB-INF内部
server:
port: 8083
spring:
application:
name: springcloud_orderServer #指定自己提供服务的名字,用于client调用
#这是一个错误示例,服务名字不能还有下划线_
mvc:
view:
prefix: /WEB-INF/ # 配置视图文件所在位置;
suffix: .jsp # 配置视图文件后缀名;
eureka:
client:
registerWithEureka: true #是否将自己注册到Eureka服务中,默认为true
fetchRegistry: true #是否从Eureka中获取注册信息,默认为true
serviceUrl: #Eureka客户端与Eureka服务端进行交互的地址
defaultZone: http://shawemou:shawemou@127.0.0.1:6868/eureka/ #因为eureka使用了安全校验,所以要加用户和密码
instance:
prefer-ip-address: true #将自己的ip地址注册到Eureka服务中
4,编写入口ServerSpringApplication
@EnableEurekaClient
@SpringBootApplication
public class ServerSpringApplication {
public static void main(String[] args) {
SpringApplication.run(ServerSpringApplication.class, args);
}
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate(new OkHttp3ClientHttpRequestFactory());
}
}
5,这里提一下,里面用到了后台表单校验
User类
public class User {
@NotNull(message="名字不能为null")
private String name;
@NotNull(message="年龄不能为null")
private Integer age;
@Valid
@NotNull
private Cat cat;
Cat类
public class Cat {
@NotNull(message = "猫的名字 不能为null")
private String name;
@NotNull(message = "猫的年龄不能为null")
private Integer age;
Controller
@GetMapping("insert2")
public String insert(@Valid User user,BindingResult result,String i,HttpServletRequest request){
System.out.println(user);
ModelAndView mv = new ModelAndView("NewFile");
if(result.hasErrors()){
// 收集错误信息
List<String> msgs = new ArrayList<>();
for (ObjectError error : result.getAllErrors()) {
System.out.println(error.getDefaultMessage());
msgs.add(error.getDefaultMessage());
}
mv.addObject("msgs",msgs);
request.setAttribute("msgs", msgs);
// 参数有错,响应400
return "NewFile";
}
return "NewFile";
}
这样就将服务注册到eureka中了
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。