SPRING INITIALIZR 构建工程
spring boot 可通过 SPRING INITIALIZR 构建项目访问 SPRING INITIALIZR 官网,填写项目相关信息后,生成项目。将下载的项目解压,打开 idea,file–>new–>project from existing sources。import project 选择 maven 项目,jdk 选择 1.8 或以上,一直 next 即可。
spring cloud server 提供服务注册
1. 修改 pom.xml
<?xml version=”1.0″ encoding=”UTF-8″?>
<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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/> <!– lookup parent from repository –>
</parent>
<groupId>com.gyt</groupId>
<artifactId>helloworld.Eureka.server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>helloworld.Eureka.server</name>
<description>Demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!–eureka server –>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!– spring boot test–>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
2. 通过 SPRING INITIALIZR 构建工程后,修改配置文件 application.properties,我这里使用的是 application.yml,两者只在格式上存在差别。3. 启动类 application.java 添加 @EnableEurekaServer 注解。
@EnableEurekaServer
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
4. 访问 http://localhost:8000/
spring cloud client 提供服务
1. 同样创建项目后,修改 pom.xml
<?xml version=”1.0″ encoding=”UTF-8″?>
<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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/> <!– lookup parent from repository –>
</parent>
<groupId>com.gyt</groupId>
<artifactId>helloworld.eureka.client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>helloworld.eureka.client</name>
<description>Demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
2.application.yml
eureka:
client:
serviceUrl:
#server 对应的地址
defaultZone: http://localhost:8000/eureka/
instance:
hostname: localhost
server:
port: 8089
spring:
application:
#应用名
name: service-helloworld
ribbon:
eureka:
enabled: true
其中关于 ribbon 的学习来自于 https://www.jianshu.com/p/f86…3.ApplicationClient.java
@SpringBootApplication
@EnableDiscoveryClient
public class ApplicationClient {
public static void main(String[] args) {
SpringApplication.run(ApplicationClient.class, args);
}
}
4.HelloController.java
@RestController
public class HelloController {
@RequestMapping(“/hello”)
public String index(@RequestParam String name) {
return “hello “+name+”,hahahaha!”;
}
}
5. 访问 http://localhost:8089/hello?name= w 此时再看服务端,多了 service-helloworld 这个应用。
spring cloud consumer 使用服务
1. 新建项目,修改 pom.xml
<?xml version=”1.0″ encoding=”UTF-8″?>
<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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/> <!– lookup parent from repository –>
</parent>
<groupId>com.gyt</groupId>
<artifactId>helloworld.eureka.consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>helloworld.eureka.consumer</name>
<description>Demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!–eureka server –>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!– spring boot test–>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
2.application.propertites【这里特意没有用 yml】
spring.application.name=spring-cloud-consumer
server.port=9001
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
3.ApplicationConsumer.java
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ApplicationConsumer {
public static void main(String[] args) {
SpringApplication.run(ApplicationConsumer.class, args);
}
}
4. 创建接口类,HelloTest.java
// 这里的 name 指定客户端的应用名
@FeignClient(name= “service-helloworld”)
public interface HelloTest {
// 这里 requestMapping 与客户端相同
@RequestMapping(value = “/hello”)
public String hello(@RequestParam(value = “name”) String name);
}
5.ConsumerController.java
@RestController
public class ConsumerController {
@Autowired
HelloTest helloTest;
@RequestMapping(“/hello/{name}”)
public String index(@PathVariable(“name”) String name) {
return helloTest.hello(name);
}
}
6. 访问 http://localhost:9001/hello/ w 此时,服务端存在两个应用