码迷,mamicode.com
首页 > 其他好文 > 详细

Rest微服务构建 案例工程模块

时间:2019-09-08 20:05:05      阅读:73      评论:0      收藏:0      [点我收藏+]

标签:eureka   ddr   jar包   ==   部门   ref   ribbon   resource   默认   

总体介绍

承接着我们的springmvc+mybatis+mysql初级高级课程,以Dept部门模块做一个微服务通用案例
Consumer消费者(Client)通过REST调用Provider提供者(Server)提供的服务

Maven的分包分模块架构复习

一个简单的Maven模块结构是这样的:

springcloud父工程(Project)下初次带着3个子模块(Module)

 

创建pom文件

 

 

技术图片

 

 

 

<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.spring.springcloud</groupId>
  <artifactId>springcloud_parent</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
  
  <properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
		<junit.version>4.12</junit.version>
		<log4j.version>1.2.17</log4j.version>
		<lombok.version>1.16.18</lombok.version>
	</properties>
  
  
  <dependencyManagement>
      <dependencies>
          <!-- springcloud -->
          <dependency>
             <groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Dalston.SR1</version>
				<type>pom</type>
				<scope>import</scope>
          </dependency>
               
           <!-- springboot依赖 -->   
           <dependency>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-dependencies</artifactId>
				<version>1.5.9.RELEASE</version>
				<type>pom</type>
				<scope>import</scope>
		  </dependency>
		  
		  <!-- mysql依赖 -->
		  <dependency>
				<groupId>mysql</groupId>
				<artifactId>mysql-connector-java</artifactId>
				<version>5.0.4</version>
			</dependency>
			
			<!-- druid依赖 -->
			<dependency>
				<groupId>com.alibaba</groupId>
				<artifactId>druid</artifactId>
				<version>1.0.31</version>
			</dependency>
			
			<!-- spring-boot依赖 -->
			<dependency>
				<groupId>org.mybatis.spring.boot</groupId>
				<artifactId>mybatis-spring-boot-starter</artifactId>
				<version>1.3.0</version>
			</dependency>
			
			<!-- logback插件 -->
			<dependency>
                 <groupId>ch.qos.logback</groupId>
                 <artifactId>logback-core</artifactId>
                 <version>1.2.3</version>
            </dependency>

			
			<!-- junit插件 -->
			<dependency>
				<groupId>junit</groupId>
				<artifactId>junit</artifactId>
				<version>${junit.version}</version>
				<scope>test</scope>
			</dependency>
			
			<!-- log4j日志包 -->
			<dependency>
				<groupId>log4j</groupId>
				<artifactId>log4j</artifactId>
				<version>${log4j.version}</version>
			</dependency>
      </dependencies>
  
  
  
  </dependencyManagement>
  
  <build>
      <finalName>springcloud_parent</finalName>
      <resources>
			<resource>
				<directory>src/main/resources</directory>
				<filtering>true</filtering>
			</resource>
		</resources>
		
		<plugins>
		    <plugin>
		      <groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-resources-plugin</artifactId>
				<configuration>
					<delimiters>
						<delimit>$</delimit>
					</delimiters>
				</configuration>
		    </plugin>
	
		</plugins>
  
  
  </build>
  
  <modules>
  	<module>springcloud_api</module>
  	<module>springcloud_provider_dept</module>
  	<module>springcloud_consumer_dept</module>
  	<module>springcloud_eureka</module>
  	<module>springcloud_eureka_7002</module>
  	<module>springcloud_eureka_7003</module>
  </modules>
</project>

  

microservicecloud-api

封装的整体Entity/接口/公共配置等

jar文件

package com.spring.pojo;

import java.io.Serializable;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

//@SuppressWarnings("serial")
@NoArgsConstructor
@Data
@Accessors(chain=true)
public class Dept implements Serializable{
	
	public Long deptno;
	public String dname;
	public String db_source;
	

	public Dept(String dname)
	{
		super();
		this.dname = dname;
	}


	public Dept(Long deptno, String dname, String db_source) {
		super();
		this.deptno = deptno;
		this.dname = dname;
		this.db_source = db_source;
	}
	
}

  

<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>
  <parent>
    <groupId>com.spring.springcloud</groupId>
    <artifactId>springcloud_parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  
  <artifactId>springcloud_api</artifactId>
  
  <dependencies><!-- 当前Module需要用到的jar包,按自己需求添加,如果父类已经包含了,可以不用写版本号 -->
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-feign</artifactId>
		</dependency>
	</dependencies>
  
</project>

  技术图片

 

 

 创建springcloud_provider_dept       微服务提供者

jar项目 pom文件

<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>
  <parent>
    <groupId>com.spring.springcloud</groupId>
    <artifactId>springcloud_parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  
  <properties>
       <version>0.0.1-SNAPSHOT</version>
  </properties>
  
  <artifactId>springcloud_provider_dept</artifactId>
  
  <dependencies>
           <!-- 引用自己定义的api通用包,可以使用Dept部门Entity -->
           <dependency>
                  <groupId>com.spring.springcloud</groupId>
                  <artifactId>springcloud_api</artifactId>
                  <version>${version}</version>
           </dependency>
           
            <!-- actuator监控信息完善 -->
            <dependency>
                   <groupId>org.springframework.boot</groupId>
			       <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
            
            <!-- 将微服务provider侧注册进eureka -->
            <dependency>
                    <groupId>org.springframework.cloud</groupId>
			        <artifactId>spring-cloud-starter-eureka</artifactId>
            </dependency>
            
            <!--  -->
            <dependency>
                   <groupId>org.springframework.cloud</groupId>
			       <artifactId>spring-cloud-starter-config</artifactId>
            </dependency>
            
            <!-- junit插件 -->
            <dependency>
                   <groupId>junit</groupId>
			       <artifactId>junit</artifactId>
            </dependency>
            
             <!-- mysql -->
             <dependency>
                    <groupId>mysql</groupId>
			        <artifactId>mysql-connector-java</artifactId>
             </dependency>
             
             <!-- druid连接池 -->
             <dependency>
                     <groupId>com.alibaba</groupId>
			         <artifactId>druid</artifactId>
             </dependency>
             
              <!-- logback  -->
              <dependency>
                    <groupId>ch.qos.logback</groupId>
			         <artifactId>logback-core</artifactId>
              </dependency>
              
              <!-- mybatis-spring -->
              <dependency>
                     <groupId>org.mybatis.spring.boot</groupId>
			         <artifactId>mybatis-spring-boot-starter</artifactId>
              </dependency>
              
              <!-- spring-jetty -->
              <dependency>
                     <groupId>org.springframework.boot</groupId>
			         <artifactId>spring-boot-starter-jetty</artifactId>
              </dependency>
              
              <!-- spring-starter -->
              <dependency>
                    <groupId>org.springframework.boot</groupId>
			         <artifactId>spring-boot-starter-web</artifactId>
              </dependency>
              
              <!-- spring-starter -->
              <dependency>
                    <groupId>org.springframework.boot</groupId>
			        <artifactId>spring-boot-starter-test</artifactId>
              </dependency>
              
              <!-- 修改后立即生效,热部署 -->
              <dependency>
                     <groupId>org.springframework</groupId>
			         <artifactId>springloaded</artifactId>
              </dependency>
              <dependency>
			         <groupId>org.springframework.boot</groupId>
			          <artifactId>spring-boot-devtools</artifactId>
		       </dependency>
       </dependencies>
  
</project>

  application.yml

server:
  port: 8001
  
mybatis:
  config-location: classpath:mybatis/mybatis.cfg.xml        # mybatis配置文件所在路径
  type-aliases-package: com.spring.pojo    # 所有Entity别名类所在包
  mapper-locations:
  - classpath:mybatis/mapper/**/*.xml                       # mapper映射文件
    
spring:
   application:
    name: spingcloud-dept 
   datasource:
    type: com.alibaba.druid.pool.DruidDataSource            # 当前数据源操作类型
    driver-class-name: org.gjt.mm.mysql.Driver              # mysql驱动包
    url: jdbc:mysql://localhost:3306/cloudDB01              # 数据库名称
    username: root
    password: root
    dbcp2:
      min-idle: 5                                           # 数据库连接池的最小维持连接数
      initial-size: 5                                       # 初始化连接数
      max-total: 5                                          # 最大连接数
      max-wait-millis: 200                                  # 等待连接获取的最大超时时间
      
      
eureka:  
    client: #客户端注册进eureka服务列表内
     service-url:
       # defaultZone: http://localhost:7001/eureka 
        defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
        
instance:
    instance-id: spingcloud-dept:8001 
    prefer-ip-address:  true  #访问路径可以显示IP地址
    
 
info: 
  app.name: springcloud-dept
  company.name: www.leitf.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$

    
          

  mybatis.cfg.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>

	<settings>
		<setting name="cacheEnabled" value="true" /><!-- 二级缓存开启 -->
	</settings>

</configuration>

  DeptMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.spring.dao.DeptDao">

	<select id="findeById" resultType="Dept" parameterType="Long">
	   select deptno,dname,db_source from dept where deptno=#{deptno};
	</select>
     
    <select id="findAll" resultType="Dept">
      select deptno,dname,db_source from dept;
    </select>
    
    <insert id = "addDept" parameterType="Dept">
      insert into dept(dname,db_source) values(#{dname},#{db_source});
    </insert>
</mapper>
 

  DeptDao

package com.spring.dao;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;

import com.spring.pojo.Dept;

@Mapper
public interface DeptDao {

	//添加部门
	public boolean addDept(Dept dept);
	
	//根据id
	public Dept findeById(Long id);
	
	//查询所有
	public List<Dept> findAll();

	
}

  DeptService

package com.spring.service;

import java.util.List;

import com.spring.pojo.Dept;

public interface DeptService {

	    //添加部门
		public boolean add(Dept dept);
		
		//根据id
		public Dept get(Long id);
		
		//查询所有
		public List<Dept> list();
}

  DeptServiceImpl

package com.spring.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.spring.dao.DeptDao;
import com.spring.pojo.Dept;
import com.spring.service.DeptService;

@Service
public class DeptServiceImpl implements DeptService {

	@Autowired
	private DeptDao deptdao;

	@Override
	public boolean add(Dept dept) {
		// TODO Auto-generated method stub
		return deptdao.addDept(dept);
	}

	@Override
	public Dept get(Long id) {
		// TODO Auto-generated method stub
		return deptdao.findeById(id);
	}

	@Override
	public List<Dept> list() {
		// TODO Auto-generated method stub
		System.out.println("==================");
		return deptdao.findAll();
	}
	


}

  DeptController

package com.spring.controller;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.spring.pojo.Dept;
import com.spring.service.DeptService;

@RestController
public class DeptController {

	@Autowired
	private DeptService deptService;
	
	@Autowired
	private DiscoveryClient client;
	
	@RequestMapping(value="/dept/add",method=RequestMethod.POST)
	public boolean add(@RequestBody Dept dept){
		return deptService.add(dept);
	}
	
	@RequestMapping(value="/dept/get/{id}",method=RequestMethod.GET)
	public Dept get(Long id){
		return deptService.get(id);
	}
	
	@RequestMapping(value="/dept/list",method=RequestMethod.GET)
	public List<Dept> list(){
		return deptService.list();
	}
	
	
//	@Autowired
//	private DiscoveryClient client;
	@RequestMapping(value = "/dept/discovery", method = RequestMethod.GET)
	public Object discovery()
	{
		List<String> list = client.getServices();
		System.out.println("**********" + list);

		List<ServiceInstance> srvList = client.getInstances("MICROSERVICECLOUD-DEPT");
		for (ServiceInstance element : srvList) {
			System.out.println(element.getServiceId() + "\t" + element.getHost() + "\t" + element.getPort() + "\t"
					+ element.getUri());
		}
		return this.client;
	}

	
}

 sp

package com.spring;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient //本服务启动后会自动注册到Eureka中
@EnableDiscoveryClient
public class springcloud_dept8001_App {
  public static void main(String[] args){
	  SpringApplication.run(springcloud_dept8001_App.class, args);
  }
}

  

 

ringcloud_consumer_dept   部门消费者

<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>
  <parent>
    <groupId>com.spring.springcloud</groupId>
    <artifactId>springcloud_parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <properties>
       <version>0.0.1-SNAPSHOT</version>
  </properties>
  <artifactId>springcloud_consumer_dept</artifactId>
  	<description>部门微服务消费者</description>
  	
  	
  <dependencies>
        <!-- 引用自己定义的api通用包,可以使用Dept部门Entity -->
           <dependency>
                  <groupId>com.spring.springcloud</groupId>
                  <artifactId>springcloud_api</artifactId>
                  <version>${version}</version>
           </dependency>
         
         <!-- Ribbon相关 -->
         <dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-ribbon</artifactId>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		
		<!-- 修改后立即生效,热部署 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>springloaded</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
		</dependency>
  </dependencies>
</project>

  application.yml

server:
  port: 80

  configBeans

package com.spring;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient //本服务启动后会自动注册到Eureka中
@EnableDiscoveryClient
public class springcloud_dept8001_App {
  public static void main(String[] args){
	  SpringApplication.run(springcloud_dept8001_App.class, args);
  }
}

  controller

package com.spring.controller;

import java.util.List;

import javax.ws.rs.ApplicationPath;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.spring.pojo.Dept;

@RestController
public class DeptController_consumer {

	private static String REST_URL_PREFIX="http://localhost:8001"; 
	@Autowired
	private RestTemplate restTemplate;
	
	@RequestMapping(value="/consumer/dept/add")
	public boolean add(Dept dept){
		return restTemplate.postForObject(REST_URL_PREFIX+"/dept/add", dept, boolean.class);
	}
	
	@RequestMapping(value="/consumer/dept/get/{id}")
	public Dept get(@PathVariable("id") Long id){
		return restTemplate.getForObject(REST_URL_PREFIX+"/dept/get"+id, Dept.class);
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/consumer/dept/list")
    public List<Dept> list(){
		return restTemplate.getForObject(REST_URL_PREFIX+"/dept/list", List.class);
	}
	
	@RequestMapping(value="/consumer/dept/discovery")
	public Object discovery(){
		return restTemplate.getForObject(REST_URL_PREFIX+"/dept/discovery", Object.class);
	}
}

  

package com.spring;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class springcloud_dept80_App {
     
	public static void main(String[] args){
		SpringApplication.run(springcloud_dept80_App.class, args);
	}
}

  

80端口是为HTTP(HyperText Transport Protocol)即超文本传输协议开放的
此为上网冲浪使用次数最多的协议,主要用于WWW(World Wide Web)即万维网传输信息的协议。

可以通过HTTP地址(即常说的"网址")加":80"来访问网站,

因为浏览网页服务默认的端口号都是80,因此只需输入网址即可,不用输入":80"了。

 

Rest微服务构建 案例工程模块

标签:eureka   ddr   jar包   ==   部门   ref   ribbon   resource   默认   

原文地址:https://www.cnblogs.com/jacksonxiao/p/11487862.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!