package com.example.springcloudservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class SpringcloudServiceApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudServiceApplication.class, args);
}
}
application.properties文件:
server.port=8888
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
pom文件:
"1.0" encoding="UTF-8"?>
"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">
4.0.0
com.example
springcloud-service
0.0.1-SNAPSHOT
jar
springcloud-service
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
1.5.7.RELEASE
UTF-8
UTF-8
1.8
Dalston.SR3
org.springframework.cloud
spring-cloud-starter-eureka-server
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
启动后访问 localhost:8888 出现如下图 ,这时你的 注册中心已经好了
@RestController
public class DemoController {
@Autowired
private HelloService helloService;
@Autowired
private HelloServiceApi serviceApi;
@RequestMapping("/getUser")
public String index(){
List<User> user=helloService.getUser(1);
return "Hello Spring Boot"+user.size();
}
@RequestMapping("/getStringApi")
public String getString(){
return serviceApi.getStringApi();
}
}
**自己的接口实现:**
package com.example.service;
import com.example.model.User;
import java.util.List;
public interface HelloService {
List<User> getUser(int id);
}
----------------------------------
package com.example.service;
import com.example.mapper.UserMapper;
import com.example.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Transactional
public class HelloServiceImpl implements HelloService{
@Autowired
private UserMapper userMapper;
public List<User> getUser(int id){
return userMapper.selectAll();
}
}
**调用外部服务接口:**
package com.example.service;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
@FeignClient(name = "client")
public interface HelloServiceApi {
@RequestMapping("/test")
String getStringApi();
}
**application.yml文件:**
server:
port: 8889
# context-path: /helloboot
tomcat:
max-threads: 800
uri-encoding: UTF-8
logging:
classpath: logback.xml
book:
author: ruxiang
name: spring boot
hello:
msg: ruxiang
# 项目配置
spring:
application:
name: demo
http:
encoding: UTF-8
datasource:
name: ikcrm
url: jdbc:mysql://自己的数据库地址:3306/数据库名称?characterEncoding=utf8&autoReconnect=true
username: ******
password: ******
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
filters: stat
maxActive: 100
initialSize: 2
maxWait: 60000
minIdle: 8
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 120000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 100
mybatis:
type-aliases-package: com.example.model
mapper-locations: classpath:mapper/*.xml
mapper:
mappers:
- com.example.model.MyMapper
not-empty: false
identity: MYSQL
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
# eureka地址
eureka:
client:
service-url:
defaultZone: http://localhost:8888/eureka/
**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.0modelVersion>
<groupId>com.examplegroupId>
<artifactId>demoartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>jarpackaging>
<name>demoname>
<description>Demo project for Spring Bootdescription>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>1.5.7.RELEASEversion>
<relativePath/>
parent>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
<mybatis.spring.version>1.2.4mybatis.spring.version>
<spring-cloud.version>Dalston.SR3spring-cloud.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>1.3.1version>
dependency>
<dependency>
<groupId>tk.mybatisgroupId>
<artifactId>mapper-spring-boot-starterartifactId>
<version>1.1.4version>
dependency>
<dependency>
<groupId>com.github.pagehelpergroupId>
<artifactId>pagehelper-spring-boot-starterartifactId>
<version>1.2.2version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.0.16version>
dependency>
<dependency>
<groupId>org.mybatis.extendgroupId>
<artifactId>mybatis-genericartifactId>
<version>1.0.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-eurekaartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-eureka-serverartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-feignartifactId>
dependency>
dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>${spring-cloud.version}version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
**启动类:**
@EnableEurekaClient //发现服务注解
@EnableFeignClients //声明
@EnableEurekaServer
@SpringBootApplication
@ComponentScan(basePackages = {"com.example.service" ,"com.example.controller"})
@MapperScan(basePackages = "com.example.mapper")
public class Application extends WebMvcConfigurerAdapter implements CommandLineRunner {
private Logger logger = LoggerFactory.getLogger(Application.class);
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... args) throws Exception {
logger.info("服务启动完成!");
}
}
package com.client.controller;
import com.client.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Autowired
private TestService testService;
@RequestMapping(value = "/test",method = RequestMethod.GET)
public String test(){
System.out.println("ccccccccccccccccc");
return testService.testss();
}
}
package com.client.service;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
@FeignClient(name = "demo")
public interface TestService {
@RequestMapping("/getUser")
String testss();
}
**application.properties文件:**
server.port=8890
eureka.client.service-url.defaultZone: http://localhost:8888/eureka/
spring.application.name=client
**pom文件:**
"1.0" encoding="UTF-8"?>
"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">
4.0.0
com.client
client
0.0.1-SNAPSHOT
jar
client
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
1.5.7.RELEASE
.build.sourceEncoding>UTF-8 .build.sourceEncoding>
.reporting.outputEncoding>UTF-8 .reporting.outputEncoding>
.version>1.8 .version>
.version>Dalston.SR3 .version>
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.cloud
spring-cloud-starter-feign
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-eureka-server
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
**启动类:**
package com.client;
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;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
@EnableEurekaClient //发现服务注解
//@EnableDiscoveryClient
@EnableFeignClients //声明
@EnableEurekaServer
@SpringBootApplication
//@ComponentScan(basePackages = {"com.client.service" ,"com.client.controller"})
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
}
}