feat:完成feign服务调用的复习
This commit is contained in:
parent
9f779fbd6a
commit
aa622abeb3
|
|
@ -1,12 +1,22 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Encoding">
|
||||
<file url="file://$PROJECT_DIR$/springcloud-EurekaServer-7001/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/springcloud-EurekaServer-7001/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/springcloud-EurekaServer-7002/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/springcloud-EurekaServer-7002/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/springcloud-EurekaServer-7003/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/springcloud-EurekaServer-7003/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/springcloud-api/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/springcloud-api/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/springcloud-consumer-dept-80/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/springcloud-consumer-dept-80/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/springcloud-provider-dept-8001/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/springcloud-provider-dept-8001/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/springcloud-provider-dept-8002/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/springcloud-provider-dept-8002/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/springcloud-provider-dept-8003/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/springcloud-provider-dept-8003/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
|
||||
</component>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
5
pom.xml
5
pom.xml
|
|
@ -14,8 +14,13 @@
|
|||
<!-- <module>springcloud-eureka-7001</module>-->
|
||||
<module>springcloud-api</module>
|
||||
<module>springcloud-provider-dept-8001</module>
|
||||
<module>springcloud-provider-dept-8002</module>
|
||||
<module>springcloud-provider-dept-8003</module>
|
||||
<module>springcloud-api</module>
|
||||
<module>springcloud-consumer-dept-80</module>
|
||||
<module>springcloud-EurekaServer-7001</module>
|
||||
<module>springcloud-EurekaServer-7002</module>
|
||||
<module>springcloud-EurekaServer-7003</module>
|
||||
</modules>
|
||||
|
||||
<!--打包方式 总项目工程-->
|
||||
|
|
|
|||
|
|
@ -0,0 +1,35 @@
|
|||
<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.learning</groupId>
|
||||
<artifactId>springcloud</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<groupId>org.learning.springcloud</groupId>
|
||||
<artifactId>springcloud-EurekaServer-7001</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>springcloud-EurekaServer</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>3.8.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-eureka-server</artifactId>
|
||||
<version>1.4.6.RELEASE</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
package org.learning.springcloud;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
|
||||
|
||||
/**
|
||||
* Hello world!
|
||||
*
|
||||
*/
|
||||
@EnableEurekaServer
|
||||
@SpringBootApplication
|
||||
public class Eureka7001Application
|
||||
{
|
||||
public static void main( String[] args )
|
||||
{
|
||||
SpringApplication.run(Eureka7001Application.class,args);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
server:
|
||||
port: 7001
|
||||
#eureka配置
|
||||
eureka:
|
||||
instance:
|
||||
hostname: eureka7001.com #Eureka服务端的实例名称
|
||||
client:
|
||||
fetch-registry: false #fetch-registry如果为false,表明自己为注册中心
|
||||
register-with-eureka: false #表示是否向eureka注册中心注册自己
|
||||
service-url:
|
||||
defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
package org.learning.springcloud;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
/**
|
||||
* Unit test for simple App.
|
||||
*/
|
||||
public class AppTest
|
||||
extends TestCase
|
||||
{
|
||||
/**
|
||||
* Create the test case
|
||||
*
|
||||
* @param testName name of the test case
|
||||
*/
|
||||
public AppTest( String testName )
|
||||
{
|
||||
super( testName );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the suite of tests being tested
|
||||
*/
|
||||
public static Test suite()
|
||||
{
|
||||
return new TestSuite( AppTest.class );
|
||||
}
|
||||
|
||||
/**
|
||||
* Rigourous Test :-)
|
||||
*/
|
||||
public void testApp()
|
||||
{
|
||||
assertTrue( true );
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
<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.learning</groupId>
|
||||
<artifactId>springcloud</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<groupId>org.learning.springcloud</groupId>
|
||||
<artifactId>springcloud-EurekaServer-7002</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>springcloud-EurekaServer</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>3.8.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-eureka-server</artifactId>
|
||||
<version>1.4.6.RELEASE</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
package org.learning.springcloud;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
|
||||
|
||||
/**
|
||||
* Hello world!
|
||||
*
|
||||
*/
|
||||
@EnableEurekaServer
|
||||
@SpringBootApplication
|
||||
public class Eureka7002Application
|
||||
{
|
||||
public static void main( String[] args )
|
||||
{
|
||||
SpringApplication.run(Eureka7002Application.class,args);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
server:
|
||||
port: 7002
|
||||
#eureka配置
|
||||
eureka:
|
||||
instance:
|
||||
hostname: eureka7002.com #Eureka服务端的实例名称
|
||||
client:
|
||||
fetch-registry: false #fetch-registry如果为false,表明自己为注册中心
|
||||
register-with-eureka: false #表示是否向eureka注册中心注册自己
|
||||
service-url:
|
||||
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7003.com:7003/eureka/
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
package org.learning.springcloud;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
/**
|
||||
* Unit test for simple App.
|
||||
*/
|
||||
public class AppTest
|
||||
extends TestCase
|
||||
{
|
||||
/**
|
||||
* Create the test case
|
||||
*
|
||||
* @param testName name of the test case
|
||||
*/
|
||||
public AppTest( String testName )
|
||||
{
|
||||
super( testName );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the suite of tests being tested
|
||||
*/
|
||||
public static Test suite()
|
||||
{
|
||||
return new TestSuite( AppTest.class );
|
||||
}
|
||||
|
||||
/**
|
||||
* Rigourous Test :-)
|
||||
*/
|
||||
public void testApp()
|
||||
{
|
||||
assertTrue( true );
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
<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.learning</groupId>
|
||||
<artifactId>springcloud</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<groupId>org.learning.springcloud</groupId>
|
||||
<artifactId>springcloud-EurekaServer-7003</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>springcloud-EurekaServer</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>3.8.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-eureka-server</artifactId>
|
||||
<version>1.4.6.RELEASE</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
package org.learning.springcloud;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
|
||||
|
||||
/**
|
||||
* Hello world!
|
||||
*
|
||||
*/
|
||||
@EnableEurekaServer
|
||||
@SpringBootApplication
|
||||
public class Eureka7003Application
|
||||
{
|
||||
public static void main( String[] args )
|
||||
{
|
||||
SpringApplication.run(Eureka7003Application.class,args);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
server:
|
||||
port: 7003
|
||||
#eureka配置
|
||||
eureka:
|
||||
instance:
|
||||
hostname: eureka7003.com #Eureka服务端的实例名称
|
||||
client:
|
||||
fetch-registry: false #fetch-registry如果为false,表明自己为注册中心
|
||||
register-with-eureka: false #表示是否向eureka注册中心注册自己
|
||||
service-url:
|
||||
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
package org.learning.springcloud;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
/**
|
||||
* Unit test for simple App.
|
||||
*/
|
||||
public class AppTest
|
||||
extends TestCase
|
||||
{
|
||||
/**
|
||||
* Create the test case
|
||||
*
|
||||
* @param testName name of the test case
|
||||
*/
|
||||
public AppTest( String testName )
|
||||
{
|
||||
super( testName );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the suite of tests being tested
|
||||
*/
|
||||
public static Test suite()
|
||||
{
|
||||
return new TestSuite( AppTest.class );
|
||||
}
|
||||
|
||||
/**
|
||||
* Rigourous Test :-)
|
||||
*/
|
||||
public void testApp()
|
||||
{
|
||||
assertTrue( true );
|
||||
}
|
||||
}
|
||||
|
|
@ -24,5 +24,28 @@
|
|||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 在消费者(客户端)集成Ribbon -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-ribbon</artifactId>
|
||||
<version>1.4.6.RELEASE</version>
|
||||
</dependency>
|
||||
<!-- 配置eureka 依赖,因为Ribbon需要从服务注册中心获知哪些服务可用 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-eureka</artifactId>
|
||||
<version>1.4.6.RELEASE</version>
|
||||
</dependency>
|
||||
<!-- 添加Feign依赖 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||
<version>2.2.2.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-netflix-eureka-client</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
|
|||
|
|
@ -1,7 +1,15 @@
|
|||
package org.learning.springcloud;
|
||||
import org.learning.springcloud.config.RibbonConfig;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
|
||||
import org.springframework.cloud.netflix.ribbon.RibbonClient;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableFeignClients
|
||||
@EnableEurekaClient //也作为Eureka的客户端,因为要向注册中心获取服务信息
|
||||
@RibbonClient(name = "springcloud-provider-dept", configuration = RibbonConfig.class)
|
||||
public class ConsumerApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ConsumerApplication.class,args);
|
||||
|
|
|
|||
|
|
@ -1,11 +1,20 @@
|
|||
package org.learning.springcloud.config;
|
||||
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
@Configuration
|
||||
public class BeanConfig {//@Configuration ==>spring applicationContext.xml
|
||||
/**
|
||||
* IRule
|
||||
* RoundRobinRule 轮询
|
||||
* RandomRule 随机
|
||||
* AvailabilityFilteringRule 会先过滤掉跳闸或访问故障的服务,对剩下的进行轮询
|
||||
* RetryRule 会先按照轮询获取服务,如果服务获取失败,则在指定时间内进行重试
|
||||
*/
|
||||
@Bean
|
||||
@LoadBalanced //在注册RestTemplate类时,使用注解,启动Ribbon实现负载均衡(默认轮询)
|
||||
public RestTemplate addRestTemplate(){
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,21 @@
|
|||
package org.learning.springcloud.config;
|
||||
|
||||
import com.netflix.loadbalancer.IRule;
|
||||
import com.netflix.loadbalancer.RoundRobinRule;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class RibbonConfig {
|
||||
/**
|
||||
* IRule
|
||||
* RoundRobinRule 轮询
|
||||
* RandomRule 随机
|
||||
* AvailabilityFilteringRule 会先过滤掉跳闸或访问故障的服务,对剩下的进行轮询
|
||||
* RetryRule 会先按照轮询获取服务,如果服务获取失败,则在指定时间内进行重试
|
||||
*/
|
||||
@Bean
|
||||
public IRule rule(){
|
||||
return new RoundRobinRule();
|
||||
}
|
||||
}
|
||||
|
|
@ -17,8 +17,10 @@ public class DeptConsumerController {
|
|||
@Autowired
|
||||
private RestTemplate restTemplate;
|
||||
|
||||
private static final String REST_URL_PREFIX = "http://localhost:8001";
|
||||
|
||||
// 使用Ribbon时,服务用服务名来访问
|
||||
// 既然是负载均衡,那必然是多台服务器的负载均衡,用IP访问就没有意义了,因此用负载均衡的注解时,不能用IP或者localhost,而应该用服务名。
|
||||
//private static final String REST_URL_PREFIX = "http://localhost:8001";
|
||||
private static final String REST_URL_PREFIX = "http://springcloud-provider-dept";
|
||||
@RequestMapping("/consumer/dept/get/{id}")
|
||||
public Dept get(@PathVariable("id") Long id){
|
||||
return restTemplate.getForObject(REST_URL_PREFIX+"/dept/get/"+id, Dept.class);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,28 @@
|
|||
package org.learning.springcloud.controller;
|
||||
|
||||
import org.learning.springcloud.domain.Dept;
|
||||
import org.learning.springcloud.service.DeptFeignService;
|
||||
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 java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/consumer/dept")
|
||||
public class DeptConsumerFeignController {
|
||||
// 消费者,不应该有service层
|
||||
@Autowired
|
||||
private DeptFeignService deptFeignService;
|
||||
|
||||
@RequestMapping("/feign/get/{id}")
|
||||
public Dept get(@PathVariable("id") Long id){
|
||||
return deptFeignService.findDeptById(id);
|
||||
}
|
||||
|
||||
@RequestMapping("/feign/findAll")
|
||||
public List<Dept> findAll(){
|
||||
return deptFeignService.findAll();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
package org.learning.springcloud.service;
|
||||
|
||||
import org.learning.springcloud.domain.Dept;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
//使用注解的方式来配置,完成对服务提供方的接口绑定
|
||||
@FeignClient(value="SPRINGCLOUD-PROVIDER-DEPT")
|
||||
public interface DeptFeignService {
|
||||
|
||||
@GetMapping("/dept/get/{id}")
|
||||
Dept findDeptById(@PathVariable("id")Long id);
|
||||
|
||||
@GetMapping("/dept/findAll")
|
||||
List<Dept> findAll();
|
||||
}
|
||||
|
|
@ -1,2 +1,9 @@
|
|||
server:
|
||||
port: 80
|
||||
port: 80
|
||||
|
||||
#eureka配置
|
||||
eureka:
|
||||
client:
|
||||
register-with-eureka: false #不向注册中心注册自己
|
||||
service-url:
|
||||
defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7001.com:7001/eureka/,http://eureka7003.com:7003/eureka/
|
||||
|
|
@ -69,5 +69,16 @@
|
|||
<version>1.0-SNAPSHOT</version>
|
||||
<!-- <scope>compile</scope>-->
|
||||
</dependency>
|
||||
|
||||
<!--添加 eureka 服务配置和 actuator 完善监控信息 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-eureka</artifactId>
|
||||
<version>1.4.6.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-actuator</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,19 @@
|
|||
package org.learning.springcloud;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* Hello world!
|
||||
*
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@EnableEurekaClient //在服务启动后自动注册到Eureka中
|
||||
@EnableDiscoveryClient //服务发现
|
||||
public class Dept8001Application {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Dept8001Application.class, args);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
package org.learning.springcloud;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* Hello world!
|
||||
*
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class DeptApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(DeptApplication.class, args);
|
||||
}
|
||||
}
|
||||
|
|
@ -4,6 +4,8 @@ import org.learning.springcloud.domain.Dept;
|
|||
import org.learning.springcloud.result.ResultForm;
|
||||
import org.learning.springcloud.service.DeptService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
|
|
@ -19,6 +21,9 @@ public class DeptController {
|
|||
@Autowired
|
||||
public DeptService deptService;
|
||||
|
||||
@Autowired
|
||||
private DiscoveryClient client;
|
||||
|
||||
@GetMapping("/get/{id}")
|
||||
public Dept getDeptById(@PathVariable("id") Integer id){
|
||||
|
||||
|
|
@ -27,6 +32,7 @@ public class DeptController {
|
|||
// resultForm.setSuccess(false);
|
||||
// resultForm.setMessage("Id不能为空!");
|
||||
// }
|
||||
System.out.println("---------- 8001->/get/{id}");
|
||||
return deptService.getDeptById(id);
|
||||
// if(dept == null){
|
||||
// resultForm.setSuccess(false);
|
||||
|
|
@ -41,4 +47,21 @@ public class DeptController {
|
|||
public List<Dept> findAll(){
|
||||
return deptService.findAll();
|
||||
}
|
||||
|
||||
@RequestMapping("/discovery")
|
||||
public Object discovery(){
|
||||
//获取服务列表信息
|
||||
List<String> services = client.getServices();
|
||||
System.out.println(services);
|
||||
List<ServiceInstance> instances = client.getInstances("springcloud-provider-dept");
|
||||
for (ServiceInstance instance : instances) {
|
||||
System.out.println(
|
||||
instance.getHost()+"\t"+
|
||||
instance.getPort()+"\t"+
|
||||
instance.getUri()+"\t"+
|
||||
instance.getServiceId()+"\t"
|
||||
);
|
||||
}
|
||||
return client;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,4 +11,14 @@ spring:
|
|||
username: postgres
|
||||
password: 123
|
||||
application:
|
||||
name: springcloud-provider-dept
|
||||
name: springcloud-provider-dept #提供的是同一个服务所以应用名称保持不变
|
||||
#添加客户端Eureka的配置,服务注册到哪里
|
||||
eureka:
|
||||
client:
|
||||
service-url:
|
||||
defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7001.com:7001/eureka/,http://eureka7003.com:7003/eureka/
|
||||
instance:
|
||||
instance-id: springcloud-provider-dept-8001 #修改Eureka上的默认描述信息
|
||||
info:
|
||||
app.name: provider001
|
||||
# company.name: www.magicCubeSat.com
|
||||
|
|
@ -0,0 +1,84 @@
|
|||
<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.learning</groupId>
|
||||
<artifactId>springcloud</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<groupId>org.learning</groupId>
|
||||
<artifactId>springcloud-provider-dept-8002</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>springcloud-provider-dept-8001</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
</dependency>
|
||||
<!--导入数据库和数据源、mybatis、lombok-->
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>druid</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mybatis.spring.boot</groupId>
|
||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-test</artifactId>
|
||||
</dependency>
|
||||
<!--导入web依赖-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-devtools</artifactId>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
|
||||
<dependency>
|
||||
<groupId>com.github.pagehelper</groupId>
|
||||
<artifactId>pagehelper-spring-boot-starter</artifactId>
|
||||
<version>2.1.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.learning</groupId>
|
||||
<artifactId>springcloud-api</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<!-- <scope>compile</scope>-->
|
||||
</dependency>
|
||||
|
||||
<!--添加 eureka 服务配置和 actuator 完善监控信息 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-eureka</artifactId>
|
||||
<version>1.4.6.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-actuator</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
package org.learning.springcloud;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* Hello world!
|
||||
*
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@EnableEurekaClient //在服务启动后自动注册到Eureka中
|
||||
@EnableDiscoveryClient //服务发现
|
||||
public class Dept8002Application {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Dept8002Application.class, args);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,65 @@
|
|||
package org.learning.springcloud.controller;
|
||||
|
||||
import org.learning.springcloud.domain.Dept;
|
||||
import org.learning.springcloud.service.DeptService;
|
||||
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.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/dept")
|
||||
public class DeptController {
|
||||
|
||||
@Autowired
|
||||
public DeptService deptService;
|
||||
|
||||
@Autowired
|
||||
private DiscoveryClient client;
|
||||
|
||||
@GetMapping("/get/{id}")
|
||||
public Dept getDeptById(@PathVariable("id") Integer id){
|
||||
|
||||
// ResultForm resultForm = new ResultForm<Dept>();
|
||||
// if(id == null){
|
||||
// resultForm.setSuccess(false);
|
||||
// resultForm.setMessage("Id不能为空!");
|
||||
// }
|
||||
System.out.println("--------- 8002->/get/{id}");
|
||||
return deptService.getDeptById(id);
|
||||
// if(dept == null){
|
||||
// resultForm.setSuccess(false);
|
||||
// resultForm.setMessage("没有找到id为:"+id+"的部门!");
|
||||
// }else{
|
||||
// resultForm.setSuccess(true);
|
||||
// resultForm.setContent(dept);
|
||||
// }
|
||||
// return resultForm;
|
||||
}
|
||||
@GetMapping("/findAll")
|
||||
public List<Dept> findAll(){
|
||||
return deptService.findAll();
|
||||
}
|
||||
|
||||
@RequestMapping("/discovery")
|
||||
public Object discovery(){
|
||||
//获取服务列表信息
|
||||
List<String> services = client.getServices();
|
||||
System.out.println(services);
|
||||
List<ServiceInstance> instances = client.getInstances("springcloud-provider-dept");
|
||||
for (ServiceInstance instance : instances) {
|
||||
System.out.println(
|
||||
instance.getHost()+"\t"+
|
||||
instance.getPort()+"\t"+
|
||||
instance.getUri()+"\t"+
|
||||
instance.getServiceId()+"\t"
|
||||
);
|
||||
}
|
||||
return client;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
package org.learning.springcloud.mapper;
|
||||
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.learning.springcloud.domain.Dept;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface DeptMapper {
|
||||
Dept getDeptById(@Param("id") Integer id);
|
||||
|
||||
List<Dept> findAll();
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
package org.learning.springcloud.service;
|
||||
|
||||
import org.learning.springcloud.domain.Dept;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface DeptService {
|
||||
Dept getDeptById(Integer id);
|
||||
|
||||
List<Dept> findAll();
|
||||
}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
package org.learning.springcloud.service.impl;
|
||||
|
||||
import org.learning.springcloud.domain.Dept;
|
||||
import org.learning.springcloud.service.DeptService;
|
||||
import org.learning.springcloud.mapper.DeptMapper;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class DeptServiceImpl implements DeptService {
|
||||
@Autowired
|
||||
public DeptMapper deptMapper;
|
||||
@Override
|
||||
public Dept getDeptById(Integer id) {
|
||||
|
||||
return deptMapper.getDeptById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Dept> findAll() {
|
||||
return deptMapper.findAll();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
server:
|
||||
port: 8002
|
||||
mybatis:
|
||||
config-location: classpath:mybatis/mybatis-config.xml
|
||||
type-aliases-package: com.learning.springcloud.domian
|
||||
spring:
|
||||
datasource:
|
||||
type: com.alibaba.druid.pool.DruidDataSource
|
||||
driver-class-name: org.postgresql.Driver
|
||||
url: jdbc:postgresql://localhost:5432/postgres
|
||||
username: postgres
|
||||
password: 123
|
||||
application:
|
||||
name: springcloud-provider-dept #提供的是同一个服务所以应用名称保持不变
|
||||
#添加客户端Eureka的配置,服务注册到哪里
|
||||
eureka:
|
||||
client:
|
||||
service-url:
|
||||
defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7001.com:7001/eureka/,http://eureka7003.com:7003/eureka/
|
||||
instance:
|
||||
instance-id: springcloud-provider-dept-8002 #修改Eureka上的默认描述信息
|
||||
info:
|
||||
app.name: provider002
|
||||
# company.name: www.magicCubeSat.com
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
<?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="org.learning.springcloud.mapper.DeptMapper">
|
||||
<resultMap id="deptMap" type="org.learning.springcloud.domain.Dept">
|
||||
<!-- 专门用于映射主键 -->
|
||||
<!-- property表示Java属性名称,column表示列名 -->
|
||||
<id property="id" column="id" />
|
||||
<!-- result表示非主键列 -->
|
||||
<result property="name" column="NAME" />
|
||||
<!-- 还有一个密码没有设置!不需要! -->
|
||||
<!-- 其实NAME也不需要,因为名字相同! -->
|
||||
<result property="groupId" column="group_id" />
|
||||
</resultMap>
|
||||
<select id="getDeptById" parameterType="Integer" resultMap="deptMap">
|
||||
select * from dept where id = #{id,jdbcType=BIGINT}
|
||||
</select>
|
||||
<select id="findAll" resultMap="deptMap">
|
||||
select * from dept
|
||||
</select>
|
||||
</mapper>
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE configuration PUBLIC "-//mybatis.org//D/EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
|
||||
|
||||
<configuration>
|
||||
<properties>
|
||||
<property name="_defaultDatabaseId" value="oracle"/><!--使用的数据库类型(目前仅支持oracle,mysql)-->
|
||||
</properties>
|
||||
<settings>
|
||||
<!-- xml配置对于NULL值默认作为字符串来处理 -->
|
||||
<setting name="jdbcTypeForNull" value="NULL"/>
|
||||
<!-- <setting name="logImpl" value="NoLoggingImpl"/>-->
|
||||
<setting name="callSettersOnNulls" value="true"/>
|
||||
<setting name="cacheEnabled" value="true"/> <!--开启使用缓存-->
|
||||
<setting name="mapUnderscoreToCamelCase" value="true"/>
|
||||
</settings>
|
||||
<typeAliases>
|
||||
|
||||
</typeAliases>
|
||||
|
||||
<plugins>
|
||||
<!--<plugin interceptor="com.github.pagehelper.PageHelper">-->
|
||||
<plugin interceptor="com.github.pagehelper.PageInterceptor">
|
||||
<!--<property name="dialect" value="oracle"/>-->
|
||||
<!--<property name="helperDialect" value="oracle"/>-->
|
||||
<property name="offsetAsPageNum" value="true"/>
|
||||
<property name="rowBoundsWithCount" value="true"/>
|
||||
<property name="pageSizeZero" value="true"/> <!--true表示 pageSize=0时候,返回全部-->
|
||||
</plugin>
|
||||
</plugins>
|
||||
|
||||
<mappers>
|
||||
<!-- <package name="mapper.*"/>-->
|
||||
<mapper resource="mapper/DeptMapper.xml"></mapper>
|
||||
</mappers>
|
||||
|
||||
</configuration>
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
package org.learning.springcloud;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
/**
|
||||
* Unit test for simple App.
|
||||
*/
|
||||
public class AppTest
|
||||
extends TestCase
|
||||
{
|
||||
/**
|
||||
* Create the test case
|
||||
*
|
||||
* @param testName name of the test case
|
||||
*/
|
||||
public AppTest( String testName )
|
||||
{
|
||||
super( testName );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the suite of tests being tested
|
||||
*/
|
||||
public static Test suite()
|
||||
{
|
||||
return new TestSuite( AppTest.class );
|
||||
}
|
||||
|
||||
/**
|
||||
* Rigourous Test :-)
|
||||
*/
|
||||
public void testApp()
|
||||
{
|
||||
assertTrue( true );
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,84 @@
|
|||
<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.learning</groupId>
|
||||
<artifactId>springcloud</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<groupId>org.learning</groupId>
|
||||
<artifactId>springcloud-provider-dept-8003</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>springcloud-provider-dept-8001</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
</dependency>
|
||||
<!--导入数据库和数据源、mybatis、lombok-->
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>druid</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mybatis.spring.boot</groupId>
|
||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-test</artifactId>
|
||||
</dependency>
|
||||
<!--导入web依赖-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-devtools</artifactId>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
|
||||
<dependency>
|
||||
<groupId>com.github.pagehelper</groupId>
|
||||
<artifactId>pagehelper-spring-boot-starter</artifactId>
|
||||
<version>2.1.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.learning</groupId>
|
||||
<artifactId>springcloud-api</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<!-- <scope>compile</scope>-->
|
||||
</dependency>
|
||||
|
||||
<!--添加 eureka 服务配置和 actuator 完善监控信息 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-eureka</artifactId>
|
||||
<version>1.4.6.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-actuator</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
package org.learning.springcloud;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* Hello world!
|
||||
*
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@EnableEurekaClient //在服务启动后自动注册到Eureka中
|
||||
@EnableDiscoveryClient //服务发现
|
||||
public class Dept8003Application {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Dept8003Application.class, args);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,67 @@
|
|||
package org.learning.springcloud.controller;
|
||||
|
||||
import org.learning.springcloud.domain.Dept;
|
||||
import org.learning.springcloud.result.ResultForm;
|
||||
import org.learning.springcloud.service.DeptService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/dept")
|
||||
public class DeptController {
|
||||
|
||||
@Autowired
|
||||
public DeptService deptService;
|
||||
|
||||
@Autowired
|
||||
private DiscoveryClient client;
|
||||
|
||||
@GetMapping("/get/{id}")
|
||||
public Dept getDeptById(@PathVariable("id") Integer id){
|
||||
|
||||
// ResultForm resultForm = new ResultForm<Dept>();
|
||||
// if(id == null){
|
||||
// resultForm.setSuccess(false);
|
||||
// resultForm.setMessage("Id不能为空!");
|
||||
// }
|
||||
System.out.println("------------ 8003->/get/{id}");
|
||||
return deptService.getDeptById(id);
|
||||
// if(dept == null){
|
||||
// resultForm.setSuccess(false);
|
||||
// resultForm.setMessage("没有找到id为:"+id+"的部门!");
|
||||
// }else{
|
||||
// resultForm.setSuccess(true);
|
||||
// resultForm.setContent(dept);
|
||||
// }
|
||||
// return resultForm;
|
||||
}
|
||||
@GetMapping("/findAll")
|
||||
public List<Dept> findAll(){
|
||||
return deptService.findAll();
|
||||
}
|
||||
|
||||
@RequestMapping("/discovery")
|
||||
public Object discovery(){
|
||||
//获取服务列表信息
|
||||
List<String> services = client.getServices();
|
||||
System.out.println(services);
|
||||
List<ServiceInstance> instances = client.getInstances("springcloud-provider-dept");
|
||||
for (ServiceInstance instance : instances) {
|
||||
System.out.println(
|
||||
instance.getHost()+"\t"+
|
||||
instance.getPort()+"\t"+
|
||||
instance.getUri()+"\t"+
|
||||
instance.getServiceId()+"\t"
|
||||
);
|
||||
}
|
||||
return client;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
package org.learning.springcloud.mapper;
|
||||
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.learning.springcloud.domain.Dept;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface DeptMapper {
|
||||
Dept getDeptById(@Param("id") Integer id);
|
||||
|
||||
List<Dept> findAll();
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
package org.learning.springcloud.service;
|
||||
|
||||
import org.learning.springcloud.domain.Dept;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface DeptService {
|
||||
Dept getDeptById(Integer id);
|
||||
|
||||
List<Dept> findAll();
|
||||
}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
package org.learning.springcloud.service.impl;
|
||||
|
||||
import org.learning.springcloud.domain.Dept;
|
||||
import org.learning.springcloud.service.DeptService;
|
||||
import org.learning.springcloud.mapper.DeptMapper;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class DeptServiceImpl implements DeptService {
|
||||
@Autowired
|
||||
public DeptMapper deptMapper;
|
||||
@Override
|
||||
public Dept getDeptById(Integer id) {
|
||||
|
||||
return deptMapper.getDeptById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Dept> findAll() {
|
||||
return deptMapper.findAll();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
server:
|
||||
port: 8003
|
||||
mybatis:
|
||||
config-location: classpath:mybatis/mybatis-config.xml
|
||||
type-aliases-package: com.learning.springcloud.domian
|
||||
spring:
|
||||
datasource:
|
||||
type: com.alibaba.druid.pool.DruidDataSource
|
||||
driver-class-name: org.postgresql.Driver
|
||||
url: jdbc:postgresql://localhost:5432/postgres
|
||||
username: postgres
|
||||
password: 123
|
||||
application:
|
||||
name: springcloud-provider-dept #提供的是同一个服务所以应用名称保持不变
|
||||
#添加客户端Eureka的配置,服务注册到哪里
|
||||
eureka:
|
||||
client:
|
||||
service-url:
|
||||
defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7001.com:7001/eureka/,http://eureka7003.com:7003/eureka/
|
||||
instance:
|
||||
instance-id: springcloud-provider-dept-8003 #修改Eureka上的默认描述信息
|
||||
info:
|
||||
app.name: provider003
|
||||
# company.name: www.magicCubeSat.com
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
<?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="org.learning.springcloud.mapper.DeptMapper">
|
||||
<resultMap id="deptMap" type="org.learning.springcloud.domain.Dept">
|
||||
<!-- 专门用于映射主键 -->
|
||||
<!-- property表示Java属性名称,column表示列名 -->
|
||||
<id property="id" column="id" />
|
||||
<!-- result表示非主键列 -->
|
||||
<result property="name" column="NAME" />
|
||||
<!-- 还有一个密码没有设置!不需要! -->
|
||||
<!-- 其实NAME也不需要,因为名字相同! -->
|
||||
<result property="groupId" column="group_id" />
|
||||
</resultMap>
|
||||
<select id="getDeptById" parameterType="Integer" resultMap="deptMap">
|
||||
select * from dept where id = #{id,jdbcType=BIGINT}
|
||||
</select>
|
||||
<select id="findAll" resultMap="deptMap">
|
||||
select * from dept
|
||||
</select>
|
||||
</mapper>
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE configuration PUBLIC "-//mybatis.org//D/EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
|
||||
|
||||
<configuration>
|
||||
<properties>
|
||||
<property name="_defaultDatabaseId" value="oracle"/><!--使用的数据库类型(目前仅支持oracle,mysql)-->
|
||||
</properties>
|
||||
<settings>
|
||||
<!-- xml配置对于NULL值默认作为字符串来处理 -->
|
||||
<setting name="jdbcTypeForNull" value="NULL"/>
|
||||
<!-- <setting name="logImpl" value="NoLoggingImpl"/>-->
|
||||
<setting name="callSettersOnNulls" value="true"/>
|
||||
<setting name="cacheEnabled" value="true"/> <!--开启使用缓存-->
|
||||
<setting name="mapUnderscoreToCamelCase" value="true"/>
|
||||
</settings>
|
||||
<typeAliases>
|
||||
|
||||
</typeAliases>
|
||||
|
||||
<plugins>
|
||||
<!--<plugin interceptor="com.github.pagehelper.PageHelper">-->
|
||||
<plugin interceptor="com.github.pagehelper.PageInterceptor">
|
||||
<!--<property name="dialect" value="oracle"/>-->
|
||||
<!--<property name="helperDialect" value="oracle"/>-->
|
||||
<property name="offsetAsPageNum" value="true"/>
|
||||
<property name="rowBoundsWithCount" value="true"/>
|
||||
<property name="pageSizeZero" value="true"/> <!--true表示 pageSize=0时候,返回全部-->
|
||||
</plugin>
|
||||
</plugins>
|
||||
|
||||
<mappers>
|
||||
<!-- <package name="mapper.*"/>-->
|
||||
<mapper resource="mapper/DeptMapper.xml"></mapper>
|
||||
</mappers>
|
||||
|
||||
</configuration>
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
package org.learning.springcloud;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
/**
|
||||
* Unit test for simple App.
|
||||
*/
|
||||
public class AppTest
|
||||
extends TestCase
|
||||
{
|
||||
/**
|
||||
* Create the test case
|
||||
*
|
||||
* @param testName name of the test case
|
||||
*/
|
||||
public AppTest( String testName )
|
||||
{
|
||||
super( testName );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the suite of tests being tested
|
||||
*/
|
||||
public static Test suite()
|
||||
{
|
||||
return new TestSuite( AppTest.class );
|
||||
}
|
||||
|
||||
/**
|
||||
* Rigourous Test :-)
|
||||
*/
|
||||
public void testApp()
|
||||
{
|
||||
assertTrue( true );
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue