赞
踩
目录
三个生产者 一个消费者,三个生产者需要用mysql+mybatis 三个不同的数据库。
页面输入用户名和密码,提交到后端消费者,消费者传到生产者后端在进行判断,返回登录成功或登录失败到消费者,消费者再放到页面上进行显示。
restTemplate.getForObject 向远程取数据 两个参数url和数据类型
restTemplate.postForObject 向远程服务传参数 三个参数url、参数、返回值类型
@RequestBody 跨域(端口不同)得到对象数据,必须是post提交
- <!DOCTYPE html>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>登录页面</title>
- </head>
- <body>
- <form th:action="@{/loginEntity}" method="post">
- <span th:text="${result}"></span>
- <input type="username" th:placeholder="请输入用户名" name="username"/>
- <input type="password" placeholder="请输入密码" name="password" />
- <input type="submit" th:value="登录"/>
- </form>
-
- </body>
- </html>
- @RequestMapping("/gotoLogin")
- public String gotoLogin() {
- return "login";
- }
- @RequestMapping("/loginEntity")
- public ModelAndView loginEntity(User user) {
- ModelAndView mav = new ModelAndView();
- String result = restTemplate.postForObject("http://PROVIDER/loginProvider", user, String.class);
- mav.addObject("result", result);
- mav.setViewName("login");
- return mav;
- }
因为使用负载均衡,所以不知道第一次回去那个生产者,所以每个生产者都需要写loginProvider。
依赖
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-configuration-processor</artifactId>
- <optional>true</optional>
- </dependency>
- <dependency>
- <groupId>org.example.entity</groupId>
- <artifactId>common</artifactId>
- <version>1.0-SNAPSHOT</version>
- <scope>compile</scope>
- </dependency>
- <dependency>
- <groupId>com.mysql</groupId>
- <artifactId>mysql-connector-j</artifactId>
- </dependency>
- <dependency>
- <groupId>org.mybatis.spring.boot</groupId>
- <artifactId>mybatis-spring-boot-starter</artifactId>
- </dependency>
配置文件
三个生产者每个生产者各使用一个数据库,所以这里以生产者3为例
- server:
- port: 8003
- eureka:
- client:
- service-url:
- defaultZone: http://localhost:7000/eureka/
- spring:
- datasource:
- driver-class-name: com.mysql.cj.jdbc.Driver
- url: jdbc:mysql://localhost:3306/my_sql_3
- username: root
- password: root
- application:
- name: PROVIDER
- mybatis:
- mapper-locations: classpath:/mapper/*.xml
- configuration:
- log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
- type-aliases-package: org.example.entity
控制层
- @RestController
- public class MyController {
- @Autowired
- MyUserService myUserService;
-
- @RequestMapping("/loginProvider")
- public String loginProvider(@RequestBody User user) {
- User user1 = myUserService.getUser(user);
- if(user1!=null){
- return "登录成功,欢迎";
- }else {
- return "登录失败,暂无该用户,请注册";
- }
- }
访问层
- @Mapper
- public interface MyUserDao {
- public userEntity getUser(userEntity user);
- }
实体类
- @Data
- public class userEntity {
- public int id;
- public String username;
- public String password;
- }
实现类
- @Service
- public class MyUserServiceImpl implements MyUserService {
- @Autowired
- MyUserDao myUserDao;
- @Override
- public userEntity getUser(userEntity user) {
- return myUserDao.getUser(user);
- }
- }
mapper
- <mapper namespace="org.example.dao.MyUserDao">
- <select id="getUser" resultType="User">
- select *
- from my_user
- where username = #{username} and password=#{password}
- </select>
- </mapper>
进入gotoLogin页面
登录失败
登录成功
Feign是Spring Cloud提供的声明式、模板化的HTTP生产者,它使得调用远程服务就像调用本地服务一样简单,只需要创建一个接口并添加一个注解即可。
Spring Cloud集成Feign并对其进行了增强,使Feign支持了Spring MVC注解;Feign默认集成了Ribbon,所以Fegin默认就实现了负载均衡的效果。Spring Cloud 常见的集成方式是使用Feign+Ribbon技术来完成服务间远程调用及负载均衡的。
前端传值
后端传值
@FeignClient的属性name:指定FeignClient的名称,如果项目使用了Ribbon,name属性会作为微服务的名称,用于服务发现。
- @Service
- @FeignClient(name = "PROVIDER",fallbackFactory= MyFeignClientFallbackFactory.class)//name=客户端远程服务的名字,fallbackFactory一旦出现错误进入这个类中
- public interface UserService {
-
- @RequestMapping("/loginProvider")
- public String loginProvider(User user);
-
- @RequestMapping("/getuser/{name}")
- public String getuser(@PathVariable("name") String name);
- }
- @RequestMapping("/loginEntity")
- public ModelAndView loginEntity(User user) {
- ModelAndView mav = new ModelAndView();
- // String result = restTemplate.postForObject("http://PROVIDER/loginProvider", user, String.class);
- String result = userService.loginProvider(user);
- mav.addObject("result", result);
- mav.setViewName("login");
- return mav;
- }
最后效果一致。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。