三个模块需要的 pom的代码块
//spring boot 的版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>
//cloud 的版本
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
注册中心 pom代码
<dependencies>
//spring-cloud 的依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
需要一个 application.yml 文件
#端口
server:
port: 端口
eureka:
instance:
hostname: localhost
client:
#是否注册到注册中心,注册中心自己不需要注册
registerWithEureka: false
#是否抓取注册中心的注册信息
fetchRegistry: false
#是微服务和客服端用来注册和发现的地址
serviceUrl:
defaultZone: http://地址:端口/eureka/
需要一个main
@SpringBootApplication
@EnableEurekaServer
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}
service 模块的pom
//spring-cloud 的依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
//mysql 驱动
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
//spring 集成mybatis boot
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
// spring jpa 组件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
//小辣椒 依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.6</version>
<scope>provided</scope>
</dependency>
</dependencies>
他的main
@SpringBootApplication
@EnableEurekaClient
@RestController
public class UserMain {
public static void main(String[] args) {
new SpringApplicationBuilder(UserMain.class).web(true).run(args);
}
}
他的一个contoller小例子
@RestController
public class contoaller {
@Autowired
private Usemap user;
@PostMapping(value="/valueUser")
//开启传参
public Result valueUser(@RequestBody studnt stu){
Result ps=new Result();
List<Map> myquery = user.myquery(stu.getId(), stu.getAge());
if(myquery.size() == 0){
ps.setCode(1);
ps.setMsg("输入错误");
}
return ps;
}
}
他的application.yml 文件
#同上
server:
port: 8792
eureka:
instance:
prefer-ip-address: true
hostname: localhost
client:
registerWithEureka: true
fetch-registry: true
serviceUrl:
defaultZone: http://localhost:8761/eureka/
spring:
application:
#设置名字
name: userservice
datasource:
#四要素
url: jdbc:mysql://localhost/unit02
username: root
password: ps123456
driver-class-name: com.mysql.jdbc.Driver
useis (客户端)
pom 的依赖
<dependencies>
// spring cloud 的组件
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
//小辣椒依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.6</version>
<scope>provided</scope>
</dependency>
</dependencies>
注 他的html文件在 resources 目录下 要一个 static || public 目录
resources的目录 下要一个 application.yml 文件
#同上
server:
port: 80
eureka:
instance:
prefer-ip-address: true
hostname: localhost
client:
registerWithEureka: true
fetch-registry: true
serviceUrl:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: uiseId
他的main
@SpringBootApplication
@EnableEurekaClient
@RestController
public class IsMain {
//开启反向代理
@LoadBalanced
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
new SpringApplicationBuilder(IsMain.class).web(true).run(args);
}
}
contoller 的一个例子
//返回路径
@Controller
public class contoallers {
//反向代理方法
@Autowired
private RestTemplate restTemplate;
@PostMapping(value="/users")
public String valueUser(studnt stu){
String url="http://USERSERVICE/valueUser";
//跳转路径
ResponseEntity<Result> resultResponseEntity = restTemplate.postForEntity(url, stu, Result.class);
Result body = resultResponseEntity.getBody();
if(body.getCode() == 1){
return "redirect:/login.html";
}else{
return "redirect:/opst.html";
}
}
}
(完)