• springcloud (一) 介绍


    开启springcloud 之旅...

    传统单体架构:

    微服务架构:

    每个模块独立运行,就是独立的进程。

    接下来基于springboot 来构建微服务:

        1, 客户端-》订单微服务-》用户微服务

    订单微服务结构:

     

    pom.xml:

    <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.tuling.springcloud</groupId>
    	<artifactId>03-microservice-order</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    	<packaging>jar</packaging>
    
    	<name>03-microservice-order</name>
    	<url>http://maven.apache.org</url>
    
    	<properties>
    		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    		<java.version>1.8</java.version>
    	</properties>
    
    	<parent>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-parent</artifactId>
    		<version>1.5.10.RELEASE</version>
    	</parent>
    	<dependencies>
    		<!-- Spring WEB -->
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-web</artifactId>
    		</dependency>
    		<!-- Test -->
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-test</artifactId>
    			<scope>test</scope>
    		</dependency>
    		<!-- 集成Mybatis -->
    		<dependency>
    			<groupId>org.mybatis.spring.boot</groupId>
    			<artifactId>mybatis-spring-boot-starter</artifactId>
    			<version>1.1.1</version>
    		</dependency>
    		<dependency>
    			<groupId>mysql</groupId>
    			<artifactId>mysql-connector-java</artifactId>
    		</dependency>
    	</dependencies>
    
    	<build>
    		<plugins>
    			<plugin>
    				<groupId>org.springframework.boot</groupId>
    				<artifactId>spring-boot-maven-plugin</artifactId>
    			</plugin>
    		</plugins>
    	</build>
    </project>
    

    OrderController:

    package com.tuling.springcloud.api;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate;
    
    @RestController
    @RequestMapping("/order")
    public class OrderController {
    	private final Logger logger = LoggerFactory.getLogger(OrderController.class);
    	
    	@Autowired
    	private RestTemplate restTemplate;
    	
    	@Value("${user.getUserByIdUrl}")
    	private String userByIdUrl;
    	
    	
        @RequestMapping("/user/getById")
        public Object getUserById(String userId) {
        	String url = userByIdUrl + "?id=" + userId;
        	logger.debug("param userId : {}, request url : {}", userId, url);
        	Object result = restTemplate.getForEntity(url, Object.class);
            return result;
        }
    }

    CommonConfig.java:

    package com.jiagoushi.springcloud.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.client.RestTemplate;
    
    @Configuration
    public class CommonConfig {
    
    	@Bean
    	public RestTemplate restTemplate(){
    		return new RestTemplate();
    	}
    }
    

      

    application.properties

    server.port=8082
    ## db config
    spring.datasource.url=jdbc:mysql://localhost:3306/springcloud
    spring.datasource.username=root
    spring.datasource.password=root
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    
    ## log
    logging.level.root=INFO
    logging.level.org.springframework.web=DEBUG
    logging.level.com.tuling.springcloud=DEBUG
    
    user.getUserByIdUrl=http://localhost:8083/user/getById

    订单application.properties: user.getUserByIdUrl=http://localhost:8083/user/getById 是用户微服务地址


     用户微服务结构:

     

    pom.xml:

    <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.tuling.springcloud</groupId>
    	<artifactId>03-microservice-user</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    	<packaging>jar</packaging>
    
    	<name>03-microservice-user</name>
    	<url>http://maven.apache.org</url>
    
    	<properties>
    		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    		<java.version>1.8</java.version>
    	</properties>
    
    	<parent>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-parent</artifactId>
    		<version>1.5.10.RELEASE</version>
    	</parent>
    	<dependencies>
    		<!-- Spring WEB -->
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-web</artifactId>
    		</dependency>
    		<!-- Test -->
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-test</artifactId>
    			<scope>test</scope>
    		</dependency>
    		<!-- 集成Mybatis -->
    		<dependency>
    			<groupId>org.mybatis.spring.boot</groupId>
    			<artifactId>mybatis-spring-boot-starter</artifactId>
    			<version>1.1.1</version>
    		</dependency>
    		<dependency>
    			<groupId>mysql</groupId>
    			<artifactId>mysql-connector-java</artifactId>
    		</dependency>
    	</dependencies>
    
    	<build>
    		<plugins>
    			<plugin>
    				<groupId>org.springframework.boot</groupId>
    				<artifactId>spring-boot-maven-plugin</artifactId>
    			</plugin>
    		</plugins>
    	</build>
    </project>
    

      UserController.java

    package com.tuling.springcloud.api;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.tuling.springcloud.bean.User;
    import com.tuling.springcloud.service.UserService;
    
    @RestController
    @RequestMapping("/user")
    public class UserController {
    	private final Logger logger = LoggerFactory.getLogger(UserController.class);
    	
    	@Autowired
    	private UserService userService;
    	
        @RequestMapping("/getById")
        public User getUserById(String id) {
        	logger.debug("param id : {}", id);
            return this.userService.findById(Integer.parseInt(id));
        }
    
        
    }
    

      UserService.java:

    package com.tuling.springcloud.service;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.tuling.springcloud.bean.User;
    import com.tuling.springcloud.mapper.UserMapper;
    
    @Service
    public class UserService {
        private final Logger logger = LoggerFactory.getLogger(UserService.class);
        
        @Autowired
        private UserMapper userMapper;
        
        public User findById(Integer id){
            logger.debug("param id : {}" , id);
            User u = this.userMapper.findById(id);
            logger.debug("result name : {}" , u.getName());
            return u;
        }
    }

    UserMapper

    package com.tuling.springcloud.service;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.tuling.springcloud.bean.User;
    import com.tuling.springcloud.mapper.UserMapper;
    
    @Service
    public class UserService {
        private final Logger logger = LoggerFactory.getLogger(UserService.class);
        
        @Autowired
        private UserMapper userMapper;
        
        public User findById(Integer id){
            logger.debug("param id : {}" , id);
            User u = this.userMapper.findById(id);
            logger.debug("result name : {}" , u.getName());
            return u;
        }
    }

    User

    package com.tuling.springcloud.bean;
    public class User {
        private Integer id;
        private String name;
        private Integer age;
        private String address;
        private String phone;
        public Integer getId() {
            return id;
        }
        public void setId(Integer id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public Integer getAge() {
            return age;
        }
        public void setAge(Integer age) {
            this.age = age;
        }
        public String getAddress() {
            return address;
        }
        public void setAddress(String address) {
            this.address = address;
        }
        public String getPhone() {
            return phone;
        }
        public void setPhone(String phone) {
            this.phone = phone;
        }
        
    }

    application.properties

    server.port=8083
    ## db config
    spring.datasource.url=jdbc:mysql://localhost:3306/springcloud
    spring.datasource.username=root
    spring.datasource.password=123456
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    
    ## log
    logging.level.root=INFO
    logging.level.org.springframework.web=DEBUG
    logging.level.com.tuling.springcloud=DEBUG

    order 微服务 通过spring提供的 RestTemplate这个类来调用微服务。

    2个微服务都启动之后,客户端直接访问订单微服务,就可以得到结果。因为订单微服务通过rest调用了用户微服务去查数据库了

    总结:

    1,这次是一个简单的微服务相互之间调用,每个微服务之间都可以有自己独立的数据库,也可没有。

    2,这种微服务之间相互依赖,耦合度强,如何集群的话就不知道调哪个微服务,所以需要eureka来做注册中心

     欢迎来QQ群:592495675 一起研究 java技术,群里技术大佬,还有所有springCloud 学习源代码

  • 相关阅读:
    CSS优化,提高性能的方法有哪些?
    稀疏数组(SparseArray)(Go)
    Go
    Vue 实战-6 rest 重置表单不生效原因
    Go
    Vue 实战-5 批量导出 excel功能
    Vue 实战-4 表格展开行
    Vue 实战-3 vue 中使用watch 监听 el-input属性值
    Vue 实战-2 输入框加搜索图标
    Vue 实战-1 去掉 input [number] 默认增减箭头样式
  • 原文地址:https://www.cnblogs.com/smallFishs/p/10555629.html
Copyright © 2020-2023  润新知